Skip to content

Commit e66b71d

Browse files
committed
Tutorial vignette: light editing and formatting
1 parent f5fff43 commit e66b71d

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

vignettes/intro_htmlwidgets.Rmd

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ vignette: >
1313
knitr::opts_chunk$set(eval = FALSE)
1414
```
1515

16-
The [htmlwidgets](https://www.htmlwidgets.org) 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](https://facebook.github.io/react/)-powered JavaScript library. The **reactR** package adds to 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](https://github.com/borisyankov/react-sparklines) React JavaScript library.
16+
The [htmlwidgets](https://www.htmlwidgets.org) 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](https://facebook.github.io/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](https://github.com/borisyankov/react-sparklines) React JavaScript library.
1717

1818
## Software pre-requisites
1919

@@ -39,7 +39,7 @@ install.packages(c("shiny", "devtools", "usethis", "htmlwidgets", "reactR"))
3939
To create a new widget you can call `scaffoldReactWidget` to generate the basic structure and build configuration. This function will:
4040

4141
* Create the .R, .js, .yaml, and .json files required by your widget;
42-
* If provided, take an [npm](https://www.npmjs.com/) 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.
42+
* If provided, take an [npm](https://www.npmjs.com/) package name and version as a named list with `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.
4343

4444
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:
4545

@@ -58,7 +58,7 @@ reactR::scaffoldReactWidget("sparklines", c("react-sparklines", "^1.7.0"))
5858

5959
### Building the JavaScript
6060

61-
The next step is to navigate to the `sparklines` directory in your terminal. Then, run the following commands:
61+
The next step is to navigate to the newly-created `sparklines` directory in your terminal. Then, run the following commands:
6262

