闪亮的 renderDataTable table_cell_clicked

Mar*_*ssa 7 r shiny dt

我正在尝试使用 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)
Run Code Online (Sandbox Code Playgroud)

最小错误代码

gre*_*g L 6

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)
Run Code Online (Sandbox Code Playgroud)