The htmlwidgets package provides a framework for creating R bindings to JavaScript libraries. HTML Widgets can be:
By following a small set of easy-to-follow conventions, it is possible to create HTML widgets with very little code. All widgets include the following components:
Dependencies. These are the JavaScript and CSS assets used by the widget (e.g. the library you are creating a wrapper for).
R binding. This is the function that end users will call to provide input data to the widget as well as specify various options for how the widget should render. This also includes some short boilerplate functions required to use the widget within Shiny applications.
JavaScript binding. This is the JavaScript code that glues everything together, passing the data and options gathered in the R binding to the underlying JavaScript library.
HTML widgets are always hosted within an R package and should include all of the source code for their dependencies. This is to ensure that code which depends on widgets is fully reproducible (i.e. doesn’t require an internet connection or the ongoing availability of an internet service to run).
To start with we’ll walk through the creation of a simple widget that wraps the sigma.js graph visualization library. When we’re done we’ll be able to use it to display interactive visualizations of GEXF (Graph Exchange XML Format) data files. For example:
library(sigma)
data <- system.file("examples/ediaspora.gexf.xml", package = "sigma")
sigma(data)