README.md

r2d3svg

Lifecycle: experimental

The simple goal of the r2d3svg package is to provide high-quality vector-based file outputs (svg and pdf) for visualizations created with the r2d3 package (or technically any htmlwidget that produces a single visualization contained in an <svg> tag).

The motivation for this package is the desire for vector-based file outputs of web-based visualizations, potentially for use in publications, etc. The webshot and webshot2 packages provide a great way to capture raster images (or, additionally, entire web page pdf printouts in the case of webshot2), but raster is often not enough.

Installation

You can install r2d3svg with:

# install.packages("remotes")
remotes::install_github("hafen/r2d3svg")

Example

library(r2d3)
library(r2d3svg)
d3 <- r2d3(
  data = c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20),
  script = system.file("examples/barchart.js", package = "r2d3")
)
save_d3_svg(d3, file = tempfile(fileext = ".svg"))
save_d3_pdf(d3, file = tempfile(fileext = ".pdf"))

Notes

This package works by rendering the r2d3 graphic in a headless Chromium-based browser using the chromote package. Once rendered, we can simply extract the contents of the <svg> tag and save it as an SVG file. To create a PDF, the rsvg package is used to convert the SVG to a PDF. For PDF outputs, this is preferable to using webshot2 to create a PDF printout of the entire page.

This package makes use of the chromote package, which is experimental and under devlopment. While using something like PhantomJS under the hood would be more stable, it has been discontinued and cannot handle some modern d3 visualizations (that use ES6 for example).

To be ready for more stable use, there are a few things that would be ideal:



hafen/r2d3svg documentation built on Feb. 7, 2020, 12:23 a.m.