Learn R Programming

vegawidget

Vega-Lite is an implementation of the grammar-of-graphics, rendered in the browser with interactivity.

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.

Features

New to vegawidget 0.4

  • vegawidget now supports the last two Vega-Lite major-versions, currently versions 5 and 4.

    However, for a given R session (e.g. rendering of an RMarkdown file), the vegawidget() function can use only one major-version; this version is determined using the $schema element of the first vegaspec evaluated using vegawidget().

    This restriction does not apply to the image functions, e.g. vw_to_svg(), or to the compilation function, vw_to_vega().

  • use vega_version_all() to see the available versions:

library("vegawidget")

vega_version_all()
#>   widget vega_lite   vega vega_embed
#> 1    vl5     5.2.0 5.21.0     6.20.2
#> 2    vl4    4.17.0 5.17.0     6.12.2
  • Compiling a spec and creating an image now uses the V8 package, rather than depending on a local installation of nodejs.

Installation

You can install vegawidget from CRAN with:

install.packages("vegawidget")

The development version of vegawidget is available from GitHub with:

# install.packages("devtools")
devtools::install_github("vegawidget/vegawidget")

Note: There are documentation websites for both the CRAN version and the development version of this package.

Introduction

Vega(-Lite) specifications are just text, formatted as JSON. However, in R, we can use lists to build specifications:

library("vegawidget")

spec_mtcars <-
  list(
    `$schema` = vega_schema(), # specifies Vega-Lite
    description = "An mtcars example.",
    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")
    )
  ) %>% 
  as_vegaspec()

The as_vegaspec() function is used to turn the list into a vegaspec; many of this package’s functions are built to support, and render, vegaspecs:

spec_mtcars

The rendering of the chart above depends on where you are reading it:

  • On this package’s pkgdown site, it is rendered as part of an HTML environment, showing its full capabilities.

  • At its GitHub code site, the chart is further rendered to a static SVG file, then incorporated into the Markdown rendering.

A learnr tutorial is available: learnr::run_tutorial("overview", package = "vegawidget").

For more, please see our Getting Started article. Additionally, the Vega-Lite website has a comprehensive introduction.

Other articles for this package:

Acknowledgements

  • Alicia Schep has been instrumental in guiding the evolution of the API, and for introducing new features, particularly the JavaScript and Shiny functions.
  • Haley Jeppson and Stuart Lee have provided valuable feedback and contributions throughout the package’s development.
  • Bob Rudis and the vegalite package provided a lot of the inspiration for this work, providing a high-level interface to Vega-Lite.
  • The Altair developers, for further popularizing the notion of using a programming language (Python) to create and render Vega-Lite specifications.
  • The Vega-Lite developers, for providing a foundation upon which the rest of this is built.

Contributing

Contributions are welcome, please see this guide. Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('vegawidget')

Monthly Downloads

1,108

Version

0.4.1

License

MIT + file LICENSE

Maintainer

Last Published

January 29th, 2022

Functions in vegawidget (0.4.1)

.vw_handler_body

Constructor for internal S3 class
shiny-setters

Set information in a Vega chart from Shiny
shiny-getters

Get information from a Vega chart into Shiny
vw_fetch

Callback helpers
vegawidgetOutput

Shiny-output for vegawidget
use_vegawidget

Add vegawidget functions to your package
vw_as_json

Coerce vegaspec to JSON
spec_mtcars

Example vegaspec: mtcars scatterplot
vega_embed

Vega embed options
vw_handler

Construct a vegawidget handler
knit_print.vegaspec

Knit-print method
glue_js

Interpolate into a JavaScript string
image

Create or write image
vega_schema

Create string for schema-URL
%>%

Pipe operator
vw_autosize

Autosize vegaspec
vw_rename_datasets

Rename datasets in a vegaspec
vw_handler_signal

Construct a JavaScript handler
vw_examine

Examine vegaspec
vegawidget

Create a Vega/Vega-Lite htmlwidget
vega_version

Get Vega JavaScript versions
vw_to_vega

Convert to Vega specification
vw_spec_version

Determine vegaspec version
vw_shiny_demo

Run Shiny demonstration-apps
JS

Mark character strings as literal JavaScript code
renderVegawidget

Render shiny-output for vegawidget
add-listeners

Add JavaScript listeners
vw_handler_add_effect

Add a side-effect to a JavaScript handler
vw_set_base_url

Set base URL
vw_serialize_data

Serialize data-frame time-columns
vw_handler_body_compose

Compose a JavaScript handler
s3_register

Register an s3 method
.vw_handler_def

Constructor for internal S3 class
as_vegaspec

Coerce to vegaspec
data_category

Example dataset: Categorical data
data_seattle_daily

Example dataset: Seattle daily weather
data_seattle_hourly

Example dataset: Seattle hourly temperatures