Dependent SidebarMenus

Hi, I'm attempting the following:

Goal: Implement a dependent sideBarMenu that contains a dropdown list whose list choices depend on the item selection of another static sideBarMenu.

Issue: Menu item selection keeps reverting back to the first menu item.

I think this has to do something with the callbacks running in the background. Or maybe the structure of my code.

ui <- dashboardPage(
        dashboardHeader(title="Some Header"),
        dashboardSidebar(
          sidebarMenu(id="sbm"
            ,menuItem("Menu",tabName="m_1"
              ,menuSubItem("Menu Item 1", tabName="mi_1")
              ,menuSubItem("Menu Item 2", tabName="mi_2")
              ,menuSubItem("Menu Item 3", tabName="mi_3")
            )
          )
          ,sidebarMenuOutput("dependent_dropdown")
        ),
        dashboardBody(
          uiOutput("checkItemValue")
          ,uiOutput("checkListValue")
        )
)## end user function

server <- function(input, output){
            react_val <- reactiveValues()
            
            observeEvent(input$sbm,{
              react_val$selected_list <- paste0(input$sbm,"_Li_",1:6)
              react_val$initial_selection <- paste0(input$sbm,"_Li_",1:6)[1]
            })
            
            output$checkItemValue <- renderText({
              paste0("Menu Item ",gsub("mi_","",input$sbm)," selected")
            })
            
            output$checkListValue <- renderText({
              paste0("List Item ",input$dropdown_selection," selected")
            })            
            
            output$dependent_dropdown <- renderMenu({
              sidebarMenu(id="dropdown",
                          menuItem("Dependent Dropdown"
                            ,selectInput("dropdown_selection"
                              ,paste0("Menu Item ",gsub("mi_","",input$sbm)," list:")
                              ,choices=as.list(react_val$selected_list)
                              ,selected=react_val$initial_selection)
                          ))
            })
            
}## end server function

shinyApp(ui, server) 

Hey y'all. Solved it. Needed to use updateSelectInput on the server side with a plain ol' sideBarMenu on the user side. Here's the code with the desired functionality:


ui <- dashboardPage(
        dashboardHeader(title="Some Header"),
        dashboardSidebar(
          sidebarMenu(id="sbm"
            ,menuItem("Menu",tabName="m_1",icon = icon("th")
              ,menuSubItem("Menu Item 1", tabName="mi_1")
              ,menuSubItem("Menu Item 2", tabName="mi_2")
              ,menuSubItem("Menu Item 3", tabName="mi_3")
            )
          )
          ,sidebarMenu(id="dependent_dropdown",
              menuItem("Dependent Dropdown",icon=icon("database")
               ,selectInput("dropdown_selection"
                 ,"Menu Item List:"
                 ,choices=c()
                 ,selected=NULL)
            )
          )
        ),
        dashboardBody(
          uiOutput("checkItemValue")
          ,uiOutput("checkListValue")
        )
)## end user function

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

            observeEvent(input$sbm,{
              react_val$selected_list <- paste0(input$sbm,"_Li_",1:6)
              react_val$initial_selection <- paste0(input$sbm,"_Li_",1:6)[1]
              
              updateSelectInput(session,"dropdown_selection",
                                label = paste0("Menu Item ",gsub("mi_","",input$sbm)," List:"),
                                choices = react_val$selected_list,
                                selected = react_val$initial_selection)
            })

            output$checkItemValue <- renderText({
              paste0("Menu Item ",gsub("mi_","",input$sbm)," selected")
            })

            output$checkListValue <- renderText({
              paste0("List Item ",gsub(paste0(input$sbm,"_Li_"),"",input$dropdown_selection)," selected")
            })

}## end server function
1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

Yup, thanks. Marked solved.

Thanks,Tyler.

1 Like