【发布时间】:2018-12-06 11:39:27
【问题描述】:
我正在尝试做一些有点棘手的事情,我希望有人可以帮助我。
我想在数据表中添加selectInput。
如果我启动应用程序,我会看到输入 col_1、col_2.. 与数据表连接良好(您可以切换到 a、b 或 c)
但是
如果我更新数据集(从iris 到mtcars),输入和数据表之间的连接就会丢失。现在,如果您更改 selectinput,则日志不会显示修改。如何保留链接?
我使用shiny.bindAll() 和shiny.unbindAll() 做了一些测试,但没有成功。
有什么想法吗?
请查看应用程序:
library(shiny)
library(DT)
library(shinyjs)
library(purrr)
ui <- fluidPage(
selectInput("data","choose data",choices = c("iris","mtcars")),
DT::DTOutput("tableau"),
verbatimTextOutput("log")
)
server <- function(input, output, session) {
dataset <- reactive({
switch (input$data,
"iris" = iris,
"mtcars" = mtcars
)
})
output$tableau <- DT::renderDT({
col_names<-
seq_along(dataset()) %>%
map(~selectInput(
inputId = paste0("col_",.x),
label = NULL,
choices = c("a","b","c"))) %>%
map(as.character)
DT::datatable(dataset(),
options = list(ordering = FALSE,
preDrawCallback = JS("function() {
Shiny.unbindAll(this.api().table().node()); }"),
drawCallback = JS("function() { Shiny.bindAll(this.api().table().node());
}")
),
colnames = col_names,
escape = FALSE
)
})
output$log <- renderPrint({
lst <- reactiveValuesToList(input)
lst[order(names(lst))]
})
}
shinyApp(ui, server)
【问题讨论】:
标签: javascript r datatables shiny