当选定的行用作反应值时,DT的闪亮外观混乱

时间:2018-06-26 16:38:31

标签: r datatables shiny reactive-programming

当使用输入值来自第一个表中选择的行的反应性值时,通过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)

在没有第二张桌子的情况下看起来还不错:

1 个答案:

答案 0 :(得分:0)

该问题是由部分style = 'bootstrap'return(NULL)配合使用引起的。在输出中将if (rv$val == FALSE) return()替换为req(rv$val)已解决了该问题。已引用here