Capture screenshots of entire pages or parts of pages in Shiny apps
Demo
Β·
by Dean Attali
{shinyscreenshot} allows you to capture screenshots of entire pages or parts of pages in Shiny apps. Can be used to capture the current state of a Shiny app, including interactive widgets (such as plotly, timevis, maps, etc). The captured image is automatically downloaded as a PNG image.
Need Shiny help? Iβm available for
consulting.
If you find {shinyscreenshot}
useful, please consider supporting my
work to unlock rewards! β€
- How to use
- Screenshot button
- Features
- Installation
- Motivation
- Browser support and limitations
- Similar packages
- Sponsors π
Using {shinyscreenshot} is as easy as it gets. When you want to take a screenshot, simply call screenshot()
and a full-page screenshot will be taken and downloaded as a PNG image. Try it for yourself!
It's so simple that an example isn't needed, but here's one anyway:
library(shiny)
library(shinyscreenshot)
ui <- fluidPage(
textInput("text", "Enter some text", "test"),
actionButton("go", "Take a screenshot")
)
server <- function(input, output) {
observeEvent(input$go, {
screenshot()
})
}
shinyApp(ui, server)
The screenshot()
function can be called any time inside the server portion of a Shiny app. A very common case is to take a screenshot after clicking a button. That case is so common that there's a function for it: screenshotButton()
. It accepts all the same parameters as screenshot()
, but instead of calling it in the server, you call it in the UI.
screenshotButton()
creates a button that, when clicked, will take a screenshot.
-
Region: By default, the entire page is captured. If you'd like to capture a specific part of the screen, you can use the
selector
parameter to specify a CSS selector. For example, if you have a plot with IDmyplot
then you can usescreenshot(selector="#myplot")
. -
Scale: The image file will have the same height and width as what is visible in the browser. Using
screenshot(scale=2)
will result in an image that's twice the height and width (and also a larger file size). -
Timer: Usually you want the screenshot to be taken immediately, but sometimes you may want to tell Shiny to take a screenshot in, for example, 3 seconds from now. That can be done using
screenshot(timer=3)
. -
File name: You can choose the name of the downloaded file using the
filename
parameter. -
Module support: As an alternative to the
selector
argument, you can also use theid
argument. For example, instead of usingscreenshot(selector="#myplot")
, you could usescreenshot(id="myplot")
. The advantage with using an ID directly is that theid
parameter is module-aware, so even if you're taking a screenshot inside a Shiny module, you don't need to worry about namespacing.
To install the stable CRAN version:
install.packages("shinyscreenshot")
To install the latest development version from GitHub:
install.packages("remotes")
remotes::install_github("daattali/shinyscreenshot")
For years, I saw people asking online how can they take screenshots of the current state of a Shiny app. This question comes up especially with interactive outputs (plotly, timevis, maps, DT, etc). Some of these don't allow any way to save the current state as an image, and a few do have a "Save as image" option, but they only save the base/initial state of the output, rather than the current state after receiving user interaction.
After seeing many people asking about this, one day my R-friend Eric Nantz asked about it as well, which gave me the motivation to come up with a solution.
The screenshots are powered by the 'html2canvas' JavaScript library. They do not always produce perfect screenshots, please refer to 'html2canvas' for more information about the limitations.
The JavaScript libraries used in this package may not be supported by all browsers. {shinyscreenshot} should work on Chrome, Firefox, Edge, Chrome on Android, Safari on iPhone (and probably more that I haven't tested). It does not work in Internet Explorer.
As mentioned above, the libraries used by {shinyscreenshot} do have limitations and may not always work. There are two other packages that came out recently that also provide screenshot functionality which you may try and compare: snapper by Jonathan Sidi and capture by dreamRs.
There are no sponsors yet