Hide sidebar in default in shinydashboard
Asked Answered
H

4

18

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)
Hailstorm answered 3/2, 2016 at 6:33 Comment(2)
@NicE Thanks. I only want to hide the sidebar, not to disable it.Hailstorm
dashboardSidebar also now has an argument collapsed. If True, the sidebar will be collapsed by default.Starvation
D
25

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)
Dagmar answered 3/2, 2016 at 7:27 Comment(6)
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.Hailstorm
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 weirdDagmar
It do work for me. Just see an warning message in RStudio.Hailstorm
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 allDagmar
I've posted a follow up question about this linked with mouse hover here: #44900206Paronym
This is how we can un-hide the sidebar programatically. Thanks Dean! shinyjs::runjs("$('.sidebar-toggle').click();")Crespo
R
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.

Reproval answered 19/12, 2017 at 10:43 Comment(1)
This works wonderfully! Is there also a way to disable the sidebar toggle?Softener
D
25

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)
Dagmar answered 3/2, 2016 at 7:27 Comment(6)
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.Hailstorm
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 weirdDagmar
It do work for me. Just see an warning message in RStudio.Hailstorm
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 allDagmar
I've posted a follow up question about this linked with mouse hover here: #44900206Paronym
This is how we can un-hide the sidebar programatically. Thanks Dean! shinyjs::runjs("$('.sidebar-toggle').click();")Crespo
I
2

Add

dashboardSidebar(collapsed = TRUE)

instead of

dashboardSidebar()

in UI page.

Ilse answered 8/7, 2021 at 2:20 Comment(0)
N
1

Or simply

ui <- shinyUI(dashboardPage(
  dashboardHeader(),
  dashboardSidebar(collapsed = TRUE),
  dashboardBody()
))
Nonet answered 2/7, 2021 at 10:27 Comment(1)
Thanks iLona. Would you know the command to un-collapse the sidebar when called?Crespo

© 2022 - 2024 — McMap. All rights reserved.