Collapses

Description

Collapse panels allow you to reduce clutter in your Shiny app by making panels of information that open and close with a user's click. Any type of content can go in a collapse panel. Standard Bootstrap styling options are available.

Details

Collapses are designed to mimic list("tabsetPanel") in their implementation. Start with bsCollapse to create a panel group, then fill it with panels using bsCollapsePanel.

bsCollapse acts as an input, so you can retrieve which panels are open from the input object passed to the function in list("shinyServer").

updateCollapse can be used within your server logic to open/close collapse panels or to change their style.

Components

bsCollapse

bsCollapse is used in your UI to create a collapse panel group. Use list("bsCollapsePanel") to populate this object with panels.

Usage

bsCollapse(..., id = NULL, multiple = FALSE, open = NULL)

Parameters

id
Optional You can use input$id in your Server logic to determine which panels are open, and list("updateCollapse") to open/close panels.
multiple
Can more than one panel be open at a time? Defaults to FALSE.
open
The value, (or if none was supplied, the title) of the panel(s) you want open on load.
...
list("bsCollapsePanel") elements to include in the Collapse.

bsCollapsePanel

bsCollapsePanel creates individual panels within a list("bsCollapse") object.

Usage

bsCollapsePanel(title, ..., value = title, style = NULL)

Parameters

title
The title to display at the top of the panel.
value
Optional The value to return when this panel is open. Defaults to title.
style
Optional A Bootstrap style to apply to the panel. (primary, danger, warning, info, or success)
...
UI elements to include within the panel.

updateCollapse

updateCollapse is used within the Server logic of your Shiny app to modify a Collapse after load.

Usage

updateCollapse(session, id, open = NULL, close = NULL, style = NULL)

Parameters

session
The session object passed to function given to shinyServer.
id
The id of the Collapse object you want to change.
open
A vector of value (or title if no value was provided) values identifying the panels you want to open.
close
A vector of value (or title if no value was provided) values identifying the panels you want to close.
style
A named list of Bootstrap styles (primary, danger, info, warning, success, or default). The names should correspond to the value (or title if no value was provided) of the list("bsCollapsePanel") you want to change.

Changes

style is a new option that wasn't available in previous versions of shinyBS.

Example



library(shiny)
library(shinyBS)

shinyApp(
 ui =
 fluidPage(
   sidebarLayout(
     sidebarPanel(HTML("This button will open Panel 1 using updateCollapse."),
                  actionButton("p1Button", "Push Me!"),
                  selectInput("styleSelect", "Select style for Panel 1",
                   c("default", "primary", "danger", "warning", "info", "success"))
     ),
     mainPanel(
       bsCollapse(id = "collapseExample", open = "Panel 2",
                  bsCollapsePanel("Panel 1", "This is a panel with just text ",
                   "and has the default style. You can change the style in ",
                   "the sidebar.", style = "info"),
                  bsCollapsePanel("Panel 2", "This panel has a generic plot. ",
                   "and a 'success' style.", plotOutput("genericPlot"), style = "success")
       )
     )
   )
 ),
 server =
 function(input, output, session) {
   output$genericPlot <- renderPlot(plot(rnorm(100)))
   observeEvent(input$p1Button, ({
     updateCollapse(session, "collapseExample", open = "Panel 1")
   }))
   observeEvent(input$styleSelect, ({
     updateCollapse(session, "collapseExample", style = list("Panel 1" = input$styleSelect))
   }))
 }
)