The main use of this package is to render a vegawidget, which is also an htmlwidget. This function builds a vegawidget using a vegaspec.

vegawidget(
spec,
embed = NULL,
width = NULL,
height = NULL,
elementId = NULL,
base_url = NULL,
...
)

## Arguments

spec

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

embed

list to specify vega-embed options, see Details on how this is set if NULL.

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

elementId

character, explicit element ID for the vegawidget, useful if you have other JavaScript that needs to explicitly discover and interact with a specific vegawidget

base_url

character, the base URL to prepend to data-URL elements in the vegaspec. This could be the path to a local directory that contains a local file referenced in the spec. It could be the base for a remote URL. Please note that by specifying the base_url here, you will override any loader that you specify using vega_embed(). Please note that this does not work with knitr. See examples.

...

other arguments passed to htmlwidgets::createWidget()

## Value

S3 object of class vegawidget and htmlwidget

## Details

If embed is NULL, vegawidget() uses:

• getOption("vega.embed"), if that is NULL:

• an empty call to vega_embed()

The most-important arguments to vega_embed() are:

• renderer, to specify "canvas" (default) or "svg"

• actions, to specify action-links for export, source, compiled, and editor

If either width or height is specified, the autosize() function is used to override the width and height of the spec. There are some important provisions:

• Specifying width and height is effective only for single-view charts and layered charts. It will not work for concatenated, faceted, or repeated charts.

• In the spec, the default interpretation of width and height is to describe the dimensions of the plotting rectangle, not including the space used by the axes, labels, etc. Here, width and height describe the dimensions of the entire rendered chart, including axes, labels, etc.

Please note that if you are using a remote URL to refer to a dataset in your vegaspec, it may not render properly in the RStudio IDE, due to a security policy set by RStudio. If you open the chart in a browser, it should render properly.

vega-embed options, vega_embed(), vw_autosize()

## Examples

  vegawidget(spec_mtcars, width = 350, height = 350)

