R闪亮仪表板中的反应下拉菜单

时间:2018-06-08 15:58:03

标签: r shiny shinydashboard

这里有新亮点。我正在创建一个仪表板,并有不同的类别和子类别。我想做的是如下:

当屏幕加载时,我希望所有类别和子类别都可供选择,但如果我选择类别' 1',那么只有' sub1'和' sub2'应该可供选择。所以就像它被类别过滤一样。 我如何在闪亮的服务器文件中实现这一点?  我的代码的一个简短示例如下:(服务器是sudo代码)

UI -->     
    dashboardSidebar(
        sidebarMenu(
          menuItem("CSF2", tabName = "CSF2", icon=icon("bar-chart")),
            selectInput("category", "Select a category", c("1", "2"),
            selectInput("subcategory", "Select a subcategory",       c("sub1", "sub2", "sub3" "sub4"),
        )),


server--->
shinyServer(function(input,output){
    if (category_selected == 1){subcategoryOptions= c(sub1,sub2)}
    if (category_selected == 2){subcategoryOptions= c(sub3,sub4)}

}

1 个答案:

答案 0 :(得分:1)

library(shiny)
library(shinydashboard)

UI <- dashboardSidebar(
  sidebarMenu(
    menuItem("CSF2", tabName = "CSF2", icon = icon("bar-chart")),
    selectInput("category", "Select a category", c("1", "2")),
    selectInput("subcategory", "Select a subcategory",       
                c("sub1", "sub2", "sub3", "sub4"))
  )
)


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

  observe({
    updateSelectInput(
      session, "subcategory", "Select a subcategory",
      choices = switch(input$category,
        "1" = c("sub1", "sub2"),
        "2" = c("sub3", "sub4"))
    )
  })

}

shinyApp(UI, server)