Takes screenshots of web pages, including Shiny applications.
Webshot makes it easy to take screenshots of web pages from R. It can also run Shiny applications locally and take screenshots of the app.
It requires an installation of the external program PhantomJS. Please make sure you have PhantomJS version 2 or higher installed. Previous versions may have trouble rendering some fonts. You may either download PhantomJS from its website, or use the function
webshot::install_phantomjs() to install it automatically.
Once PhantomJS is installed you can install webshot with:
webshot will use a 992x744 pixel viewport (a virtual browser window) and take a screenshot of the entire page, even the portion outside the viewport.
library(webshot)webshot("", "r.png")webshot("", "r.pdf") # Can also output to PDF
You can clip it to just the viewport region:
webshot("", "r-viewport.png", cliprect = "viewport")
You can also get screenshots of a portion of a web page using CSS selectors. If there are multiple matches for the CSS selector, it will use the first match.
webshot("", "r-sidebar.png", selector = ".sidebar")
If you supply multiple CSS selectors, it will take a screenshot containing all of the selected items.
webshot("", "r-selectors.png",selector = c("#getting-started", "#news"))
The clipping rectangle can be expanded to capture some area outside the selected items:
webshot("", "r-expand.png",selector = "#getting-started",expand = c(40, 20, 40, 20))
You can take higher-resolution screenshots with the
zoom option. This isn't exactly the same as taking a screenshot with a HiDPI ("Retina") device: it is like increasing the zoom to 200% in a desktop browser and 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).
webshot("", "r-sidebar-zoom.png",selector = ".sidebar", zoom = 2)
All parameters of function
webshot. That means that multiple screenshots can be taken with a single command. When taking a lot of screenshots, vectorization can divide by 5 the execution time.
# Take a screenshot of different siteswebshot(c("", ""),file = c("r.png", "webshot.png"))# Save screenshots of the same site in different formatswebshot("", file = c("r.png", "r.pdf"))# Take screenshots of different sections of the same site.# Note that unlike arguments "url" and "file", a list is required to specify# multiple selectors. This is also the case for arguments "cliprect" and# "expand"webshot("",file = c("leaflet_features.png", "leaflet_install.png"),selector = list("#features", "#installation"))
appshot() function will run a Shiny app locally in a separate R process, and take a screenshot of it. After taking the screenshot, it will kill the R process that is running the Shiny app.
# Get the directory of one of the Shiny examplesappdir <- system.file("examples", "01_hello", package="shiny")appshot(appdir, "01_hello.png")
If you have GraphicsMagick (recommended) or ImageMagick installed, you can pass the result to
resize() to resize the image after taking the screenshot. This can take any valid ImageMagick geometry specifictaion, like
"400x" (for an image 400 pixels wide). However, you may get different (and often better) results by using the
zoom option: the fonts and graphical elements will render more sharply. However, compared to simply resizing, zooming out may result in slightly different positioning of text and layout elements.
You can also call
shrink(), which runs OptiPNG to shrink the PNG file losslessly.
webshot("", "r-small-resized.png") %>%resize("75%") %>%shrink()# Using zoom instead of resize()webshot("", "r-small-zoomed.png", zoom = 0.75) %>%shrink()# Can specify pixel dimensions for resize()webshot("", "r-small.png") %>%resize("400x") %>%shrink()
To illustrate the difference between
zoom, here is an image with
And here is one with
zoom = 0.5. If you look closely, you'll see that the text and graphics are sharper. You'll also see that the bullet points and text are positioned slightly differently:
Updated to CasperJS 1.1.3.
zoom option for higher-resolution screen shots. (#26)
webshot() now returns objects with class
webshot. There is also a new
knit_print method for
webshot objects. (#27)
Fixed problem installing PhantomJS on R 3.3.2 and above. (#35)
The leading tilde in the path of PhantomJS is expanded now (#19).
Changed URL for PhantomJS binaries so that
install_phantomjs() doesn't hit rate limits, and added workaround for downloading problems with R 3.3.0 and 3.3.1.
appshot()to take screenshots via PhantomJS, and
shrink()to manipulate images via GraphicsMagick/ImageMagick and OptiPNG.