问题
I'm trying to manually expand a submenu in a sidebar in shiny dashboard. The updateTabItems function allows me to go to the tab, but it does not expand the menu with it.
I am looking for a reproducible answer to the question here:how to manually expand a submenu in a shiny dashboard side bar which includes a nice working example and answers which are however not implemented.
回答1:
Here is a working code based on the referred answer. Please note that in this case the JavaScript uses the tabName
as a selector to add the active
class to the tree menu.
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "Simple tabs"),
dashboardSidebar(
sidebarMenu(
id = "tabs",
actionButton('switchtab', 'Switch tab'),
menuItem("Widgets", tabName = "widgets", icon = icon("th")),
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"),
menuSubItem("Sub Menu 1",icon = icon("folder-open"), tabName = "subMenu1")
)
)
),
dashboardBody(
useShinyjs(),
extendShinyjs(text = "shinyjs.activateTab = function(name){
setTimeout(function(){
$('a[href$=' + '\"#shiny-tab-' + name + '\"' + ']').closest('li').addClass('active')
}, 200);
}"
),
tabItems(
tabItem(tabName = "subMenu1",
h2("Dashboard tab / Sub menu 1 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)
if (newtab == "subMenu1")
js$activateTab("dashboard")
})
}
shinyApp(ui, server)
来源:https://stackoverflow.com/questions/40847838/shiny-expanding-submenu-items-manually