Learn R Programming

⚠️There's a newer version (0.4.1) of this package.Take me there.

vegawidget

The goal of vegawidget is to render Vega-Lite and Vega specifications as htmlwidgets, and to provide you a means to communicate with a Vega chart using JavaScript or Shiny. Its ambition is to be a low-level interface to the Vega(-Lite) API, such that other packages can build upon it to offer higher-level functions to compose Vega(-Lite) specifications.

This is the key difference with the vegalite package: it provides a set of higher-level functions to compose specifications, whereas vegawidget concerns itself mainly with the rendering of the htmlwidget.

To be clear, although Vega-Lite offers a grammar-of-graphics, this package does not offer a user-friendly framework to compose graphics, like those provided by ggplot2 or ggvis. However, this package may be useful to:

  • build re-usable Vega and Vega-Lite specifications for deployment elsewhere, if you can tolerate the frustration of building specifications using lists.
  • develop higher-level, user-friendly packages to build specific types of plots, or even to build a general ggplot2-like framework, using this package as a foundation (or inspiration).

Features

New to vegawidget 0.3

  • vegawidget offers Vega-Lite 4.0.2 and Vega 5.9.0. Consequently, this package works only with browsers that support ES6. This includes all modern browsers and the 1.2 version of the RStudio IDE.

New to vegawidget 0.2

  • When knitting to a non-HTML format, e.g. github_document, this package provides a knit_print() function that will intercept the normal renderer, using instead its own renderer, allowing you to specify "png" , "svg" or "pdf". It requires that nodejs be installed on your system, as well as the R packages rsvg and png. MacOS users will require an X11 system, such as XQuartz, to be installed.

    For an example, see the GitHub rendering of this README file. For more details, please see the Render Using Vegawidget article.

  • For interactive applications, there are now functions to support vega-view’s addDataListener() function: vw_add_data_listener(), and vw_shiny_get_data().

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.

For more, please see our Getting Started article. For other introductory material, please see:

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,644

Version

0.3.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

January 22nd, 2020

Functions in vegawidget (0.3.1)

.vw_handler_body

Constructor for internal S3 class
as_vegaspec

Coerce to vegaspec
data_category

Example dataset: Categorical data
renderVegawidget

Render shiny-output for vegawidget
shiny-getters

Get information from a Vega chart into Shiny
vega_embed

Vega embed options
vw_as_json

Coerce vegaspec to JSON
vw_autosize

Autosize vegaspec
vw_handler_signal

Construct a JavaScript handler
vw_rename_datasets

Rename datasets in a vegaspec
vw_spec_version

Determine vegaspec version
vw_to_vega

Convert to Vega specification
knit_print.vegaspec

Knit-print method
vw_handler_body_compose

Compose a JavaScript handler
%>%

Pipe operator
vw_handler_add_effect

Add a side-effect to a JavaScript handler
JS

Mark character strings as literal JavaScript code
add-listeners

Add JavaScript listeners
data_seattle_daily

Example dataset: Seattle daily weather
vega_schema

Create string for schema-URL
shiny-setters

Set information in a Vega chart from Shiny
glue_js

Interpolate into a JavaScript string
vegawidget

Create a Vega/Vega-Lite htmlwidget
spec_mtcars

Example vegaspec: mtcars scatterplot
vegawidgetOutput

Shiny-output for vegawidget
image

Create or write image
data_seattle_hourly

Example dataset: Seattle hourly temperatures
vw_examine

Examine vegaspec
vega_version

Determine Vega JavaScript versions
vw_handler

Construct a vegawidget handler
vw_serialize_data

Serialize data-frame time-columns
vw_shiny_demo

Run Shiny demonstration-apps
.vw_handler_def

Constructor for internal S3 class
use_vegawidget

Add vegawidget functions to your package