6363
```{bash eval = FALSE}
6464
yarn install
@@ -73,7 +73,7 @@ yarn run webpack
7373

7474
### Installing the R package
7575

76-
Now that the widget's JavaScript is compiled, go ahead install the R package:
76+
Now that the widget's JavaScript is compiled, go ahead and install the R package:
7777

7878
```{r}
7979
devtools::document()
@@ -96,9 +96,9 @@ Alternatively, in RStudio, you can open `app.R` and press `Ctrl-Shift-Enter` (`C
9696
knitr::include_graphics('./widget_app.jpg')
9797
```
9898

99-
## Authoring a react binding
99+
## Authoring a React binding
100100

101-
At this point, we've built some scaffolding for an htmlwidget powered by react -- let's modify to create an interface to the `react-sparklines` library. Authoring the interface requires some changes on both the JavaScript and R side, but most of the hard thinking will be in figuring how best to design your interface. To give you an example of how this could work, let's build an interface to the `Sparklines` component of the 'spark-lines' library.
101+
At this point, we've built some scaffolding for an htmlwidget powered by React. Let's modify it to create an interface to the `react-sparklines` library. Authoring the interface requires some changes on both the JavaScript and R side, but most of the hard thinking will be in figuring how best to design your interface. To give you an example of how this could work, let's build an interface to the `Sparklines` component of the 'spark-lines' library.
102102

103103
### First, outline an interface
104104

@@ -109,22 +109,20 @@ import React from 'react';
109109
import { Sparklines } from 'react-sparklines';
110110

111111
<Sparklines data={sampleData}>
112-
<SparklinesLine color="#56b45d" />
113-
<SparklinesSpots style={{ fill: "#56b45d" }} />
112+
<SparklinesLine color="#56b45d" />
113+
<SparklinesSpots style={{ fill: "#56b45d" }} />
114114
</Sparklines>
115115
```
116116

117-
You have some choice in terms of how to design an R interface to this sort of react library, but usually it makes sense to have one function per component and have the arguments to that function feed into the properties of that react component. In other words, our goal is to create an R function that allows users of our package to recreate this example with the following code:
117+
You have some choice in terms of how to design an R interface to this sort of React library, but usually it makes sense to have one function per component and have the arguments to that function feed into the properties of that React component. In other words, our goal is to create an R function that allows users of our package to recreate this example with the following code:
118118

119119
```r
120120
library(sparklines)
121121
sparklines(
122-
data = sampleData,
123-
sparklinesLine(color = "#56b45d"),
124-
sparklinesSpots(style = list(fill = "#56b45d"))
122+
data = sampleData,
123+
sparklinesLine(color = "#56b45d"),
124+
sparklinesSpots(style = list(fill = "#56b45d"))
125125
)
126-
=======
127-
reactWidget('reactSparklines', 'output', SparklinesComponents, {});
128126
```
129127

130128
The following sections show how to implement this R interface from our scaffolded widget.
@@ -151,7 +149,7 @@ sparklines <- function(message, width = NULL, height = NULL, elementId = NULL) {
151149
}
152150
```
153151

154-
This function is designed to simply display a message within an HTML div using **reactR** and **htmlwidgets**. The critical piece here that makes it all work is `reactR::reactMarkup()`. This function can prepare a payload containing a mix of HTML tags (constructed via `htmltools::tag()`), React components (constructed via `reactR::component()`), or character vectors in a such way that the **reactR** and **htmlwidgets** toolchain will understand and know how to render in the browser (assuming we've imported our react component appropriately, as we cover later). Thus, to send a `<Sparklines>` react component instead of an HTML `<div>`, we could simply change:
152+
This function is designed to simply display a message within an HTML div using **reactR** and **htmlwidgets**. The critical piece here that makes it all work is `reactR::reactMarkup()`. This function can prepare a payload containing a mix of HTML tags (constructed via `htmltools::tag()`), React components (constructed via `reactR::component()`), or character vectors in a such way that the **reactR** and **htmlwidgets** toolchain will understand and know how to render in the browser (assuming we've imported our React component appropriately, as we cover later). Thus, to send a `<Sparklines>` react component instead of an HTML `<div>`, we could simply change:
155153

156154
```r
157155
content <- htmltools::tag("div", list(message))
@@ -185,7 +183,7 @@ sparklines <- function(data, ..., width = NULL, height = NULL) {
185183
}
186184
```
187185

188-
At this point, we define functions that make it easy for the user to create the other components by adding these to the `R/reactSparklines.R`
186+
At this point, we define functions that make it easy for the user to create the other components by adding these to `R/reactSparklines.R`
189187

190188
```{r}
191189
#' @export
@@ -201,15 +199,15 @@ sparklinesSpots <- function(...) {
201199

202200
### JavaScript changes
203201

204-
In order for the **reactR** toolchain to know how to render components from the 'react-sparklines' library, we need to essentially register the react components on the JavaScript side. This can be done in the `srcjs/sparklines.js` file which currently looks like this:
202+
In order for the **reactR** toolchain to know how to render components from the 'react-sparklines' library, we need to register the React components on the JavaScript side. This can be done in the `srcjs/sparklines.js` file which currently looks like this:
205203

206204
```{js}
207205
import { reactWidget } from 'reactR';
208206
209207
reactWidget('sparklines', 'output', {});
210208
```
211209

212-
First, `reactWidget` is [imported](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) from the `'reactR'` JavaScript module. This function will essentially register the react components that we want inside the **reactR** and **htmlwidgets** toolchain. Note that the `'reactR'` JS module is as an html dependency, but webpack is configured in `webpack.config.js` to consider it a module, so it's available to us here.
210+
First, `reactWidget` is [imported](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) from the `'reactR'` JavaScript module. This function will register the React components we want within the **reactR** and **htmlwidgets** toolchain. Note that the `'reactR'` JavaScript is an html dependency, but webpack is configured in `webpack.config.js` to consider it a module, so it's available to us here via `import` syntax.
213211

214212
Then, there's a call to `reactWidget`, and we pass it three arguments:
215213

@@ -232,7 +230,7 @@ reactWidget('sparklines', 'output', {
232230

233231
### Go for a spin
234232

235-
Now that we've made the necessary changes to the JavaScript and R source code, it's time to compile the JavaScript, install the R package
233+
Now that we've made the necessary changes to the JavaScript and R source code, it's time to compile the JavaScript and install the R package:
236234

237235
```bash
238236
# TODO: maybe we could provide a Makefile to compile JS and install R pkg?
@@ -245,7 +243,7 @@ This should open up the `sparklines()` widget in your browser. If it does, congr
245243

246244
### Shiny integration
247245

248-
The scaffolding template already provides the glue you need to get your **reactR** widget to render in **shiny**. The two relevant functions are `renderSparklines()` and `sparklinesOutput()`. You shouldn't need to modify these functions -- they should work out of the box. You will, however, want to modify the example **shiny** app provided by the in the `app.R` file:
246+
The scaffolding template already provides the glue you need to get your **reactR** widget to render in **shiny**. The two relevant functions are `renderSparklines()` and `sparklinesOutput()`. You shouldn't need to modify these functions -- they should work out of the box. You will, however, want to modify the example **shiny** app in the `app.R` file:
249247

250248
```{r}
251249
library(shiny)
@@ -273,4 +271,4 @@ Now, when you run `shiny::runApp()`, you should see your vision become reality!
273271

274272
## Further learning
275273

276-
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> profile, so head there if you'd like to see example of interfacing to other react libraries.
274+
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.

0 commit comments

Comments
 (0)