【发布时间】: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