我正在尝试为一直在工作的闪亮应用程序创建单元测试,但无法解决如何为可编辑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?)
以测试模式运行不能解决问题。
答案 0 :(得分:0)
在遇到同样的问题时,我设法解决了您的问题。 错误就在这里(当您尝试更新反应值时):
tableUpdate(tableUpdate() + 1)
而不是....
tableUpdate(table)
请在下面找到更新后的完整代码。
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)