【问题标题】:Testing editable DT using Shinytest使用 Shinytest 测试可编辑的 DT
【发布时间】:2020-01-25 13:49:49
【问题描述】:

我正在尝试为我一直在开发的闪亮应用创建单元测试,但不知道如何为可编辑的 DT 表输入值。

示例应用:

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("table"), 
  textOutput("mean")
)

server <- function(input, output) {
  tableUpdate <- reactiveVal(0)

  table <- data.frame(A = 1:5, B = 6:10, C = 11:15)

  output$table <- renderDT({table},
    options = list(paging = FALSE, dom = 't'),
    selection = 'none',
    server = FALSE,
    editable = list(target = 'row')
  )

  observeEvent(input$table_cell_edit, {
    table <<- editData(table, input$table_cell_edit)
    tableUpdate(tableUpdate() + 1)
  })

  output$mean <- renderText({
    tableUpdate()
    paste(mean(table$A), mean(table$B), mean(table$C))
  })
}

shinyApp(ui, server)

示例测试:

library(shinytest)

app <- ShinyDriver$new(".")
app$setInputs(table_cell_clicked = list(row = 2, col = 2, value = 7), allowInputNoBinding_ = TRUE)
app$setInputs(table_cell_edit = data.frame(row = 2, col = 0:3, value = "1"), 
              allowInputNoBinding_ = TRUE, priority_ = "event", wait_ = FALSE, values_ = FALSE)
app$takeScreenshot()
app$stop()
rm(app)

当我记录测试时,我主要保留了它的输出方式,但我已经更正了 table_cell_edit 和 table_cell_clicked 的 setInputs 值(作为向量输出)。

这给出了错误: Error in sd_getAllValues(self, private, input, output, export) : Unable to fetch all values from server. Is target app running with options(shiny.testmode=TRUE?)

在测试模式下运行并不能解决问题。

【问题讨论】:

  • 您的app.R需要调整,未能成功编辑和存储已编辑的值。还有,要不要在app.R中定义table_cell_clicked,代码好像在那里找不到。

标签: r shiny dt


【解决方案1】:

在遇到同样的问题时,我设法解决了您的问题。 错误就在这里(当您尝试更新反应值时):

tableUpdate(tableUpdate() + 1)

而不是......

tableUpdate(表)

请在下面找到更新后的完整代码。

library(shiny)
library(DT)

ui <- fluidPage(
    DTOutput("tabled"),
    textOutput("mean")
)

server <- function(input, output) {
    tableUpdate <- reactiveVal(NULL)
    table <- data.frame(A = 1:5, B = 6:10, C = 11:15)
    output$tabled <- renderDT({table},
                             options = list(paging = FALSE, dom = 't'),
                             selection = 'none',
                             server = TRUE,
                             editable = 'row')
    observeEvent(input$tabled_cell_edit, {
        table<<- editData(table, input$tabled_cell_edit,'tabled')
        tableUpdate(table)
        a<-mean(table$A)
        b<-mean(table$B)
        c<-mean(table$C)
    output$mean <- renderText(paste(a,b,c))
})
}
shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2019-09-05
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多