选择同一组 R 中的多个单元格可反应(可能还有 actionButtons)

Selecting multiple cells within the same group R reactable (and maybe actionButtons)

下面的代码产生以下 table:

目标 是能够 select 制造商 列中的一个类别(例如/别克)并且4 个模型(2 个来自中型,2 个来自大型)得到 selected。目前 Select All 功能仅适用于按 Type.

分组的模型

我尝试了什么: ActionButton 和 observeEvent 与 updateReactable("table", selected = ...) 问题是我想让用户能够 select all Models from multiple Manufacturer categories (ex/ The user selects all rows under Acura AND all rows在别克之下)。我尝试过的操作按钮仅适用于一个类别,如果单击另一个按钮,则 table 会重新设置。如果有帮助,很高兴包含这个失败的代码。

代码:

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)

也许您正在寻找这个

library(shiny)
library(reactable)

data <- MASS::Cars93[, 1:7]

ui <- fluidPage(
  selectInput("filter_Man", "Filter Manufacturer", unique(data$Manufacturer), multiple = TRUE),
  reactableOutput("table")
)

server <- function(input, output) {
  
  filtered_df <- eventReactive(input$filter_Man, {
    if (length(input$filter_Man) > 0) {
      data[data$Manufacturer %in% input$filter_Man, ]
    } else {data}
  }) 
  
  output$table <- renderReactable({
    reactable(
      filtered_df(),
      filterable = TRUE,
      searchable = TRUE,
      selection = "multiple",
      groupBy = c("Manufacturer",
                  "Type")
      
    )
  })

}

shinyApp(ui, server)

我找到了一个可行的解决方案

问题:我试图让用户能够select给定类型组中的所有模型此外 能够 select 较大制造商组中的所有型号。

我的解决方案: 用户能够 select 不同的制造商 filter_man 并且这存储在 select_man.

如果用户开始 select(通过复选框)模型,这将存储在 selected 中。如果省略此选项,则如果用户制作个人 select 离子,然后决定要 select 来自整个制造商组的模型,则个人 select 离子将被删除,只留下 select离子来自select_man。这就是为什么 obsereEvent 包括 selected()(单个电池 select 离子)和 select_man()(来自特定制造商的所有型号)

代码:

library(shiny)
library(reactable)

data <- MASS::Cars93[, 1:7]

ui <- fluidPage(
  selectInput("filter_man", "Manufacturer:", unique(data$Manufacturer), multiple = TRUE),
  actionButton("selections", "Select Manufacturer",class ="btn btn-primary btn-md"),
  reactableOutput("table")
)

server <- function(input, output) {
  
  
  output$table <- renderReactable({
    reactable(
      data,
      filterable = TRUE,
      searchable = TRUE,
      selection = "multiple",
      groupBy = c("Manufacturer",
                  "Type")
      
    )
  })
  
  selected <- reactive(getReactableState("table", "selected"))  
  
  select_man <- eventReactive(input$filter_man,{
    which(data$Manufacturer%in% input$filter_man )
  })
  

  
  observeEvent(input$selections, {
    # Select rows
    updateReactable("table", selected = c(selected(), select_man()))
  })
  
}

shinyApp(ui, server)