Data 304
The goal of vegawidget is to render Vega-Lite and Vega specifications as htmlwidgets, and to help you communicate with a Vega chart using JavaScript or Shiny. Its ambition is to be a low-level interface to the Vega(-Lite) API, so that other packages can build upon it.
Accordingly, this package may be useful to:
- build (using lists of lists) re-usable Vega and Vega-Lite specifications for deployment elsewhere.
- develop higher-level, user-friendly packages to compose specific types of plots, or even to build a general ggplot2-like framework, using this package as the rendering foundation.
Create JSON spec as an R string.
Pass string to as_vegaspec()
to convert to vegaspec and render.
```{r}
'
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"url": "https://cdn.jsdelivr.net/npm/vega-datasets@2.8.0/data/gapminder.json"},
"height": 150, "width": 400,
"mark": "point",
"transform": [{"filter": "datum.year == 1990"}],
"encoding": {
"x": {"field": "fertility", "type": "quantitative"},
"y": {"field": "life_expect", "type": "quantitative"},
"color": {"value": "maroon"}
}
}' |> as_vegaspec()
```
Instead of creating a Vega-Lite specification in JSON as a string, you have the option to
vega_schema()
to output the schema string.as_vegaspec()
```{r}
my_spec <-
list(
`$schema` = vega_schema(),
data = list(url = "https://cdn.jsdelivr.net/npm/vega-datasets@2.8.0/data/gapminder.json"),
height = 150, width = 400,
mark = "point",
transform = list(list(filter = "datum.year == 1990")),
encoding = list(
x = list(field = "fertility", type = "quantitative"),
y = list(field = "life_expect", type = "quantitative"),
color = list(value = "maroon")
)
)
my_spec |> as_vegaspec()
```
You can export your Vega-Lite graphic as SVG or PNG.
Use this code to include your graphic in a quarto document.

I recommend using the typst
format for PDFs.
Do that by setting the format to typst
in your YAML header.
From the vegawidget
documentation:
This package provides functions to render Vega(-Lite) specifications; although it provides some helpers, it does not provide higher-level functions to build specifications. Rather, this is left to other packages. Even though you can use its functions directly, you are invited to import and re-export them for use in your package.
R packages altair
and vegabrite
provide tools to build Vega-Lite specifications using R functions.
Vega-Lite specifications can also be created with Python or Javascript code.