R Shiny:从分组的复选框列表中提取选中的值

Rshiny: Extract checked values from a list of grouped checkboxes

我正在尝试为分组变量中的每个车型制作复选框,'cylinders'。

为了实现这一点,我正在使用 lapply() 遍历各个组,在折叠井面板中为每个组创建一个 groupedCheckbox。

但是只读取了部分选中的车型。最初可以读取第一个气缸组中所有已检查的汽车,但不能读取第二组中的那些 !

然而,一旦勾选了第一组中的一些车型,那么第二组中的汽车也会被读取。仅读取第一个框中的复选框的多个复选框组的图像

MultipleGroupCheckboxProblem

有谁知道如何将所有打勾的汽车提取到反应向量中?

我觉得解决方案可能涉及正确使用 lapply and/or unlist。

汽车名称也源自输入的行名称 table。

代码在这里,使用 mtcars :

library(shiny)
library(shinyBS)
ui <- fixedPage(
  h2("R Shiny: Multiple collapsing tickboxes"),
  

  
  tags$style(HTML("
.checkbox{margin-top: -20px; margin-left: 0px; margin-bottom: -5px;padding:-5px; margin-right: -400px;}
  .panel{margin: -5px;}")),

  uiOutput("grouped.tickboxes"), 
  
  textOutput("selectedtext")
)

# .panel{margin: 2px;padding:1px}")),



server <- function(input, output, session) {
  
  output$grouped.tickboxes <- renderUI({
    
    lapply(sort(unique(mtcars$cyl)), function(x) {
      
      fluidRow(
        
        div(tags$style(HTML("
                 .checkbox{margin: 0px; ;padding:0px; margin-right: -400px;}")),
            
        bsCollapsePanel(paste0("Cylinders: ", x), 
                        style = "color:grey; border-top: 1px solid black",
                        # style = "color:grey;",
                        
                        column(12,
                               
                               checkboxGroupInput(inputId = "stuff", 
                                                  NULL, choices = sort(row.names(subset(mtcars, cyl %in% x))))))
      )
      )
    })
  })
  
  
  seltex = reactive({
    ## maybe need to use lapply to read values.
    # paste0(input$stuff, collapse = ", ")
    # paste0(as.vector(unlist(input$stuff, use.names = FALSE)), collapse = ", ")
    # as.vector(unlist(input$stuff, use.names = FALSE))[1]
    # head(str(input$stuff))
    # lapply(input$stuff, str(input$stuff)[2]
    # paste0(unlist(unlist(unlist(input$stuff), use.names = FALSE)), collapse = ", ")
    # paste0(unlist(unlist(unlist(input$stuff)), use.names = FALSE), collapse = ", ")
    # paste0(unlist(unlist(input$stuff)), collapse = ", ")
    paste0("Selected cars : ", paste0(unlist(input$stuff), collapse = ", "))
  })

  output$selectedtext = renderText({ as.character(seltex() )})
  
}


# grouped.tickboxes

shinyApp(ui, server)

据我所知,问题的出现是因为您将相同的 inputId 分配给所有三个复选框面板。因此,使您的应用正常工作的一种方法是为复选框面板分配不同的 inputId。试试这个:

library(shiny)
library(shinyBS)

ui <- fixedPage(
  h2("R Shiny: Multiple collapsing tickboxes"),
 
  tags$style(HTML("
.checkbox{margin-top: -20px; margin-left: 0px; margin-bottom: -5px;padding:-5px; margin-right: -400px;}
  .panel{margin: -5px;}")),
  
  uiOutput("grouped.tickboxes"), 
  
  textOutput("selectedtext")
)

# .panel{margin: 2px;padding:1px}")),

server <- function(input, output, session) {
  
  output$grouped.tickboxes <- renderUI({
    
    lapply(sort(unique(mtcars$cyl)), function(x) {
      
      fluidRow(
        
        div(tags$style(HTML("
                 .checkbox{margin: 0px; ;padding:0px; margin-right: -400px;}")),
            
            bsCollapsePanel(paste0("Cylinders: ", x), 
                            style = "color:grey; border-top: 1px solid black",
                            # style = "color:grey;",
                            
                            column(12,
                                   
                                   checkboxGroupInput(inputId = paste0("stuff", x), 
                                                      NULL, choices = sort(row.names(subset(mtcars, cyl %in% x))))))
        )
      )
    })
  })
  
  
  seltex = reactive({
    cars <- purrr::reduce(sort(unique(mtcars$cyl)), ~ c(.x, input[[paste0("stuff", .y)]]), .init = character(0))
    
    paste0("Selected cars : ", paste0(cars, collapse = ", "))
  })
  
  output$selectedtext = renderText({ seltex() })
  
}


# grouped.tickboxes

shinyApp(ui, server)