【问题标题】:Unit Testing R Shiny App rhandsontable with shinytest单元测试 R Shiny App rhandsontable 与 shinytest
【发布时间】:2019-07-26 21:23:24
【问题描述】:

我正在尝试使用 shinytest 包在 R 闪亮应用程序中测试 rhandsontable。但是访问table3失败,报如下错误:

session_makeRequest(self, private, endpoint, data, params, headers) 中的错误: undefined 不是对象(评估 '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)

我对这个脚本的期望是第一行中的前两个单元格将获得分配给它们的值 1,但它不会发生。请告知如何解决它。谢谢。

【问题讨论】:

    标签: r unit-testing shiny rhandsontable


    【解决方案1】:

    这是因为您在不在包含table3 的选项卡中时执行脚本。试试:

    app <- ShinyDriver$new("../")
    app$snapshotInit("mytest")
    
    app$setInputs(nav = "Table 3") # <-- go the tab containing 'table3'
    
    script <- paste0(
      "var $table = HTMLWidgets.getInstance(table3).hot;",
      "$table.setDataAtCell([[0,0,'1'],[0,1,'1']]);"
    )
    app$executeScript(script)
    
    app$snapshot()
    

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 2018-03-14
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多