Capturing wild widgets with webshot
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
NOTE: you won’t need to use this function if you use the development version of knitr
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
path
reference (e.g./path/to/widget.png
) - a
markdown
image reference (e.g.
) - an
html
image reference (e.g.
), or - an
inline
base64 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.
#' Capture a static (png) version of a widget (e.g. for use in a PDF knitr document) #' #' Widgets are generally interactive beasts rendered in an HTML DOM with #' javascript. That makes them unusable in PDF documents. However, many widgets #' initial views would work well as static images. This function renders a widget #' to a file and make it usable in a number of contexts. #' #' What is returned depends on the value of code{output}. By default (code{"path"}), #' the full disk path will be returned. If code{markdown} is specified, a markdown #' string will be returned with a code{file:///...} URL. If code{html} is #' specified, an code{ |
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.