|
| 1 | +--- |
| 2 | +title: "htmlwidgets with reactR" |
| 3 | +author: "Alan Dipert" |
| 4 | +date: "`r Sys.Date()`" |
| 5 | +output: rmarkdown::html_vignette |
| 6 | +vignette: > |
| 7 | + %\VignetteIndexEntry{htmlwidgets with reactR} |
| 8 | + %\VignetteEngine{knitr::rmarkdown} |
| 9 | + %\VignetteEncoding{UTF-8} |
| 10 | +--- |
| 11 | + |
| 12 | +## Introduction to htmlwidgets with reactR |
| 13 | + |
| 14 | +The [htmlwidgets](https://www.htmlwidgets.org) package provides a framework for creating R bindings to JavaScript libraries. reactR adds to this framework by simplifying the creation of widgets that build on [React](https://facebook.github.io/react/)-based libraries or code. |
| 15 | + |
| 16 | +In particular, reactR adds: |
| 17 | + |
| 18 | +* The `reactR::scaffoldReactWidget` function for generating a skeletal React-based htmlwidget, including JavaScript build tool configuration. It is analagous to `htmlwidgets::scaffoldWidget`. |
| 19 | +* An [htmlDependency](https://shiny.rstudio.com/articles/packaging-javascript.html), returned by `reactR::html_dependency_reacttools`, that adds `window.reactR` to the browser environment. Scaffolded widgets include a reference to this htmlDependency, as well as one to `reactR::html_dependency_react` which provides React itself. |
| 20 | +* The `window.reactR.reactWidget` JavaScript function, which creates an installs a named widget backed by a set of [React Components](https://reactjs.org/docs/react-component.html). |
| 21 | +* `reactR::reactMarkup`, which prepares an `htmltools::tag` or `reactR::component` to be sent to the browser and rendered by React. |
| 22 | + |
| 23 | +## Tutorial: Adapting a JavaScript library |
| 24 | + |
| 25 | +In the following tutorial, we will build a widget around the [react-sparklines](https://reactjs.org/docs/react-component.html) React-based JavaScript library. |
| 26 | + |
| 27 | +We'll start by preparing our machine for React and reactR widget development. Then, we'll scaffold an initial `reactSparklines` widget package. Then, we'll add functionality that will make the widget easier to use. |
| 28 | + |
| 29 | +## Prepare your machine |
| 30 | + |
| 31 | +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](https://cran.r-project.org/bin/windows/Rtools/). |
| 32 | + |
| 33 | +> For an introduction to general R package concepts, please consult the excellent [R packages](http://r-pkgs.had.co.nz/) online book. |
| 34 | +
|
| 35 | +In addition, you'll need to install the following JavaScript tools on your machine: |
| 36 | + |
| 37 | +* [Node.js](https://nodejs.org): JavaScript engine and runtime for development outside of browsers. Provides the `node` and `npm` commands. |
| 38 | +* [Yarn](https://yarnpkg.com/en/): Command-line dependency management tool, provides the `yarn` command. |
| 39 | + |
| 40 | +## Install dependencies |
| 41 | + |
| 42 | +Several packages must be installed to continue. You can install them like this: |
| 43 | + |
| 44 | +> QA Note: The necessary version of reactR is not yet available on CRAN. Please install reactR with `devtools::install_github("react-R/reactR@enhancements")` |
| 45 | +
|
| 46 | +```{r} |
| 47 | +install.packages(c("shiny", "usethis", "htmlwidgets", "reactR")) |
| 48 | +``` |
| 49 | + |
| 50 | +## Scaffolding |
| 51 | + |
| 52 | +To create a new widget you can call `scaffoldReactWidget` to generate the basic structure and build configuration. This function will: |
| 53 | + |
| 54 | +* Create the .R, .js, .yaml, and .json files required by your widget; |
| 55 | +* If provided, take an [npm]() package name and version as a two-element character vector. For example, the npm package `foo` at version `^1.2.0` would be expressed as `c("foo", "^1.2.0")`. The package, if provided, will be added to the new widget's `package.json` as a build dependency. |
| 56 | + |
| 57 | +The following R code will create a package and add the `react-sparklines` dependency: |
| 58 | + |
| 59 | +```{r} |
| 60 | +devtools::create("reactSparklines") |
| 61 | +setwd("reactSparklines") |
| 62 | +reactR::scaffoldReactWidget("reactSparklines", c("react-sparklines", "^1.7.0")) |
| 63 | +devtools::install() |
| 64 | +``` |
0 commit comments