NOTE: you won’t need to use this function if you use the development version of
Winston Chang released his
webshot package to CRAN this past week. The package wraps the immensely useful
phantomjs utility and makes it dirt simple to capture whole or partial web pages in R. One beautiful bonus feature of
webshot is that you can install
phamtomjs with it (getting
phantomjs to work on Windows is a pain).
You can do many things with the
webshot package but I hastily drafted this post to put forth a means to generate a static image from an
htmlwidget. I won’t elaborate much since I included a fully
roxygen-doc’d function below, but the essence of
capture_widget() is to pass in an
htmlwidget object and have it rendered for you to a
png file and get back either:
- a file system
markdownimage reference (e.g.
htmlimage reference (e.g.
<img src='file:///path/to/widget.png'/>), or
inlinebase64 encoded HTML imgage reference (e.g.
which you can then use in R markdown documents knitted to PDF (or in any other context).
Take a look at the function, poke the tyres and drop suggestions in the comments. I’ll add this to one of my widgets soon so folks can submit complaints or enhancements via issues & PRs on github).
To use the function, just pipe a sized widget to it and use the output from it.