If you have V8, withr, and fs 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, ...)

## Arguments

spec

An object to be coerced to vegaspec, a Vega/Vega-Lite specification

width

integer, if specified, the total rendered width (in pixels) of the chart - valid only for single-view charts and layered charts; the default is to use the width in the chart specification

height

integer, if specified, the total rendered height (in pixels) of the chart - valid only for single-view charts and layered charts; the default is to use the height in the chart specification

base_url

character, the base URL for a data file, useful for specifying a local directory; defaults to an empty string

seed

integer, the random seed for a Vega specification, defaults to a "random" integer

scale

numeric, useful for specifying larger images supporting the increased-resolution of retina displays

...

additional arguments passed to vw_to_svg()

path

character, local path to which to write the file

## Value

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

## Details

These functions can be called using (an object that can be coerced to) a vegaspec.

The scripts used are adapted from the Vega command line utilities.

## Examples

  # call any of these functions using either a vegaspec or a vegawidget
svg <- vw_to_svg(vegawidget(spec_mtcars))
bmp <- 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
)