【问题标题】:Shiny renderDataTable table_cell_clicked闪亮的 renderDataTable table_cell_clicked
【发布时间】:2018-05-02 11:06:25
【问题描述】:

我正在尝试使用 Shiny 创建一个表,用户可以在其中单击一行以查看有关该行的更多信息。我以为我明白如何做到这一点(见附件代码)。

但是,现在只要用户单击“getQueue”操作按钮,observeEvent(input$fileList_cell_clicked, {}) 似乎就会被调用。为什么在用户甚至有机会点击一行之前调用它?生成表的时候也调用吗?有没有办法解决?

我需要将“output$devel

感谢您的任何建议!

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)

minimal error code

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    DTinput$tableId_cell_clicked 初始化为一个空列表,这会导致observeEvent 触发,因为observeEvent 默认只忽略NULL 值。当此列表为空时,您可以通过插入 req(length(input$tableId_cell_clicked) &gt; 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)
    

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 2021-11-21
      • 2019-05-03
      • 2014-07-06
      • 2018-07-30
      • 1970-01-01
      • 2016-01-02
      • 2014-09-29
      • 2020-12-26
      相关资源
      最近更新 更多