Created
August 26, 2020 23:30
-
-
Save jimbrig/1bb5659bb75ed19cd1c1d3915992a4fa to your computer and use it in GitHub Desktop.
Shiny Dashboard Collapsible Sidebar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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")) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Server ------------------------------------------------------------------ | |
| server <- function(input, output, session) { | |
| ... | |
| # Customisation ----------------------------------------------------------- | |
| collapsible_sidebar(id = "sidebar_collapsed") | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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