下面的代码生成下表:
目标是能够在制造商列中选择一个类别(例如/别克),并选择 4 款车型(2 款来自中型车,2 款来自大型车)。现在,全选功能仅适用于按类型分组的模型。
我尝试了什么: ActionButton 和 observeEvent updateReactable("table", selected = ...)
问题是我想让用户能够从多个制造商类别中选择所有 模型(例如/用户选择 Acura 下的所有行和别克下的所有行)。我尝试过的操作按钮仅适用于一个类别,如果单击另一个按钮,表格会重新设置。如果有帮助,很高兴包含这个失败的代码。
代码:
library(shiny)
library(reactable)
data <- MASS::Cars93[, 1:7]
ui <- fluidPage(
# actionButton("select_btn", "Select rows"),
# actionButton("clear_btn", "Clear selection"),
# actionButton("expand_btn", "Expand rows"),
# actionButton("collapse_btn", "Collapse rows"),
# actionButton("page_btn", "Change page"),
# selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
reactableOutput("table")
)
server <- function(input, output) {
output$table <- renderReactable({
reactable(
data,
filterable = TRUE,
searchable = TRUE,
selection = "multiple",
groupBy = c("Manufacturer",
"Type")
)
})
# observeEvent(input$select_btn, {
# # Select rows
# updateReactable("table", selected = c(1, 3, 5))
# })
#
# observeEvent(input$clear_btn, {
# # Clear row selection
# updateReactable("table", selected = NA)
# })
#
# observeEvent(input$expand_btn, {
# # Expand all rows
# updateReactable("table", expanded = TRUE)
# })
#
# observeEvent(input$collapse_btn, {
# # Collapse all rows
# updateReactable("table", expanded = FALSE)
# })
#
# observeEvent(input$page_btn, {
# # Change current page
# updateReactable("table", page = 3)
# })
#
# observe({
# # Filter data
# filtered <- if (length(input$filter_type) > 0) {
# data[data$Type %in% input$filter_type, ]
# } else {
# data
# }
# updateReactable("table", data = filtered)
# })
}
shinyApp(ui, server)