Demo of the Webshot R package. Webshot makes it easy to take screenshots of web pages (all or sections of it defined by their css selectors) from R, and save them as png or pdf. It requires an installation of the external program PhantomJS (v2+). You may install phantomjs automatically through the provided function webshot::install_phantomjs() ::.


Contents of this page:

1.1. Installation

RR Code
if (!require(webshot)) { 
	install.packages("webshot", repos="https://ftp.heanet.ie/mirrors/cran.r-project.org/") 

	# You might also want to install the development version, if you need.
	# if (!require(devtools)) {	install.packages("devtools", repos="https://ftp.heanet.ie/mirrors/cran.r-project.org/") }

	# You need to create first the folder "~/bin", which in this installation is "/var/www/bin/, since this is the place where phantomjs will be installed to and added to the PATH in GNU/Linux

1.2. Demo1

RR Code
website <- "http://rstudio.github.io/leaflet/"
outfilename.noext <- file.path(getwd(), "mywebshot")
# Might need a longer delay for all assets to display
webshot(website, paste0(outfilename.noext, ".png"), delay = 0.5)
webshot(website, paste0(outfilename.noext, ".pdf"), delay = 0.5) # Can also output to PDF
webshot(website, paste0(outfilename.noext, ".png"), selector = c("#features", "#installation"))

## Image embedded below in this wiki page with:
# ^{img src="temp/cache/R_r_test14_webshot/mywebshot.png"}^


1.3. Demo2

RR Code
outfilename.noext <- file.path(getwd(), "printtorture")
webshot("http://trunk.notre.website/print-torture", paste0(outfilename.noext, ".png"))
webshot("http://trunk.notre.website/print-torture", paste0(outfilename.noext, ".pdf")) # Can also output to PDF
webshot("http://trunk.notre.website/print-torture", paste0(outfilename.noext, "_data.png"), 
        selector = "#page-data")

## Image embedded below in this wiki page with:
# ^{img src="temp/cache/R_r_test14_webshot/printtorture_data.png"}^
## Link to PDF file displayed below with 
# [temp/cache/R_r_test14_webshot/printtorture.pdf|PDF]
PHANTOM ERROR: CasperError: No element matching selector found: #page-data
 -> phantomjs://platform/casper.js: 1043 (in function getElementBounds)
 -> phantomjs://code/webshot.js: 124
 -> undefined: 0 (in function map)
 -> phantomjs://code/webshot.js: 123 (in function findClipRect)
 -> phantomjs://code/webshot.js: 74
 -> phantomjs://platform/casper.js: 1577 (in function runStep)
 -> phantomjs://platform/casper.js: 404 (in function checkStep)


Show php error messages