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, ...)
An object to be coerced to vegaspec
, a Vega/Vega-Lite specification
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
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
character
, the base URL for a data file, useful for
specifying a local directory; defaults to an empty string
integer
, the random seed for a Vega specification,
defaults to a "random" integer
numeric
, useful for specifying larger images supporting the
increased-resolution of retina displays
additional arguments passed to vw_to_svg()
character
, local path to which to write the file
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
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.
# 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
)