Search code examples
javascriptrshinyshinydashboard

How to enable toggling between 3 menu items in shiny dashboard sidebar


I would like to make menu items in my shiny dashboard collapsible.

I found the post with similar question that had been answered some time ago:

solution to the problem with 2 div-s

However, the solution works only with 2 div-s. Is it possible to do similar thing with 3 menu items?

Attached reproducible code (posted by Martin Schmelzer)

library(shiny)
library(shinydashboard)

server <- function(input, output) { }
jsc <- '
$(document).ready(function () {
  $(".sidebar-menu").children("li").on("click", function() {
    $("#mult, #single").toggle();
  });
});
'

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard", titleWidth = 290),  
  dashboardSidebar(
    width = 290,
    sidebarMenu(
      menuItem(
        "Multiple Incident Analysis",
        tabName = "dashboard",
        icon = icon("th")),      
      div(id = "mult", splitLayout(cellWidths = c("44%", "31%", "25%"),
                  dateInput("datefrom", "Date From:", format = "mm/dd/yyyy", Sys.Date()-5),
                  textInput("datefrom_hour", "Hour",
                            value = "12:00"),
                  textInput("datefrom_noon","AM/PM", value = "AM")),      
      splitLayout(cellWidths = c("44%", "31%", "25%"),
                  dateInput("dateto", "Date To:", format = "mm/dd/yyyy"),
                  textInput("dateto_hour", "Hour",
                            value = "11:59"),
                  textInput("dateto_noon","AM/PM", value = "PM"))),
      menuItem("Single Analysis", 
               tabName = "widgets", 
               icon = icon("th")),
      div(id = "single", style="display: none;", numericInput("tckt", "Ticket Number : ", 12345,  width = 290)),
      submitButton("Submit", width = "290")
    )),  
  dashboardBody(
    tags$head(tags$script(jsc))
  ))

shinyApp(ui, server)

Solution

  • In shinydashboard, menuItem already does that for as many menu items as you want. Try this

    library(shiny)
    library(shinydashboard)
    
    server <- function(input, output) { }
    
    ui <- dashboardPage(
      dashboardHeader(title = "Dashboard", titleWidth = 290),  
      dashboardSidebar(
        width = 290,
        sidebarMenu(
          menuItem(
            "Multiple Incident Analysis",
            tabName = "dashboard",
            icon = icon("th"),      
          div(id = "mult", splitLayout(cellWidths = c("44%", "31%", "25%"),
                                       dateInput("datefrom", "Date From:", format = "mm/dd/yyyy", Sys.Date()-5),
                                       textInput("datefrom_hour", "Hour",
                                                 value = "12:00"),
                                       textInput("datefrom_noon","AM/PM", value = "AM")))), 
          menuItem(
            "Multiple Incident Analysis2",
            tabName = "dashboard2",
            icon = icon("th"),  
          div(id = "mult2", splitLayout(cellWidths = c("44%", "31%", "25%"),
                          dateInput("dateto", "Date To:", format = "mm/dd/yyyy"),
                          textInput("dateto_hour", "Hour",
                                    value = "11:59"),
                          textInput("dateto_noon","AM/PM", value = "PM")))),
          menuItem("Single Analysis", 
                   tabName = "widgets", 
                   icon = icon("th"),
          div(id = "single",  numericInput("tckt", "Ticket Number : ", 12345,  width = 290))),
          submitButton("Submit", width = "290")
        )),  
      dashboardBody()
    )
    
    shinyApp(ui, server)