单个复选框组的多个 'labels'

Multiple 'labels' for single checkbox group

shiny/shinydashboard 的新手。

我有一系列选项,我希望用户能够select其中一个或多个。 checkboxGroupInput 完成这项工作,但这意味着我只能为所有选项设置一个标签。我想要的是能够标记我的复选框的子集,但将 selected 选项 作为单个变量传递给服务器 .

例如,假设我想显示按类型分组的管弦乐器(管乐器、铜管乐器...);

 library(shinydashboard)

    dashboardPage(
      dashboardHeader(title = 'My Orchestra'),

      dashboardSidebar(
        sidebarMenu()
        ),

      dashboardBody(
        fluidRow(
          box(
            checkboxGroupInput("my_orchestra",
                               label = "String",
                               choices = c("Violin" = "Violin", "Cello" = "Cello"),
                               inline = T
                               ),

            checkboxGroupInput("my_orchestra",
                               label = "Woodwind",
                               choices = c("Bassoon" = "Bassoon", "Flute" = "Flute"),
                               inline = T
                               ),

            checkboxGroupInput("my_orchestra",
                               label = "Brass",
                               choices = c("Trumpet" = "Trumpet", "Sax" = "Sax"),
                               inline = T
                               )
            ))
        )
   )

无论选中哪个选项,我都希望这些选项可以在 server.R 中作为 input$my_orchestra 访问。正如您在上面看到的,我试图通过将所有复选框组命名为 'my_orchestra' 来做到这一点,但这是行不通的。有没有人有办法实现这一目标?

您可以将您的选择包含在 reactiveValues 中,然后像我一样将其用作 v$my_orchestra

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = 'My Orchestra'),

  dashboardSidebar(
    sidebarMenu()
  ),

  dashboardBody(
    fluidRow(
      box(
        checkboxGroupInput("my_orchestra1",
                           label = "String",
                           choices = c("Violin" = "Violin", "Cello" = "Cello"),
                           inline = T),

        checkboxGroupInput("my_orchestra2",
                           label = "Woodwind",
                           choices = c("Bassoon" = "Bassoon", "Flute" = "Flute"),
                           inline = T),

        checkboxGroupInput("my_orchestra3",
                           label = "Brass",
                           choices = c("Trumpet" = "Trumpet", "Sax" = "Sax"),
                           inline = T)
      ),
      textOutput("Selected")
      )
  )
)

server <- (function(input, output, session) {

  v <- reactiveValues()
  observe({
    v$my_orchestra <- c(input$my_orchestra1,input$my_orchestra2,input$my_orchestra3)
  })
  output$Selected <- renderText({v$my_orchestra})
})

shinyApp(ui, server)