17

I used shinydashboard to create my app. I would like to hide the sidedar in default on desktop environment (e.g. windows), but not to disable it. On the mobile device, the sidebar is hide in default. I think I need to change the css class, but don't know how to do it.

Thanks for any suggestions.

This is my playing codes:

library(shiny)

library(shinydashboard)
ui <- shinyUI(dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody()
))

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

shinyApp(ui = ui, server = server)
Bangyou
  • 9,462
  • 16
  • 62
  • 94

4 Answers4

40

if you do a ?dashboardSidebar you probably see the usage, like this

dashboardSidebar(..., disable = FALSE, width = NULL, collapsed = FALSE)

So this should work

sidebar <- dashboardSidebar(
  collapsed = TRUE,
  sidebarMenu()
)

i'm not sure if this depends on your shinydashboard version but you could check/change that as well.

Willem Bressers
  • 2,241
  • 2
  • 10
  • 3
24

This is very similar to my answer from another SO thread: "disabling/enabling sidebar from server side"

Here's code that can do what you want by hiding the sidebar when the app starts (using the package shinyjs)

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- shinyUI(dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs()
  )
))

server <- shinyServer(function(input, output, session) {
  addClass(selector = "body", class = "sidebar-collapse")
})

shinyApp(ui = ui, server = server)
Community
  • 1
  • 1
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • Thanks. I find your answer, but Rstudio give me a warning message (argument "id" is missing and with no default). So I guess a id should be passed into addClass funcion. – Bangyou Feb 03 '16 at 09:43
  • Nope, you shouldn't need to pass an id argument. If you copy this exact code you'll see that it just works and there's no error. Perhaps you're doing something else weird – DeanAttali Feb 03 '16 at 18:03
  • It do work for me. Just see an warning message in RStudio. – Bangyou Feb 03 '16 at 20:27
  • If the exact code above is giving you a warning, maybe you have an old version of shinyjs or shiny. It should run without any problems at all – DeanAttali Feb 03 '16 at 21:16
  • I've posted a follow up question about this linked with mouse hover here: https://stackoverflow.com/questions/44899706/display-sidebar-menu-on-hover-against-left-screen-wall – Mark Jul 04 '17 at 09:26
  • This is how we can `un-hide` the sidebar programatically. Thanks Dean! `shinyjs::runjs("$('.sidebar-toggle').click();")` – kraggle Jul 19 '23 at 18:39
2

Add

dashboardSidebar(collapsed = TRUE)

instead of

dashboardSidebar()

in UI page.

1

Or simply

ui <- shinyUI(dashboardPage(
  dashboardHeader(),
  dashboardSidebar(collapsed = TRUE),
  dashboardBody()
))
Ilona
  • 446
  • 4
  • 12