闪亮的renderDataTable表格单元格点击

10
我正在尝试使用Shiny创建一个表格,用户可以单击行以查看有关该行的更多信息。我认为我理解如何做到这一点(请参见附加的代码)。
然而,现在当用户点击“getQueue”操作按钮时,似乎会调用observeEvent(input$fileList_cell_clicked, {})。为什么在用户甚至没有机会单击行之前就会调用它呢?生成表格时也会调用它吗?有没有办法避免这种情况?
我需要替换“output$devel <- renderText(”cell_clicked_called“)”的代码,如果没有实际单元格可引用,它将出现各种错误。
感谢您的任何建议!
ui <- fluidPage(
   actionButton("getQueue", "Get list of queued files"),
   verbatimTextOutput("devel"),
   DT::dataTableOutput("fileList")     
)

shinyServer <- function(input, output) {
   observeEvent(input$getQueue, {
   #get list of excel files
   toTable <<- data.frame("queueFiles" = list.files("queue/", pattern = "*.xlsx")) #need to catch if there are no files in queue
   output$fileList <- DT::renderDataTable({
     toTable
   }, selection = 'single') #, selection = list(mode = 'single', selected = as.character(1))
   })
   observeEvent(input$fileList_cell_clicked, {
     output$devel <- renderText("cell_clicked_called")
   })}

shinyApp(ui = ui, server = shinyServer)

最小错误代码

1个回答

9

DT 初始化 input$tableId_cell_clicked 为空列表,这会导致 observeEvent 触发,因为默认情况下 observeEvent 只会忽略 NULL 值。您可以通过插入类似于 req(length(input$tableId_cell_clicked) > 0) 的内容来停止反应式表达式。

以下是稍微修改过的示例,演示了这一点。

library(shiny)

ui <- fluidPage(
  actionButton("getQueue", "Get list of queued files"),
  verbatimTextOutput("devel"),
  DT::dataTableOutput("fileList")     
)

shinyServer <- function(input, output) {

  tbl <- eventReactive(input$getQueue, {
    mtcars
  })

  output$fileList <- DT::renderDataTable({
    tbl()
  }, selection = 'single')

  output$devel <- renderPrint({
    req(length(input$fileList_cell_clicked) > 0)
    input$fileList_cell_clicked
  })
}

shinyApp(ui = ui, server = shinyServer)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接