Title: | Take Screenshots of Web Pages |
---|---|
Description: | Takes screenshots of web pages, including Shiny applications and R Markdown documents. 'webshot2' uses headless Chrome or Chromium as the browser back-end. |
Authors: | Winston Chang [aut, cre], Barret Schloerke [ctb] , Posit Software, PBC [cph, fnd] |
Maintainer: | Winston Chang <[email protected]> |
License: | GPL-2 |
Version: | 0.1.1.9000 |
Built: | 2024-11-20 03:34:24 UTC |
Source: | https://github.com/rstudio/webshot2 |
appshot
performs a webshot
using two different methods
depending upon the object provided. If a 'character' is provided (pointing to
an app.R file or app directory) an isolated background R process is launched
to run the Shiny application. The current R process then captures the
webshot
. When a Shiny application object is supplied to
appshot
, it is reversed: the Shiny application runs in the current R
process and an isolated background R process is launched to capture a
webshot
. The reason it is reversed in the second case has to do
with scoping: although it would be preferable to run the Shiny application in
a background process and call webshot
from the current process, with
Shiny application objects, there are potential scoping errors when run this
way.
appshot( app, file = "webshot.png", ..., port = getOption("shiny.port"), envvars = NULL ) ## S3 method for class 'character' appshot( app, file = "webshot.png", ..., port = getOption("shiny.port"), envvars = NULL ) ## S3 method for class 'shiny.appobj' appshot( app, file = "webshot.png", ..., port = getOption("shiny.port"), envvars = NULL, webshot_timeout = 60 )
appshot( app, file = "webshot.png", ..., port = getOption("shiny.port"), envvars = NULL ) ## S3 method for class 'character' appshot( app, file = "webshot.png", ..., port = getOption("shiny.port"), envvars = NULL ) ## S3 method for class 'shiny.appobj' appshot( app, file = "webshot.png", ..., port = getOption("shiny.port"), envvars = NULL, webshot_timeout = 60 )
app |
A Shiny app object, or a string naming an app directory. |
file |
A vector of names of output files. Should end with an image file
type ( |
... |
Other arguments to pass on to |
port |
Port that Shiny will listen on. |
envvars |
A named character vector or named list of environment variables and values to set for the Shiny app's R process. These will be unset after the process exits. This can be used to pass configuration information to a Shiny app. |
webshot_timeout |
The maximum number of seconds the phantom application
is allowed to run before killing the process. If a delay argument is
supplied (in |
Invisibly returns the normalized path to all screenshots taken. The character vector will have a class of '"webshot"'.
if (interactive()) { appdir <- system.file("examples", "01_hello", package="shiny") # With a Shiny directory appshot(appdir, "01_hello.png") # With a Shiny App object shinyapp <- shiny::shinyAppDir(appdir) appshot(shinyapp, "01_hello_app.png") }
if (interactive()) { appdir <- system.file("examples", "01_hello", package="shiny") # With a Shiny directory appshot(appdir, "01_hello.png") # With a Shiny App object shinyapp <- shiny::shinyAppDir(appdir) appshot(shinyapp, "01_hello_app.png") }
This does not change size of the image in pixels, nor does it affect appearance – it is lossless compression. This requires GraphicsMagick (recommended) or ImageMagick to be installed.
resize(filename, geometry)
resize(filename, geometry)
filename |
Character vector containing the path of images to resize. |
geometry |
Scaling specification. Can be a percent, as in |
The 'filename' supplied but with a class value of '"webshot"'.
if (interactive()) { # Can be chained with webshot() or appshot() webshot("https://www.r-project.org/", "r-small-1.png") %>% resize("75%") # Generate image that is 400 pixels wide webshot("https://www.r-project.org/", "r-small-2.png") %>% resize("400x") }
if (interactive()) { # Can be chained with webshot() or appshot() webshot("https://www.r-project.org/", "r-small-1.png") %>% resize("75%") # Generate image that is 400 pixels wide webshot("https://www.r-project.org/", "r-small-2.png") %>% resize("400x") }
This function can handle both static Rmd documents and Rmd documents with
runtime: shiny
.
rmdshot( doc, file = "webshot.png", ..., delay = NULL, rmd_args = list(), port = getOption("shiny.port"), envvars = NULL )
rmdshot( doc, file = "webshot.png", ..., delay = NULL, rmd_args = list(), port = getOption("shiny.port"), envvars = NULL )
doc |
The path to a Rmd document. |
file |
A vector of names of output files. Should end with an image file
type ( |
... |
Other arguments to pass on to |
delay |
Time to wait before taking screenshot, in seconds. Sometimes a longer delay is needed for all assets to display properly. If NULL (the default), then it will use 0.2 seconds for static Rmd documents, and 3 seconds for Rmd documents with runtime:shiny. |
rmd_args |
A list of additional arguments to pass to either
|
port |
Port that Shiny will listen on. |
envvars |
A named character vector or named list of environment variables and values to set for the Shiny app's R process. These will be unset after the process exits. This can be used to pass configuration information to a Shiny app. |
Invisibly returns the normalized path to all screenshots taken. The character vector will have a class of '"webshot"'.
if (interactive()) { # R Markdown file input_file <- system.file("examples/knitr-minimal.Rmd", package = "knitr") rmdshot(input_file, "minimal_rmd.png") # Shiny R Markdown file input_file <- system.file("examples/shiny.Rmd", package = "webshot") rmdshot(input_file, "shiny_rmd.png", delay = 5) }
if (interactive()) { # R Markdown file input_file <- system.file("examples/knitr-minimal.Rmd", package = "knitr") rmdshot(input_file, "minimal_rmd.png") # Shiny R Markdown file input_file <- system.file("examples/shiny.Rmd", package = "webshot") rmdshot(input_file, "shiny_rmd.png", delay = 5) }
This does not change size of the image in pixels, nor does it affect
appearance – it is lossless compression. This requires the program
optipng
to be installed.
shrink(filename)
shrink(filename)
filename |
Character vector containing the path of images to resize. Must be PNG files. |
If other operations like resizing are performed, shrinking should occur as the last step. Otherwise, if the resizing happens after file shrinking, it will be as if the shrinking didn't happen at all.
The 'filename' supplied but with a class value of '"webshot"'.
if (interactive()) { webshot("https://www.r-project.org/", "r-shrink.png") %>% shrink() }
if (interactive()) { webshot("https://www.r-project.org/", "r-shrink.png") %>% shrink() }
Take a screenshot of a URL
webshot( url = NULL, file = "webshot.png", vwidth = 992, vheight = 744, selector = NULL, cliprect = NULL, expand = NULL, delay = 0.2, zoom = 1, useragent = NULL, max_concurrent = getOption("webshot.concurrent", default = 6), quiet = getOption("webshot.quiet", default = FALSE) )
webshot( url = NULL, file = "webshot.png", vwidth = 992, vheight = 744, selector = NULL, cliprect = NULL, expand = NULL, delay = 0.2, zoom = 1, useragent = NULL, max_concurrent = getOption("webshot.concurrent", default = 6), quiet = getOption("webshot.quiet", default = FALSE) )
url |
A vector of URLs to visit. If multiple URLs are provided, it will load and take screenshots of those web pages in parallel. |
file |
A vector of names of output files. Should end with an image file
type ( |
vwidth |
Viewport width. This is the width of the browser "window". |
vheight |
Viewport height This is the height of the browser "window". |
selector |
One or more CSS selectors specifying a DOM element to set the
clipping rectangle to. The screenshot will contain these DOM elements. For
a given selector, if it has more than one match, all matching elements will
be used. This option is not compatible with |
cliprect |
Clipping rectangle. If |
expand |
A numeric vector specifying how many pixels to expand the
clipping rectangle by. If one number, the rectangle will be expanded by
that many pixels on all sides. If four numbers, they specify the top,
right, bottom, and left, in that order. When taking screenshots of multiple
URLs, this parameter can also be a list with same length as |
delay |
Time to wait before taking screenshot, in seconds. Sometimes a longer delay is needed for all assets to display properly. |
zoom |
A number specifying the zoom factor. A zoom factor of 2 will result in twice as many pixels vertically and horizontally. Note that using 2 is not exactly the same as taking a screenshot on a HiDPI (Retina) device: it is like increasing the zoom to 200 doubling the height and width of the browser window. This differs from using a HiDPI device because some web pages load different, higher-resolution images when they know they will be displayed on a HiDPI device (but using zoom will not report that there is a HiDPI device). |
useragent |
The User-Agent header used to request the URL. |
max_concurrent |
(Currently not implemented) |
quiet |
If 'TRUE', status updates via console messages are suppressed. |
Invisibly returns the normalized path to all screenshots taken. The character vector will have a class of '"webshot"'.
if (interactive()) { # Whole web page webshot("https://github.com/rstudio/shiny") # Might need a delay for all assets to display webshot("http://rstudio.github.io/leaflet", delay = 0.5) # One can also take screenshots of several URLs with only one command. # This is more efficient than calling 'webshot' multiple times. webshot(c("https://github.com/rstudio/shiny", "http://rstudio.github.io/leaflet"), delay = 0.5) # Clip to the viewport webshot("http://rstudio.github.io/leaflet", "leaflet-viewport.png", cliprect = "viewport") # Specific size webshot("https://www.r-project.org", vwidth = 1600, vheight = 900, cliprect = "viewport") # Manual clipping rectangle webshot("http://rstudio.github.io/leaflet", "leaflet-clip.png", cliprect = c(200, 5, 400, 300)) # Using CSS selectors to pick out regions webshot("http://rstudio.github.io/leaflet", "leaflet-menu.png", selector = ".list-group") # With multiple selectors, the screenshot will contain all selected elements webshot("http://reddit.com/", "reddit-top.png", selector = c("[aria-label='Home']", "input[type='search']")) # Expand selection region webshot("http://rstudio.github.io/leaflet", "leaflet-boxes.png", selector = "#installation", expand = c(10, 50, 0, 50)) # If multiple matches for a given selector, it will take a screenshot that # contains all matching elements. webshot("http://rstudio.github.io/leaflet", "leaflet-p.png", selector = "p") webshot("https://github.com/rstudio/shiny/", "shiny-stats.png", selector = "ul.numbers-summary") # Result can be piped to other commands like resize() and shrink() webshot("https://www.r-project.org/", "r-small.png") %>% resize("75%") %>% shrink() }
if (interactive()) { # Whole web page webshot("https://github.com/rstudio/shiny") # Might need a delay for all assets to display webshot("http://rstudio.github.io/leaflet", delay = 0.5) # One can also take screenshots of several URLs with only one command. # This is more efficient than calling 'webshot' multiple times. webshot(c("https://github.com/rstudio/shiny", "http://rstudio.github.io/leaflet"), delay = 0.5) # Clip to the viewport webshot("http://rstudio.github.io/leaflet", "leaflet-viewport.png", cliprect = "viewport") # Specific size webshot("https://www.r-project.org", vwidth = 1600, vheight = 900, cliprect = "viewport") # Manual clipping rectangle webshot("http://rstudio.github.io/leaflet", "leaflet-clip.png", cliprect = c(200, 5, 400, 300)) # Using CSS selectors to pick out regions webshot("http://rstudio.github.io/leaflet", "leaflet-menu.png", selector = ".list-group") # With multiple selectors, the screenshot will contain all selected elements webshot("http://reddit.com/", "reddit-top.png", selector = c("[aria-label='Home']", "input[type='search']")) # Expand selection region webshot("http://rstudio.github.io/leaflet", "leaflet-boxes.png", selector = "#installation", expand = c(10, 50, 0, 50)) # If multiple matches for a given selector, it will take a screenshot that # contains all matching elements. webshot("http://rstudio.github.io/leaflet", "leaflet-p.png", selector = "p") webshot("https://github.com/rstudio/shiny/", "shiny-stats.png", selector = "ul.numbers-summary") # Result can be piped to other commands like resize() and shrink() webshot("https://www.r-project.org/", "r-small.png") %>% resize("75%") %>% shrink() }