多个'标签'单个复选框组

时间:2016-12-06 16:53:53

标签: r shiny shinydashboard

闪亮/ shinydashboard的新手。

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

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

 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'则不起作用。有没有人有办法实现这一目标?

1 个答案:

答案 0 :(得分:1)

您可以将您的选择包装在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)

enter image description here