I am trying to test rhandsontable in R shiny app with shinytest package. But fail to access table3 and get a following error:
Error in session_makeRequest(self, private, endpoint, data, params, headers) : undefined is not an object (evaluating 'HTMLWidgets.getInstance(table3).hot')
##app.R
library(shiny)
library(rhandsontable)
create_df <-function() {
df <- data.frame(matrix(0.0, ncol = 2, nrow = 1))
return(df)
}
ui <- basicPage(
navbarPage("App", id="nav",fluid = TRUE,
tabPanel("Table 1", id="t1",
titlePanel("Table 1"),
hr(),
fluidRow(column(12,
tagList(
tags$h3("Table 1"),
rHandsontableOutput("table1")
)
)),
fluidRow(column(12,
tagList(
tags$h3("Table 2"),
rHandsontableOutput("table2")
)))
),
tabPanel("Table 3",id="t3",
titlePanel("Table 3"),
hr(),
fluidRow(column(12,
tagList(
tags$h3("Table 3"),
rHandsontableOutput("table3")
)
)))
)
)
server <- function(input, output, session) {
v = reactiveValues()
observe({ input$table1
if (!is.null(input$table1)) {
v$table1<- hot_to_r(input$table1)
} else {
v$table1<-create_df()
}
})
output$table1 <- renderRHandsontable({
rhandsontable(v$table1)
})
observe({ input$table2
if (!is.null(input$table2)) {
v$table2<- hot_to_r(input$table2)
} else {
v$table2<-create_df()
}
})
# observeEvent(input$df,{
# v$df2 <- v$df * 2
# })
output$table2 <- renderRHandsontable({
rhandsontable(v$table2)
})
observe({ input$table3
if (!is.null(input$table3)) {
v$table3<- hot_to_r(input$table3)
} else {
v$table3<-create_df()
}
})
output$table3 <- renderRHandsontable({
rhandsontable(v$table3)
})
}
shinyApp(ui = ui, server = server)
#shinytest script
library(shinytest)
app <- ShinyDriver$new("path to app")
app$snapshotInit("mytest")
app$snapshot(screenshot = FALSE)
script <- paste0(
"var $table = HTMLWidgets.getInstance(table3).hot;",
"$table.setDataAtCell([[0,0,'1'],[0,1,'1']]);"
)
app$executeScript(script)
app$snapshot(screenshot = FALSE)
My expectation of this script is that first two cells in first row will get values of 1 assigned to them, but it does not happen. Please advise how to solve it. Thank you.