Panels and cards
Use panels and cards to define areas of related content in your Shiny app.
Use panels and cards to define areas of related content.
Relevant Functions
-
absolutePanel
absolutePanel(..., top = NULL, left = NULL, right = NULL, bottom = NULL, width = NULL, height = NULL, draggable = FALSE, fixed = FALSE, cursor = c("auto", "move", "default", "inherit")) -
fixedPanel
fixedPanel(..., top = NULL, left = NULL, right = NULL, bottom = NULL, width = NULL, height = NULL, draggable = FALSE, cursor = c("auto", "move", "default", "inherit")) -
wellPanel
wellPanel(...) -
img
img(..., .noWS = NULL, .renderHook = NULL) -
card
card(..., full_screen = FALSE, height = NULL, max_height = NULL, min_height = NULL, fill = TRUE, class = NULL, wrapper = card_body, id = NULL)
Floating panel
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 125
## file: app.R
library(shiny)
library(bslib)
library(htmltools)
ui <- page_fillable(
absolutePanel(
wellPanel(
titlePanel("Draggable panel"),
"Move this panel anywhere you want.",
),
width = "300px",
right = "50px",
top = "50px",
draggable = TRUE,
),
style = css(
background_image = "url(https://unsplash.com/photos/XKXGghL7GQc/download?force=true&w=1920)",
background_repeat = "no-repeat",
background_size = "cover",
background_position = "center bottom",
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
library(htmltools)
ui <- page_fillable(
absolutePanel(
wellPanel(
titlePanel("Draggable panel"),
"Move this panel anywhere you want.",
),
width = "300px",
right = "50px",
top = "50px",
draggable = TRUE,
),
style = css(
background_image = "url(https://unsplash.com/photos/XKXGghL7GQc/download?force=true&w=1920)",
background_repeat = "no-repeat",
background_size = "cover",
background_position = "center bottom",
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)Follow these steps to create an app that has a panel floating over a main image.
First, to create the floating panel:
Use the
absolutePanel()to create a floating element.The initial unamed argument(s) passed into
absolutePanel()will be the contents for theabsolutePanel().- The example shown uses a
wellPanel()to put a nice visual bounding box so you know what can be moved.
- The example shown uses a
Position the
absolutePanel()using thetop,bottom,left, and/orrightparameters. Set the size of the panel using theheightand/orwidthparameters.If you want the panel to be draggable, set the
draggableparameter toTrue.
In the example above, we used CSS to add a scaling background image to the page. You can also use img() to create this effect:
Pass
img()to any Shiny UI page method (e.g.,page_fillable()).img()creates an image.Pass the path or URL of your desired image to
img()’ssrcparameter. Set additional parameters to control the appearance of the image (e.g.,widthandheight).
See also: fixedPanel(). fixedPanel() is equivalent to calling ui.panel_absolute() with fixed=True (i.e., the panel does not scroll with the rest of the page).
Content divided by cards
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 320
## file: app.R
library(shiny)
library(bslib)
ui <- page_fillable(
layout_columns(
card(
card_header("Card 1 header"),
p("Card 1 body"),
sliderInput("slider", "Slider", 0, 10, 5),
),
card(
card_header("Card 2 header"),
p("Card 2 body"),
textInput("text", "Add text", ""),
),
)
)
server <- function(input, output) {
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
library(shiny)
library(bslib)
ui <- page_fillable(
layout_columns(
card(
card_header("Card 1 header"),
p("Card 1 body"),
sliderInput("slider", "Slider", 0, 10, 5),
),
card(
card_header("Card 2 header"),
p("Card 2 body"),
textInput("text", "Add text", ""),
),
)
)
server <- function(input, output) {
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)Cards are general purpose containers used to group related UI elements together with a border and optional padding.
Follow these steps to create an app with content separated into cards:
Add
card()inside any Shiny UI page method (e.g.,page_fillable(),layout_columns()) to create a card. Add additional cards by adding additional calls tocard().Define the contents of each card. Inside
card(), pass elements that you would like to appear inside the card (e.g., inputs, text). You can also usecard_header()andcard_footer()to create a card header and footer.Control the appearance and functionality of each card by passing additional arguments to
card(). For example, you can setfull_screentoTRUEto allow the user to expand the card to fullscreen, or control the height of the card with theheightparameter.