When a Shiny output (such as a plot, table, map, etc.) is recalculating, it remains visible but gets greyed out. Using {shinycssloaders}, you can add a loading animation (“spinner”) to outputs instead of greying them out. By wrapping a Shiny output in withSpinner()
, a spinner will automatically appear while the output is recalculating.
You can choose from one of 8 built-in animation types, and customize the colour/size. You can also use your own image instead of the built-in animations. See the demo Shiny app online for examples.
For interactive examples and to see some of the features, check out the demo app.
Below is a simple example of what {shinycssloaders} looks like:
Simply wrap a Shiny output in a call to withSpinner()
. If you have %>%
loaded, you can use it, for example plotOutput("myplot") %>% withSpinner()
.
Basic usage:
library(shiny)
ui <- fluidPage(
actionButton("go", "Go"),
shinycssloaders::withSpinner(
plotOutput("plot")
)
)
server <- function(input, output) {
output$plot <- renderPlot({
input$go
Sys.sleep(1.5)
plot(runif(10))
})
}
shinyApp(ui, server)
To install the stable CRAN version:
install.packages("shinycssloaders")
To install the latest development version from GitHub:
install.packages("remotes")
remotes::install_github("daattali/shinycssloaders")
You can use the type
parameter to choose one of the 8 built-in animations, the color
parameter to change the spinner’s colour, and size
to make the spinner smaller or larger (2 will make the spinner twice as large). For example, withSpinner(plotOutput("myplot"), type = 5, color = "#0dc5c1", size = 2)
.
If you want all the spinners in your app to have a certain type/size/colour, instead of specifying them in each call to withSpinner()
, you can set them globally using the spinner.type
, spinner.color
, spinner.size
R options. For example, setting options(spinner.color="#0dc5c1")
will cause all your spinners to use that colour.
If you don’t want to use any of the built-in spinners, you can also provide your own image (either a still image or a GIF) to use instead, using the image
parameter.
The spinner attempts to automatically figure out the height of the output it replaces, and to vertically center itself. For some outputs (such as tables), the height is unknown, so the spinner will assume the output is 400px tall. If your output is expected to be significantly smaller or larger, you can use the proxy.height
parameter to adjust this.
By default, the out-dated output gets hidden while the spinner is showing. You can change this behaviour to have the spinner appear on top of the old output using the hide.ui = FALSE
parameter.
Spinner types 2 and 3 require you to specify a background colour. It’s recommended to use a colour that matches the background colour of the output’s container, so that the spinner will “blend in”.
The 8 built-in animations are taken from https://projects.lukehaas.me/css-loaders/.
The package was originally created by Andrew Sali.