Gaurav Bansal
Gaurav Bansal

Reputation: 5660

How to deploy R shinydashboard with menuSubItem not collapsed by default

Below is some simple sample code for shinydashboard with various subMenuItem objects under a menuItem. By default, when the app is deployed the subMenuItems are collapsed. Is there a way to set it so that they are not collapsed?

ui <- dashboardPage(
  dashboardHeader(title='Head'),
  dashboardSidebar(
    sidebarMenu(
      menuItem('Tabs', tabName='tabs',
        menuSubItem('Tab 1', tabName='tab1'),
        menuSubItem('Tab 2', tabName='tab2'),
        menuSubItem('Tab 3', tabName='tab3')
      )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName='tab1',
        h1("Tab 1")
      ),
      tabItem(tabName='tab2',
        h1("Tab 2")
      ),
      tabItem(tabName='tab3',
        h1("Tab 3")      
      )
    )
  )
)

server <- function(input, output, session) {}

shinyApp(ui, server)

Upvotes: 4

Views: 3399

Answers (2)

David A.
David A.

Reputation: 66

A feature that has likely been added since the OP: startExpanded. Set it = TRUE for any menu item with children you want expanded by default.

  dashboardSidebar(
    sidebarMenu(
      menuItem('Tabs', tabName='tabs', startExpanded = TRUE,
        menuSubItem('Tab 1', tabName='tab1'),
        menuSubItem('Tab 2', tabName='tab2'),
        menuSubItem('Tab 3', tabName='tab3')
      )
    )
  ),

Upvotes: 5

Xiongbing Jin
Xiongbing Jin

Reputation: 12087

You can use Javascript to change the default display style of the menu elements (by default they are display:none which needs to be changed to display:block). Add the following line after menuItem() (remember to add a comma also)

  tags$head(tags$script(HTML('$(document).ready(function() {$(".treeview-menu").css("display", "block");})')))

Upvotes: 6

Related Questions