Shiny dashboard preselect menuSubItem when clicking menuItem

Viewed 269

When clicking on a menu item in the side bar I would like it to not only expand and show the menu sub items but also preselect the first one and show the corresponding tab item UI.

I know it is possible to define one item as selected and it will show when I start the app. To me this is confusing behaviour because the corresponding menu item does not appear as "selected" in the sidebar. My requirement goes further anyway since I want to preselect a menu sub item every time I click on a menu item.

library(shinydashboard)
library(shiny)


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Table1" , tabname = "my_table1", icon = icon("table"),startExpanded = F,
               menuSubItem("sub menu1",tabName = "subMenu1"),
               menuSubItem("sub menu2",tabName = "subMenu2")
               ),
      menuItem("Table2" , tabname = "my_table2", icon = icon("table"),startExpanded = F,
               menuSubItem("sub menu3",tabName = "subMenu3"),
               menuSubItem("sub menu4",tabName = "subMenu4", selected = T)
               )
      )),
  
  dashboardBody(
    tabItems(
      tabItem(tabName = "my_table1",
              h2("First Table")
      ),
      tabItem(tabName = "my_table2",
              h2("Second Table")
      ),
      tabItem(tabName = "subMenu1",
              h2("First tab")
      ),
      tabItem(tabName = "subMenu2",
              h2("Second tab")
      ),
      tabItem(tabName = "subMenu3", 
              h2("Third tab")
      ),
      tabItem(tabName = "subMenu4", 
              h2("Fourth tab")
      )
    )))

server <- function(input, output) {
}
shinyApp(ui, server)
1 Answers

Your sidebarMenu needs an id and your server function needs the session argument, so you can use:

updateTabItems(session, inputId="sidebarID", selected="subMenu1")

Please check the following:

library(shinydashboard)
library(shiny)


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebarID",
      menuItem("Table1" , tabname = "my_table1", icon = icon("table"), startExpanded = TRUE,
               menuSubItem("sub menu1",tabName = "subMenu1", selected = TRUE),
               menuSubItem("sub menu2",tabName = "subMenu2")
      ),
      menuItem("Table2" , tabname = "my_table2", icon = icon("table"), startExpanded = FALSE,
               menuSubItem("sub menu3",tabName = "subMenu3"),
               menuSubItem("sub menu4",tabName = "subMenu4")
      )
    )),
  
  dashboardBody(
    tabItems(
      tabItem(tabName = "my_table1",
              h2("First Table")
      ),
      tabItem(tabName = "my_table2",
              h2("Second Table")
      ),
      tabItem(tabName = "subMenu1",
              h2("First tab")
      ),
      tabItem(tabName = "subMenu2",
              h2("Second tab")
      ),
      tabItem(tabName = "subMenu3", 
              h2("Third tab")
      ),
      tabItem(tabName = "subMenu4", 
              h2("Fourth tab")
      )
    )))

server <- function(input, output, session) {
  observeEvent(input$sidebarItemExpanded, {
    cat(paste("menuItem() currently expanded:", input$sidebarItemExpanded, "\n"))
    if(input$sidebarItemExpanded == "Table1"){
      updateTabItems(session, inputId="sidebarID", selected="subMenu1")
    } else if(input$sidebarItemExpanded == "Table2"){
      updateTabItems(session, inputId="sidebarID", selected="subMenu3")
    }
  })
  
  observe({
    cat(paste("tabItem() currently selected:", input$sidebarID, "\n"))
  })
}
shinyApp(ui, server)

Furthermore please see the related docs.

Related