I am facing a challenge in manually expanding a submenu within a sidebar on shiny dashboard. The function updateTabItems
does not seem to work with nested menus, only with normal menus.
For example, when I click on 'Switch tab', it switches the menus but does not expand the first menu that contains a submenu. It appears to only select the submenu without expanding the tree structure.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Simple tabs"),
dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"),
menuSubItem("Sub Menu 1",icon = icon("folder-open"), tabName = "subMenu1")
),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
),
actionButton('switchtab', 'Switch tab')
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content")
),
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$switchtab, {
newtab <- switch(input$tabs,
"subMenu1" = "widgets",
"widgets" = "subMenu1"
)
updateTabItems(session, "tabs", newtab)
})
}
shinyApp(ui, server)
}
I am looking for a way to manually expand the tree, select both the menu and the submenu. Any suggestions or insights would be greatly appreciated. Thank you.
Update:
A working solution code can be found at Shiny expanding submenu items manually