【问题标题】:R, Shiny Setting DataTable IDR,闪亮设置数据表 ID
【发布时间】:2018-12-02 03:23:27
【问题描述】:

我已经使用 mapply 创建了大量的数据表,但是,我需要在接下来的步骤中访问这些数据表。如果用户没有指定 ID,R 会为这些表分配随机 ID。这是我想做的一个例子:

library(shiny)

ui <- fluidPage(
  h2("Last clicked:"),
  verbatimTextOutput("last_clicked"),
  actionButton("reset", "Reset clicked value"),
  h2("Datatable:"),
  DT::dataTableOutput("dt")
)

server <- function(input, output) {

  # the last clicke value
  output$last_clicked <- renderPrint({
    str(last())
  })

  output$dt <- DT::renderDataTable({
    DT::datatable(head(mtcars, 2), elementId = "DT_Test")
  })

  observeEvent(input$dt_cell_clicked, {
    validate(need(length(input$dt_cell_clicked) > 0, ''))
    print("You clicked something!")
  })

  myProxy = DT::dataTableProxy('dt')
  last = reactiveVal(NULL)

  observe({
    last(input$dt_cell_clicked)
  })

  observeEvent(input$reset, {
    DT::selectRows(myProxy, NULL)
    last(NULL)
    output$dt <- DT::renderDataTable({    
      DT::datatable(head(mtcars, 2))      
    })                                    
  })
}

shinyApp(ui, server)

如果我查看 html,elementID 并没有变成我想要的,实际上,R 给出了警告:

Warning in origRenderFunc() :
  Ignoring explicitly provided widget ID "DT_Test"; Shiny doesn't use them

【问题讨论】:

    标签: r datatable shiny


    【解决方案1】:

    即使在通话之后,仍然不确定您要做什么。 但是如果你有一个数据表列表并且你想访问它们,它的工作原理是这样的:

    library(shiny)
    library(purrr)
    
    ui <- fluidPage(
      h2("Last clicked:"),
      verbatimTextOutput("last_clicked"),
      h2("elementId values"),
      verbatimTextOutput("elementId_values"),
      actionButton("reset", "Reset clicked value"),
      h2("Datatable:"),
      DT::dataTableOutput("dt")
    )
    
    server <- function(input, output) {
    
      # the last clicke value
      output$last_clicked <- renderPrint({
        str(last())
      })
    
      table <- DT::datatable(head(mtcars, 2), elementId = "DT_Test")
      table2 <- DT::datatable(tail(mtcars, 1), elementId = "DT_Test2")
    
      list_of_data_tables <- list(table, table2)
      element_ids <- purrr::map(list_of_data_tables, "elementId")
    
      output$elementId_values <- renderPrint({
        element_ids
      })
    
      output$dt <- DT::renderDataTable({
        list_of_data_tables[[which(element_ids == "DT_Test2")]]
      })
    
    
      observeEvent(input$dt_cell_clicked, {
        validate(need(length(input$dt_cell_clicked) > 0, ''))
        print("You clicked something!")
      })
    
      myProxy = DT::dataTableProxy('dt')
      last = reactiveVal(NULL)
    
      observe({
        last(input$dt_cell_clicked)
      })
    
      observeEvent(input$reset, {
        DT::selectRows(myProxy, NULL)
        last(NULL)
        output$dt <- DT::renderDataTable({    
          DT::datatable(head(mtcars, 2))      
        })                                    
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 不完全是我要找的...如果你看一下 HTML,你会看到 datatbale 的 id 仍然是 dt。它根本没有改变。
    猜你喜欢
    • 1970-01-01
    • 2014-10-02
    • 2014-05-13
    • 1970-01-01
    • 2021-06-21
    • 2014-09-18
    • 2017-11-15
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多