Vega and Vega-Lite use JSON as their specification-format. Within R, it seems natural to work with these specifications as lists. Accordingly, a vegaspec is also a list. This family of functions is used to coerce lists, JSON, and character strings to vegaspec.

as_vegaspec(spec, ...)

# S3 method for default
as_vegaspec(spec, ...)

# S3 method for vegaspec
as_vegaspec(spec, ...)

# S3 method for list
as_vegaspec(spec, ...)

# S3 method for json
as_vegaspec(spec, ...)

# S3 method for character
as_vegaspec(spec, encoding = "UTF-8", ...)

# S3 method for vegawidget
as_vegaspec(spec, ...)



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


Other arguments (attempt to future-proof)


character, if spec is a file or a URL, specifies the encoding.


An object with S3 class vegaspec


The character method for this function will take:

  • JSON string.

  • A path to a local JSON file.

  • A URL that returns a JSON file.

For Vega and Vega-Lite, the translation between lists and JSON is a little bit particular. This function, as_vegaspec(), can be used to translate from JSON; vw_as_json() can be used to translate to JSON.

You can use the function vw_spec_version() to determine if a vegaspec is built for Vega-Lite or Vega. You can use vw_to_vega() to translate a Vega-Lite spec to Vega.


  spec <- list(
    `$schema` = vega_schema(),
    data = list(values = mtcars),
    mark = "point",
    encoding = list(
      x = list(field = "wt", type = "quantitative"),
      y = list(field = "mpg", type = "quantitative"),
      color = list(field = "cyl", type = "nominal")


  if (FALSE) {
    # requires network-access