The htmlwidgets package provides a framework for creating R bindings to JavaScript libraries. Using the htmlwidgets package alone, it’s not necessarily straight-forward to create an R binding to a React-powered JavaScript library. The reactR package builds on the htmlwidgets framework to make it much easier to author htmlwidgets that are powered by React. This vignette will show you how to effectively leverage reactR to build an htmlwidgets package that interfaces with react-sparklines React JavaScript library.
In order to develop a reactR widget, you’ll need to install R and optionally RStudio. If you’re on Windows, you should also install Rtools.
For an excellent general introduction to R package concepts, check out the R packages online book.
In addition, you’ll need to install the following JavaScript tools on your machine:
node
and npm
commands.yarn
command.To follow along in this vignette, you’ll also need the following R packages:
To create a new widget you can call scaffoldReactWidget
to generate the basic structure and build configuration. This function will:
name
and version
elements. For example, the npm package foo
at version ^1.2.0
would be expressed as list(name = "foo", version = "^1.2.0")
. The package, if provided, will be added to the new widget’s package.json
as a build dependency.The following R code will create a package named sparklines, then provide the templating for creating an htmlwidget powered by the react-sparklines
npm package:
# Set the current working directory to your home directory. The new widget will
# be created in ~/sparklines
setwd("~")
# Create a directory 'sparklines' and populate it with skeletal package
# If run within RStudio, this will create a new RStudio session
usethis::create_package("sparklines")
# Generate skeletal reactR widget code and supporting build configuration
reactR::scaffoldReactWidget("sparklines", c("react-sparklines", "^1.7.0"))
The next step is to navigate to the newly-created sparklines
directory in your terminal. Then, run the following commands:
yarn install
downloads all of the dependencies listed in package.json
and creates a new file, yarn.lock
. You should add this file to revision control. It will be updated whenever you change dependencies and run yarn install
. Note: you only need to run it after modifying package.json. For further documentation on yarn install
, see the yarn documentation.
yarn run webpack
compiles the ES2015 JavaScript source file at srcjs/sparklines.js
into inst/htmlwidgets/sparklines.js
. The later file is one actually used by the R package and includes all the relevant JavaScript dependencies in a version of JavaScript that most browsers understand. Note that, if you add --mode=development
to the end of this command, it will include a source map is included with the compiled JavaScript, which makes JavaScript debugging much easier, but hopefully you won’t need to do much of any JavaScript debugging.
yarn run webpack
is not strictly a yarn
command. In fact, yarn run
simply delegates to the webpack program. Webpack’s configuration is generated by scaffoldReactWidget
in the file webpack.config.js
, but you can always change this configuration and/or modify the yarn run webpack
command to suit your needs.
Now that the widget’s JavaScript is compiled, go ahead and install the R package:
Alternatively, in RStudio, you can use the keyboard shortcuts Ctrl+Shift+D
and Ctrl-Shift-B
to document and build the package. (On macOS, the shortcuts are Cmd+Shift+D
and Cmd+Shift+B
)
Now that the widget’s JavaScript is compiled, and the R package is installed, run app.R
to see a demo in action:
Alternatively, in RStudio, you can open app.R
and press Ctrl-Shift-Enter
(Cmd-Shift-Enter
on macOS). You should see something like the following appear in the Viewer pane:
This tutorial walked you through the steps taken you create an R interface to the react-sparklines library. The full example package is accessible at https://github.com/react-R/sparklines-example. Our intention is keep creating example packages under the https://github.com/react-R organization, so head there if you’d like to see other examples of interfacing with React.