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
+31-4Lines changed: 31 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ In particular, reactR adds:
24
24
25
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
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.
27
+
We'll start by preparing our machine for React and reactR widget development. Then, we'll scaffold an initial `reactSparklines` widget package. Finally, we'll add functionality that will make the widget easier to use.
28
28
29
29
## Prepare your machine
30
30
@@ -80,7 +80,7 @@ yarn install
80
80
yarn run webpack --mode=development
81
81
```
82
82
83
-
*`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`. **You only need to run it after modifying package.json**.
83
+
*`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**.
84
84
*`yarn run webpack --mode=development` converts the [ES2015](https://babeljs.io/docs/en/learn/) JavaScript source file at `srcjs/reactSparklines.js` into `inst/htmlwidgets/reactSparklines.js`. The difference between the two is that the one in `inst/` bundles all dependencies. It is also compiled in a dialect of JavaScript that will run on a wider variety of old browsers.
85
85
86
86
For further documentation on `yarn install`, see the [yarn documentation](https://yarnpkg.com/lang/en/docs/cli/install/).
At this point, we've scaffolded a widget and built its JavaScript, but we have not implemented any functionality provided by the `react-sparklines` library.
118
120
119
-
To expose that functionality through our widget, we must modify the JavaScript in `srcjs/reactSparklines.js`. After scaffolding, it looks like this:
121
+
To expose that functionality through our widget, we must modify the JavaScript in `srcjs/reactSparklines.js`. Currently, it looks like this:
120
122
121
123
```{js eval=FALSE}
122
124
import { reactWidget } from 'reactR';
123
125
124
-
reactWidget('reactSparklines', 'output', {});
126
+
reactWidget('reactSparklines', 'output', {}, {});
125
127
```
128
+
129
+
First, `reactWidget` is [imported](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) from the module `'reactR'`. `'reactR'` is provided as an html dependency, but webpack is configured in `webpack.config.js` to consider it a module, so it's available to us here.
130
+
131
+
Then, there's a call to `reactWidget`, and we pass it four arguments:
132
+
133
+
1. The name of the widget (`'reactSparklines'`)
134
+
1. The type of the widget (`'output'`)
135
+
1. The React components that should be exposed to the widget (here, `{}`: none)
In order to implement additional functionality, we must change the third argument by adding React components provided by `react-sparklines`. We can do it like this:
139
+
140
+
141
+
```{js eval = FALSE}
142
+
import * as SparklinesComponents from 'react-sparklines';
Instead of passing an empty object as the React components, we pass an object populated with all of the components exported by the `'react-sparklines'` module as `SparklinesComponents`.
149
+
150
+
After updating your `app.R`, run `yarn run webpack --mode=development` to rebuild the JavaScript.
0 commit comments