There are four foundations upon which this package rests:

  • the Altair Python package, to build chart specifications
  • the reticulate R package, to provide inter-operability with Python
  • the Vega-Lite JavaScript framework, to render chart specifications in an HTML file
  • the vegawidget R package, which relies on htmlwidgets to provide inter-operability with HTML and JavaScript

This article deals with the second two items; the Field Guide to Python Issues deals with the first two.

Vegawidget

All the rendering is handled by vegawidget; so you are referred to its article on rendering.

The two main issues on rendering are:

  • sizing
  • arguments passed to vega-embed (more specialized)

These are discussed in these sections of the vegawidget rendering-article:

Rendering speed for interactive work

This 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:

library("altair")
alt$data_transformers$enable('json')

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:

    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:

  • you restart R.
  • you enable a different transformer, e.g alt$data_transformers$enable("default").

Altair’s documentation has a page dedicated to data transformers.