Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Created August 26, 2020 23:30
Show Gist options
  • Select an option

  • Save jimbrig/1bb5659bb75ed19cd1c1d3915992a4fa to your computer and use it in GitHub Desktop.

Select an option

Save jimbrig/1bb5659bb75ed19cd1c1d3915992a4fa to your computer and use it in GitHub Desktop.
Shiny Dashboard Collapsible Sidebar
# UI ------------------------------------------------------------
#' import shiny
#' import shinyjs
collapsible_sidebar_ui <- function(width = 230){
tagList(
inlineCSS(paste0(".main-header > .navbar {margin-left: ", width, "px !important}")),
inlineCSS(paste0(".main-header .logo {width: ", width, "px !important}"))
)
}
# Server --------------------------------------------------------
collapsible_sidebar <- function(id = "sidebar_collapsed") {
# Custom CSS styles -------------------------------------------------------
# Sidebar collapsed by default
addClass(selector = "body", class = "sidebar-collapse")
# Sidebar shows icons even when collapsed
addClass(selector = "body", class = "sidebar-mini")
# JavaScript events -------------------------------------------------------
# Sidebar expands to default width on mouse hover
onevent(event = "mouseenter", id = id, expr = removeClass(selector = "body", class = "sidebar-collapse"))
# Sidebar contracts after mouse hover stops
onevent(event = "mouseleave", id = id, expr = addClass(selector = "body", class = "sidebar-collapse"))
}
# Server ------------------------------------------------------------------
server <- function(input, output, session) {
...
# Customisation -----------------------------------------------------------
collapsible_sidebar(id = "sidebar_collapsed")
...
}
# Sidebar -----------------------------------------------------------------
sidebar = dashboardSidebar(
sidebarMenu(
id = "sidebar",
# Allowing the shinyjs package to be used
useShinyjs(),
# Allowing for collapsible sidebar with fixed width logo
collapsible_sidebar_ui(width = 230),
# .....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment