我有一个 Shiny 应用程序,可以添加和删除某些事件的许多选项卡。大多数选项卡包含相互依赖的数据表。每个数据表都创建了一个代理。问题是,如果我用 tableProxy %>% selectRows(NULL)
重置表选择,然后使用 removeTab()
删除带有该表的选项卡, 代表选定行的值不会重置。
这是一个最小的例子。要重现,请选择表中的任何行并转到下一个选项卡。现在一切都很好。打印所选行的索引。但是,如果您单击该按钮,您会注意到即使重置了选择并且包含表格的选项卡不再存在,打印的值仍然相同。任何想法如何解决这个问题?
软件包版本:shiny_1.0.5
, DT_0.2
library(shiny)
library(DT)
ui <- fluidPage(
tabsetPanel(
id = "tabs",
tabPanel("Table",
dataTableOutput("table")
),
tabPanel("Button & text",
actionButton("button", "Reset selection & remove the tab"),
textOutput("text")
)
)
)
server <- function(input, output, session) {
output$table <- DT::renderDataTable({
head(mtcars)
})
tableProxy <- dataTableProxy('table')
observeEvent(input$button, {
tableProxy %>% selectRows(NULL)
removeTab("tabs", "Table")
})
observe({
print(paste("Observer", input$table_rows_selected))
})
data <- reactive({
print(paste("Reactive", input$table_rows_selected))
input$table_rows_selected
})
output$text <- renderText({
data()
})
}
shinyApp(ui = ui, server = server)
最佳答案
不确定是否有一个干净的解决方案。一种选择是添加中间 reactiveVal
对象,并将其设置为 NULL
当你移除 table 时。或者在您的情况下,您可能需要考虑 reactiveValues
,并使用表的名称作为其元素的名称来存储您选择的行,因为听起来您正在动态生成很多表(?)。
library(shiny)
library(DT)
ui <- fluidPage(
tabsetPanel(
id = "tabs",
tabPanel("Table",
dataTableOutput("table")
),
tabPanel("Button & text",
actionButton("button", "Reset selection & remove the tab"),
textOutput("text")
)
)
)
server <- function(input, output, session) {
output$table <- DT::renderDataTable({
head(mtcars)
})
selected_rows <- reactiveVal()
tableProxy <- dataTableProxy('table')
observeEvent(input$button, {
tableProxy %>% selectRows(NULL)
selected_rows(NULL)
removeTab("tabs", "Table")
})
observe({
print(paste("Observer", input$table_rows_selected))
isolate(selected_rows(input$table_rows_selected))
})
data <- reactive({
print(paste("Reactive", selected_rows()))
selected_rows()
})
output$text <- renderText({
data()
})
}
shinyApp(ui = ui, server = server)
希望这可以帮助!
关于重置数据表中的行选择并在 Shiny 中删除包含该表的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229750/