If you have nodejs installed,
you can use these functions can to create
or write images as PNG or SVG, using a vegaspec or vegawidget.
To convert to a bitmap, or write a PNG file, you will additionally need
the rsvg and
png packages.
vw_to_svg(spec, width = NULL, height = NULL, base_url = NULL, seed = NULL) vw_to_bitmap(spec, scale = 1, width = NULL, height = NULL, ...) vw_write_svg(spec, path, width = NULL, height = NULL, ...) vw_write_png(spec, path, scale = 1, width = NULL, height = NULL, ...)
| spec | An object to be coerced to |
|---|---|
| width |
|
| height |
|
| base_url |
|
| seed |
|
| scale |
|
| ... | additional arguments passed to |
| path |
|
vw_to_svg()character, SVG string
vw_to_bitmap()array, bitmap array
vw_write_svg()invisible vegaspec or vegawidget, called for side-effects
vw_write_png()invisible vegaspec or vegawidget, called for side-effects
There is a known limitation to these functions - if you are using a vegaspec
that has dataset loaded from a remote URL. The nodejs scripts are not
able to use a proxy, so if your computer uses a proxy to access the remote URL,
the data will not load.
These functions can be called using (an object that can be coerced to)
a vegaspec.
The nodejs scripts used are adapted from the Vega command line utilities.
if (FALSE) { # requires nodejs to be installed # call any of these functions using either a vegaspec or a vegawidget vw_to_svg(vegawidget(spec_mtcars)) vw_to_bitmap(spec_mtcars) vw_write_png(spec_mtcars, file.path(tempdir(), "temp.png")) vw_write_svg(spec_mtcars, file.path(tempdir(), "temp.svg")) # To specify the path to a local file, use base_url spec_precip <- list( `$schema` = vega_schema(), data = list(url = "seattle-weather.csv"), mark = "tick", encoding = list( x = list(field = "precipitation", type = "quantitative") ) ) %>% as_vegaspec() data_dir <- system.file("example-data/", package = "vegawidget") vw_write_png( spec_precip, file.path(tempdir(), "temp-local.png"), base_url = data_dir ) }