Skip to content

Commit 1812397

Browse files
committed
Further expand htmlwidget intro vignette
1 parent 300823c commit 1812397

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

vignettes/intro_htmlwidgets.Rmd

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In particular, reactR adds:
2424

2525
In the following tutorial, we will build a widget around the [react-sparklines](https://reactjs.org/docs/react-component.html) React-based JavaScript library.
2626

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.
2828

2929
## Prepare your machine
3030

@@ -80,7 +80,7 @@ yarn install
8080
yarn run webpack --mode=development
8181
```
8282

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**.
8484
* `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.
8585

8686
For further documentation on `yarn install`, see the [yarn documentation](https://yarnpkg.com/lang/en/docs/cli/install/).
@@ -114,12 +114,39 @@ knitr::include_graphics('./widget_app.jpg')
114114

115115
## Implement Functionality
116116

117+
### JavaScript changes
118+
117119
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.
118120

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:
120122

121123
```{js eval=FALSE}
122124
import { reactWidget } from 'reactR';
123125
124-
reactWidget('reactSparklines', 'output', {});
126+
reactWidget('reactSparklines', 'output', {}, {});
125127
```
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)
136+
1. Additional configuration options (here, `{}`: none)
137+
138+
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';
143+
import { reactWidget } from 'reactR';
144+
145+
reactWidget('sparklines', 'output', SparklinesComponents, {});
146+
```
147+
148+
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.
151+
152+
### R changes

0 commit comments

Comments
 (0)