【问题标题】:Shiny DT appearance messed up when selected rows used as reactive values当所选行用作反应值时,闪亮的dt外观搞砸了
【发布时间】:2018-12-05 11:03:51
【问题描述】:

通过 DT 包中的 DataTables 界面显示的表格在使用其输入来自第一个表格中选择的行的反应值时显得混乱(例如无序的元素、奇怪的分页...)。使用 R 版本 3.4.3,以及闪亮的 1.1.0 和 DT 0.4,它们都来自 CRAN。

最少的代码:

library(shiny)
library(DT)

ui <- fluidPage(
  DT::dataTableOutput("dt"),
  actionButton("go", "Go"),
  wellPanel(DT::dataTableOutput("selected"))
)

server <- function(input, output, session) {
  output$dt <-  DT::renderDataTable({
    DT::datatable(
      mtcars,
      style = 'bootstrap',
      filter = 'top',
      rownames = FALSE,
      extensions = 'Buttons',
      selection = list(mode = 'single'),
      options = list(
        pageLength = 10,
        dom = '<"top"ifl>t<"bottom"Bp>',
        buttons = c('copy', 'csv', 'excel'),
        searchHighlight = TRUE
      )
    )
  })

  rv <- reactiveValues(val = FALSE)
  observeEvent(input$go, {
    rv$val <- input$go
  })
  observeEvent(input$dt_rows_selected, {
    rv$val <- FALSE
  })

  output$selected <- DT::renderDataTable({
    if (rv$val == FALSE)
      return()

    reactive({
      validate(need(input$dt_rows_selected != "", "Select a row."))
      mtcars[input$dt_rows_selected, ]
    }) -> .mtcars

    isolate({
      DT::datatable(
        .mtcars(),
        style = 'bootstrap',
        filter = 'top',
        rownames = FALSE,
        extensions = 'Buttons',
        selection = list(mode = 'single'),
        options = list(
          pageLength = 10,
          dom = '<"top"ifl>t<"bottom"Bp>',
          buttons = c('copy', 'csv', 'excel'),
          searchHighlight = TRUE
        )
      ) -> table
    })
    table
  })

}

shinyApp(ui, server)

没有第二张桌子看起来还不错:

【问题讨论】:

  • 当您注意到“没有第二张桌子看起来不错:”时,您对代码做了哪些更改?如第一个表中的值仍然是反应性的吗?
  • 是的,我应该注意到这一点。我只在服务器和 ui 中保留了第一个表 (dt)。第一个表中的值不是反应性的。
  • 添加第二个表没有反应值,只是为了测试,看起来正常吗?

标签: r datatables shiny reactive-programming


【解决方案1】:

这个问题是由style = 'bootstrap' 部分引起的,它不能很好地与return(NULL) 一起工作。将输出中的if (rv$val == FALSE) return() 替换为req(rv$val) 已解决问题。已参考here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 2019-12-03
    • 2020-11-20
    • 1970-01-01
    • 2021-01-05
    • 2020-05-28
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多