{"x":{
"chart_spec": {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json", "width": 350, "height": 350, "description": "An mtcars example.", "data": { "values": [ { "mpg": 21, "cyl": 6, "disp": 160, "hp": 110, "drat": 3.9, "wt": 2.62, "qsec": 16.46, "vs": 0, "am": 1, "gear": 4, "carb": 4, "_row": "Mazda RX4" }, { "mpg": 21, "cyl": 6, "disp": 160, "hp": 110, "drat": 3.9, "wt": 2.875, "qsec": 17.02, "vs": 0, "am": 1, "gear": 4, "carb": 4, "_row": "Mazda RX4 Wag" }, { "mpg": 22.8, "cyl": 4, "disp": 108, "hp": 93, "drat": 3.85, "wt": 2.32, "qsec": 18.61, "vs": 1, "am": 1, "gear": 4, "carb": 1, "_row": "Datsun 710" }, { "mpg": 21.4, "cyl": 6, "disp": 258, "hp": 110, "drat": 3.08, "wt": 3.215, "qsec": 19.44, "vs": 1, "am": 0, "gear": 3, "carb": 1, "_row": "Hornet 4 Drive" }, { "mpg": 18.7, "cyl": 8, "disp": 360, "hp": 175, "drat": 3.15, "wt": 3.44, "qsec": 17.02, "vs": 0, "am": 0, "gear": 3, "carb": 2, "_row": "Hornet Sportabout" }, { "mpg": 18.1, "cyl": 6, "disp": 225, "hp": 105, "drat": 2.76, "wt": 3.46, "qsec": 20.22, "vs": 1, "am": 0, "gear": 3, "carb": 1, "_row": "Valiant" }, { "mpg": 14.3, "cyl": 8, "disp": 360, "hp": 245, "drat": 3.21, "wt": 3.57, "qsec": 15.84, "vs": 0, "am": 0, "gear": 3, "carb": 4, "_row": "Duster 360" }, { "mpg": 24.4, "cyl": 4, "disp": 146.7, "hp": 62, "drat": 3.69, "wt": 3.19, "qsec": 20, "vs": 1, "am": 0, "gear": 4, "carb": 2, "_row": "Merc 240D" }, { "mpg": 22.8, "cyl": 4, "disp": 140.8, "hp": 95, "drat": 3.92, "wt": 3.15, "qsec": 22.9, "vs": 1, "am": 0, "gear": 4, "carb": 2, "_row": "Merc 230" }, { "mpg": 19.2, "cyl": 6, "disp": 167.6, "hp": 123, "drat": 3.92, "wt": 3.44, "qsec": 18.3, "vs": 1, "am": 0, "gear": 4, "carb": 4, "_row": "Merc 280" }, { "mpg": 17.8, "cyl": 6, "disp": 167.6, "hp": 123, "drat": 3.92, "wt": 3.44, "qsec": 18.9, "vs": 1, "am": 0, "gear": 4, "carb": 4, "_row": "Merc 280C" }, { "mpg": 16.4, "cyl": 8, "disp": 275.8, "hp": 180, "drat": 3.07, "wt": 4.07, "qsec": 17.4, "vs": 0, "am": 0, "gear": 3, "carb": 3, "_row": "Merc 450SE" }, { "mpg": 17.3, "cyl": 8, "disp": 275.8, "hp": 180, "drat": 3.07, "wt": 3.73, "qsec": 17.6, "vs": 0, "am": 0, "gear": 3, "carb": 3, "_row": "Merc 450SL" }, { "mpg": 15.2, "cyl": 8, "disp": 275.8, "hp": 180, "drat": 3.07, "wt": 3.78, "qsec": 18, "vs": 0, "am": 0, "gear": 3, "carb": 3, "_row": "Merc 450SLC" }, { "mpg": 10.4, "cyl": 8, "disp": 472, "hp": 205, "drat": 2.93, "wt": 5.25, "qsec": 17.98, "vs": 0, "am": 0, "gear": 3, "carb": 4, "_row": "Cadillac Fleetwood" }, { "mpg": 10.4, "cyl": 8, "disp": 460, "hp": 215, "drat": 3, "wt": 5.424, "qsec": 17.82, "vs": 0, "am": 0, "gear": 3, "carb": 4, "_row": "Lincoln Continental" }, { "mpg": 14.7, "cyl": 8, "disp": 440, "hp": 230, "drat": 3.23, "wt": 5.345, "qsec": 17.42, "vs": 0, "am": 0, "gear": 3, "carb": 4, "_row": "Chrysler Imperial" }, { "mpg": 32.4, "cyl": 4, "disp": 78.7, "hp": 66, "drat": 4.08, "wt": 2.2, "qsec": 19.47, "vs": 1, "am": 1, "gear": 4, "carb": 1, "_row": "Fiat 128" }, { "mpg": 30.4, "cyl": 4, "disp": 75.7, "hp": 52, "drat": 4.93, "wt": 1.615, "qsec": 18.52, "vs": 1, "am": 1, "gear": 4, "carb": 2, "_row": "Honda Civic" }, { "mpg": 33.9, "cyl": 4, "disp": 71.1, "hp": 65, "drat": 4.22, "wt": 1.835, "qsec": 19.9, "vs": 1, "am": 1, "gear": 4, "carb": 1, "_row": "Toyota Corolla" }, { "mpg": 21.5, "cyl": 4, "disp": 120.1, "hp": 97, "drat": 3.7, "wt": 2.465, "qsec": 20.01, "vs": 1, "am": 0, "gear": 3, "carb": 1, "_row": "Toyota Corona" }, { "mpg": 15.5, "cyl": 8, "disp": 318, "hp": 150, "drat": 2.76, "wt": 3.52, "qsec": 16.87, "vs": 0, "am": 0, "gear": 3, "carb": 2, "_row": "Dodge Challenger" }, { "mpg": 15.2, "cyl": 8, "disp": 304, "hp": 150, "drat": 3.15, "wt": 3.435, "qsec": 17.3, "vs": 0, "am": 0, "gear": 3, "carb": 2, "_row": "AMC Javelin" }, { "mpg": 13.3, "cyl": 8, "disp": 350, "hp": 245, "drat": 3.73, "wt": 3.84, "qsec": 15.41, "vs": 0, "am": 0, "gear": 3, "carb": 4, "_row": "Camaro Z28" }, { "mpg": 19.2, "cyl": 8, "disp": 400, "hp": 175, "drat": 3.08, "wt": 3.845, "qsec": 17.05, "vs": 0, "am": 0, "gear": 3, "carb": 2, "_row": "Pontiac Firebird" }, { "mpg": 27.3, "cyl": 4, "disp": 79, "hp": 66, "drat": 4.08, "wt": 1.935, "qsec": 18.9, "vs": 1, "am": 1, "gear": 4, "carb": 1, "_row": "Fiat X1-9" }, { "mpg": 26, "cyl": 4, "disp": 120.3, "hp": 91, "drat": 4.43, "wt": 2.14, "qsec": 16.7, "vs": 0, "am": 1, "gear": 5, "carb": 2, "_row": "Porsche 914-2" }, { "mpg": 30.4, "cyl": 4, "disp": 95.1, "hp": 113, "drat": 3.77, "wt": 1.513, "qsec": 16.9, "vs": 1, "am": 1, "gear": 5, "carb": 2, "_row": "Lotus Europa" }, { "mpg": 15.8, "cyl": 8, "disp": 351, "hp": 264, "drat": 4.22, "wt": 3.17, "qsec": 14.5, "vs": 0, "am": 1, "gear": 5, "carb": 4, "_row": "Ford Pantera L" }, { "mpg": 19.7, "cyl": 6, "disp": 145, "hp": 175, "drat": 3.62, "wt": 2.77, "qsec": 15.5, "vs": 0, "am": 1, "gear": 5, "carb": 6, "_row": "Ferrari Dino" }, { "mpg": 15, "cyl": 8, "disp": 301, "hp": 335, "drat": 3.54, "wt": 3.57, "qsec": 14.6, "vs": 0, "am": 1, "gear": 5, "carb": 8, "_row": "Maserati Bora" }, { "mpg": 21.4, "cyl": 4, "disp": 121, "hp": 109, "drat": 4.11, "wt": 2.78, "qsec": 18.6, "vs": 1, "am": 1, "gear": 4, "carb": 2, "_row": "Volvo 142E" } ] }, "mark": "point", "encoding": { "x": { "field": "wt", "type": "quantitative" }, "y": { "field": "mpg", "type": "quantitative" }, "color": { "field": "cyl", "type": "nominal" } }, "config": { "autosize": { "type": "fit", "contains": "padding" }, "view": { "width": 350, "height": 350 } } }, "embed_options": { "renderer": "canvas", "defaultStyle": true } },"evals":[],"jsHooks":[]} # vegaspec with a data 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()

# define local path to file
path_local <- system.file("example-data", package = "vegawidget")

# render using local path (does not work with knitr)
vegawidget(spec_precip, base_url = path_local)

{"x":{
"chart_spec": {
"\$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"url": "seattle-weather.csv"
},
"mark": "tick",
"encoding": {
"x": {
"field": "precipitation",
"type": "quantitative"
}
}
},
"embed_options": {
"renderer": "canvas",
"defaultStyle": true,
"baseURL": "lib/data-fe4446d11609146234a49085dc284285-0.0.0/"
}
}
},"evals":[],"jsHooks":[]}
if (FALSE) {
# requires network-access

# define remote path to file
url_remote <- "https://vega.github.io/vega-datasets/data"

# render using remote path
# note: does not render in RStudio IDE; open using browser
vegawidget(spec_precip, base_url = url_remote)
}