You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/intro_htmlwidgets.Rmd
+19-21Lines changed: 19 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ vignette: >
13
13
knitr::opts_chunk$set(eval = FALSE)
14
14
```
15
15
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.
To create a new widget you can call `scaffoldReactWidget` to generate the basic structure and build configuration. This function will:
40
40
41
41
* 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.
43
43
44
44
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:
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:
62
62
63
63
```{bash eval = FALSE}
64
64
yarn install
@@ -73,7 +73,7 @@ yarn run webpack
73
73
74
74
### Installing the R package
75
75
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:
77
77
78
78
```{r}
79
79
devtools::document()
@@ -96,9 +96,9 @@ Alternatively, in RStudio, you can open `app.R` and press `Ctrl-Shift-Enter` (`C
96
96
knitr::include_graphics('./widget_app.jpg')
97
97
```
98
98
99
-
## Authoring a react binding
99
+
## Authoring a React binding
100
100
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.
102
102
103
103
### First, outline an interface
104
104
@@ -109,22 +109,20 @@ import React from 'react';
109
109
import { Sparklines } from'react-sparklines';
110
110
111
111
<Sparklines data={sampleData}>
112
-
<SparklinesLine color="#56b45d"/>
113
-
<SparklinesSpots style={{ fill:"#56b45d" }} />
112
+
<SparklinesLine color="#56b45d"/>
113
+
<SparklinesSpots style={{ fill:"#56b45d" }} />
114
114
</Sparklines>
115
115
```
116
116
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:
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:
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:
205
203
206
204
```{js}
207
205
import { reactWidget } from 'reactR';
208
206
209
207
reactWidget('sparklines', 'output', {});
210
208
```
211
209
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.
213
211
214
212
Then, there's a call to `reactWidget`, and we pass it three arguments:
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:
236
234
237
235
```bash
238
236
# 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
245
243
246
244
### Shiny integration
247
245
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:
249
247
250
248
```{r}
251
249
library(shiny)
@@ -273,4 +271,4 @@ Now, when you run `shiny::runApp()`, you should see your vision become reality!
273
271
274
272
## Further learning
275
273
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