Reputation: 11
new to shiny here. I am creating a dashboard and have different categories and subcategories. what I am trying to do is as follows:
When the screen loads I would like all the categories and subcategories to be available for selection, but if I select category '1', then only 'sub1' and 'sub2' should be available for selection. So like it is being filtered by the category. How would I implement this in the server file in shiny? a brief example of my code is as follows: (server is sudo code)
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)}
}
Upvotes: 0
Views: 4120
Reputation: 12849
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)
Upvotes: 1