代码之家  ›  专栏  ›  技术社区  ›  Marissa

闪亮的renderDataTable table\u cell\u已单击

  •  8
  • Marissa  · 技术社区  · 8 年前

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

    然而,现在只要用户单击“getQueue”操作按钮,observeEvent(input$fileList\u cell\u clicked,{})似乎就会被调用。为什么在用户有机会点击一行之前调用它?生成表时也调用它吗?有什么办法吗?

    我需要用代码替换“output$devel<-renderText(“cell\u clicked\u 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)
    

    minimal error code

    1 回复  |  直到 8 年前
        1
  •  7
  •   greg L    8 年前

    DT 初始化 input$tableId_cell_clicked 作为空列表 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)