Take Screenshots of Web Pages

Takes screenshots of web pages, including Shiny applications and R Markdown documents.

Travis-CI BuildStatus AppVeyor BuildStatus

Webshot makes it easy to take screenshots of web pages from R. It can also:

  • Run Shiny applications locally and take screenshots of the application.
  • Render R Markdown documents and take screenshots of the document. Webshot can handle both static Rmd documents and interactive ones (those with runtime: shiny).

See the introduction article for examples in addition to the ones below.


Webshot can be installed from CRAN. Webshot also requires the external program PhantomJS. You may either download PhantomJS from its website, or use the function webshot::install_phantomjs() to install it automatically.



By default, 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.

webshot("https://www.r-project.org/", "r.png")
webshot("https://www.r-project.org/", "r.pdf") # Can also output to PDF

You can clip it to just the viewport region:

webshot("https://www.r-project.org/", "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("https://www.r-project.org/", "r-sidebar.png", selector = ".sidebar")

If you supply multiple CSS selectors, it will take a screenshot containing all of the selected items.

webshot("https://www.r-project.org/", "r-selectors.png",
        selector = c("#getting-started", "#news"))

The clipping rectangle can be expanded to capture some area outside the selected items:

webshot("https://www.r-project.org/", "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("https://www.r-project.org/", "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 sites
webshot(c("https://www.r-project.org/", "https://github.com/wch/webshot"),
        file = c("r.png", "webshot.png"))
# Save screenshots of the same site in different formats
webshot("https://www.r-project.org/", 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"
        file = c("leaflet_features.png", "leaflet_install.png"),
        selector = list("#features", "#installation"))

Screenshots of Shiny applications

The 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 examples
appdir <- system.file("examples", "01_hello", package="shiny")
appshot(appdir, "01_hello.png")

Screenshots of R Markdown documents

The rmdshot() function takes screenshots of R Markdown documents. For static R Markdown documents, it renders them to HTML in a temporary directory (using rmarkdown::render())and then takes a screenshot.

For dynamic R Markdown documents, it runs them using rmarkdown::run() in a separate R process and then takes a screenshot. After taking the screenshot, it will kill the R process that is running the document.

rmdshot("document.rmd", "document.png")

Manipulating images

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 "75%", or "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("https://www.r-project.org/", "r-small-resized.png") %>%
  resize("75%") %>%
# Using zoom instead of resize()
webshot("https://www.r-project.org/", "r-small-zoomed.png", zoom = 0.75) %>%
# Can specify pixel dimensions for resize()
webshot("https://www.r-project.org/", "r-small.png") %>%
  resize("400x") %>%

To illustrate the difference between resize() and zoom, here is an image with resize("50%"):

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:


webshot 0.5.1

  • Added debug parameter. When it is set to TRUE, webshot() will print out debugging messages from PhantomJS and CasperJS.

  • Fixed #51: Webshot had trouble with some sites that use HTTPS.

  • Added appshot.shiny.appobj functionality (schloerke, #55)

webshot 0.5.0

  • Added support for R Markdown documents. (#48)

  • Closed #42: Converted some instances of system2() to use processx instead.

webshot 0.4.2

  • Fixed #43: The eval argument for webshot() did not work.

webshot 0.4.1

  • Updated vignette so it doesn't error when PhantomJS is not present.

webshot 0.4.0

  • webshot, resize, and shrink all now accept a vector of URLs or filenames. ((#32), #33)

  • Updated to CasperJS 1.1.3.

  • Added 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)

webshot 0.3.2

  • Better handling of local paths in Windows. (#23)

  • More robust searching for ImageMagick. (#13)

webshot 0.3.1

  • 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.

webshot 0.3

  • The first CRAN release. Provided functions webshot()/appshot() to take screenshots via PhantomJS, and resize()/shrink() to manipulate images via GraphicsMagick/ImageMagick and OptiPNG.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.5.2 by Winston Chang, 2 years ago


Report a bug at https://github.com/wch/webshot/issues

Browse source code at https://github.com/cran/webshot

Authors: Winston Chang [aut, cre] , Yihui Xie [ctb] , Francois Guillem [ctb] , Barret Schloerke [ctb] , Nicolas Perriault [ctb] (The CasperJS library)

Documentation:   PDF Manual  

Task views: Web Technologies and Services

GPL-2 license

Imports magrittr, jsonlite, callr

Suggests httpuv, knitr, rmarkdown, shiny

System requirements: PhantomJS (http://phantomjs.org) for taking screenshots, ImageMagick (http://www.imagemagick.org) or GraphicsMagick (http://www.graphicsmagick.org) and OptiPNG (http://optipng.sourceforge.net) for manipulating images.

Imported by BETS, GALLO, antaresViz, ari, flow, heatmaply, inlmisc, kableExtra, manipulateWidget, mapview, mdsr, namedropR, nomnoml, oncrawlR, tableHTML, tidycwl, trelliscopejs, vegalite.

Suggested by BED, EHRtemporalVariability, NGLVieweR, PandemicLP, TCIU, ariExtra, asteRisk, autoshiny, bayesPO, behaviorchange, bookdown, codebook, drake, flextable, ggiraphExtra, gt, hablar, iheatmapr, knitr, modeltime, optimall, pkgnet, plotly, r2d3, rENA, radiant.data, rtweet, sdcTable, simlandr, sortable, starsTileServer, ubiquity, validata, visNetwork, wordcloud2, xpose.

See at CRAN