ibrahim ozturk

Entrepreneur, Tech CTO, Software Developer, Electronics Engineer, Author

Export/Save R Graphs with webshot

Category : General

The webshot package makes it easy to take screenshots of web pages from R. In order to export the graphs or figures created with plotly or other graphing libraries as static images/files, webshot is also great option in addition to the Orca. Hence, saving locally generated plots as png, jpg or pdf will be pretty straightforward at the end of this post. Enjoy with it!

Webshot requires an installation of the external program PhantomJS on R/RStudio. You might get this error: “PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable. NULL”, if you have not already installed PhantomJS on your computer. Anyway, either you might download the binary and put it in PATH manually or you might easily install phantom.js as:

# See ?install_phantomjs for the details
# install_phantomjs()

Instead of using webshot, you should consider to try webshot2 because webshot suffers from plotly or graph exporting issues unlike webshot2. See also my detailed answer on stackoverflow. You could find that various developers have issues on stackoverflow related with this side, and I have replicated/solved many of them by just replacing webshot with webshot2. You can access those examples throughout my stackoverflow profile as well.

Yes, moving on webshot2?

Before trying to install webshot2 package, do not forget to remove webshot if you have already one installed. In order to remove it, go to the Packages in right bottom corner of Rstudio, sear the package name and click on the adjacent X icon to remove it or you I handle it in this way from the Rstudio console:

remove.packages("webshot", lib="~/R/win-library/3.6")

If you have not done this uninstallation, you might get this kind of error:

Attaching package: ‘webshot2’
The following object is masked _by_ ‘.GlobalEnv’:

The following objects are masked from ‘package:webshot’:
appshot, resize, rmdshot, shrink, webshot

Installation of webshot2

devtools::install_github("rstudio/webshot2", force=TRUE)

Usage of webshot2


Now, we can present a minimal example:

Created on 28/04/2020
@author: Ibrahim Ozturk
@author: www.ozturkibrahim.com

# Webshot and phantomjs have been previously installed.
# Then, I loaded related packages for the minimal scenario.

test_p       = plot_ly(x = iris$Petal.Width, y = iris$Petal.Length, type = "scatter", mode = "markers")

# Save a rendered widget to an HTML file
saveWidget(widget = test_p, file = "test_p.html")

# Saving taken webshot
webshot(url       = "test_p.html", file = "webshot_iris.png")

You will get something like this message on the console:

file://C:\Users\root\.......\test_p.html screenshot completed

You could modify the extension as you like such as .png, .pdf, or .jpeg.
The output file


Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


error: Content is protected !!