image | R Documentation |
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, ...)
spec |
An object to be coerced to |
width |
|
height |
|
base_url |
|
seed |
|
scale |
|
... |
additional arguments passed to |
path |
|
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.
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
# 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
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.