knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  dev = "svg"
)

url_local <- function(x) {
  file.path("https://vegawidget.github.io/vegawidget", x)
}

CRAN status lifecycle R-CMD-check

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:

Features

New to vegawidget 0.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().

library("vegawidget")

vega_version_all()

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:

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

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.



vegawidget/vegawidget documentation built on Jan. 27, 2024, 10:48 a.m.