vignettes/field-guide-rendering.Rmd
field-guide-rendering.Rmd
There are four foundations upon which this package rests:
This article deals with the second two items; the Field Guide to Python Issues deals with the first two.
All the rendering is handled by vegawidget; so you are referred to its article on rendering.
The two main issues on rendering are:
These are discussed in these sections of the vegawidget rendering-article:
vegawidget()
functionThis section applies to interactive work; it does not apply to RMarkdown documents.
Rather than embed the data itself into the chart spec as JSON (this will become very slow as the data gets to be more than 5000 rows), Altair has an option (supported by Vega-Lite’s URL loader) that saves your data as a JSON file that is referenced in the chart spec.
Keep in mind that this will work only for interactive (non-knitting)
work. In Python you would set
alt.data_transformers.enable('json')
; in R this
becomes:
The problem is that you won’t see charts in the RStudio pane anymore. This won’t work:
vega_data <- import_vega_data()
chart <-
alt$Chart(vega_data$cars())$
mark_point()$
encode(
x = "Horsepower:Q",
y = "Miles_per_Gallon:Q",
color = "Origin:N",
tooltip = c("Name", "Horsepower", "Miles_per_Gallon", "Origin")
)
chart
This calls the print method on chart, which runs
vegawidget(chart)
; instead, you need to tell
vegawidget()
where it can expect your external file.
You have a couple of choices here:
you can call vegawidget()
supplying a value for
base_url
:
vegawidget(chart, base_url=".")
or you can set the base_url
once for your session using:
vw_set_base_url(".")
then, this should just work interactively:
chart
Keep in mind that by using
alt$data_transformers$enable("json")
, all
charts you create subsequently will write out data files until:
alt$data_transformers$enable("default")
.Altair’s documentation has a page dedicated to data transformers.