A sidebar layout creates a sidebar, typically used for inputs, and a large main area, typically used for outputs.
Relevant Functions
page_sidebar
page_sidebar(..., sidebar = NULL, title = NULL, fillable = TRUE, fillable_mobile = FALSE, theme = bs_theme(), window_title = NA, lang = NULL)
sidebar
sidebar(..., width = 250, position = c("left", "right"), open = NULL, id = NULL, title = NULL, bg = NULL, fg = NULL, class = NULL, max_height_mobile = NULL, gap = NULL, padding = NULL)
layout_sidebar
layout_sidebar(..., sidebar = NULL, fillable = TRUE, fill = TRUE, bg = NULL, fg = NULL, border = NULL, border_radius = NULL, border_color = NULL, padding = NULL, gap = NULL, height = NULL)
No matching items
Sidebar on the left
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 125
## file: app.R
library(shiny)
library(bslib)
ui <- page_sidebar(
sidebar = sidebar("Sidebar", bg="#f8f8f8"),
"Main content"
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Follow these steps to add a left-side sidebar to your app:
Use page_sidebar()
, from the bslib package, to create a layout with a sidebar.
Use page_sidebar()
’s sidebar
argument and sidebar()
to create a sidebar.
Supply components (inputs components, text, etc.) to sidebar()
to define the contents of the sidebar.
Supply additional components to page_sidebar()
to define the contents of the main content area.
Sidebar on the right
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 125
## file: app.R
library(shiny)
library(bslib)
ui <- page_sidebar(
sidebar = sidebar("Sidebar", bg="#f8f8f8", position = "right"),
"Main content"
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Follow these steps to add a right-side sidebar to your app:
Use page_sidebar()
, from the bslib package, to create a layout with a sidebar.
Use page_sidebar()
’s sidebar
argument and sidebar()
to create a sidebar.
Set sidebar()
’s position
argument to "right"
to place the sidebar on the right.
Supply components (inputs components, text, etc.) to sidebar()
to define the contents of the sidebar.
Supply additional components to page_sidebar()
to define the contents of the main content area.
Sidebar within a card
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 200
## file: app.R
library(shiny)
library(bslib)
ui <- page_fillable(
card(
card_header("Sidebar within a card"),
layout_sidebar(
sidebar = sidebar("Sidebar", bg="#f8f8f8"),
"Main content"
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Follow these steps to add a sidebar within a card to your app:
Add card()
to the UI of your app to create a card.
Pass layout_sidebar()
to card()
to define a sidebar layout within the card.
Add a sidebar()
and additional elements to layout_sidebar()
to define the sidebar and main content as usual.
Add inputs or other components as desired to sidebar()
to define the sidebar’s contents.
Collapsed sidebar
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 125
## file: app.R
library(shiny)
library(bslib)
ui <- page_sidebar(
sidebar = sidebar("Sidebar", bg="#f8f8f8", open = "closed"),
"Main content"
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
sidebar()
has an open
argument that defines whether the sidebar appears open or closed when the app launches. To create a sidebar that is initially closed, set the open
parameter to "closed"
.
The other options for open
are:
"desktop"
: The default. The sidebar starts open on a desktop screen and closed on mobile.
"open"
: The sidebar starts open and can be closed.
"always"
: The sidebar is always open and cannot be closed.