Skip to content

Commit 9b8236e

Browse files
committed
WIP intro tutorial vignette
1 parent b6d2d24 commit 9b8236e

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

vignettes/intro_htmlwidgets.Rmd

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ We'll start by preparing our machine for React and reactR widget development. Th
3030

3131
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/).
3232

33-
> For an introduction to general R package concepts, please consult the excellent [R packages](http://r-pkgs.had.co.nz/) online book.
33+
> For an excellent general introduction to R package concepts, check out the [R packages](http://r-pkgs.had.co.nz/) online book.
3434
3535
In addition, you'll need to install the following JavaScript tools on your machine:
3636

@@ -43,8 +43,8 @@ Several packages must be installed to continue. You can install them like this:
4343

4444
> QA Note: The necessary version of reactR is not yet available on CRAN. Please install reactR with `devtools::install_github("react-R/reactR@enhancements")`
4545
46-
```{r}
47-
install.packages(c("shiny", "usethis", "htmlwidgets", "reactR"))
46+
```{r eval = FALSE}
47+
install.packages(c("shiny", "devtools", "usethis", "htmlwidgets", "reactR"))
4848
```
4949

5050
## Scaffolding
@@ -56,9 +56,70 @@ To create a new widget you can call `scaffoldReactWidget` to generate the basic
5656

5757
The following R code will create a package and add the `react-sparklines` dependency:
5858

59-
```{r}
60-
devtools::create("reactSparklines")
59+
```{r eval = FALSE}
60+
# Create a directory 'reactSparklines' and populate it with skeletal package
61+
# If run within RStudio, this will create a new RStudio session
62+
usethis::create_package("reactSparklines")
63+
# Set the current working directory to the one that was created unless a new
64+
# RStudio session has been launched.
6165
setwd("reactSparklines")
66+
# Generate skeletal reactR widget code and supporting build configuration
6267
reactR::scaffoldReactWidget("reactSparklines", c("react-sparklines", "^1.7.0"))
63-
devtools::install()
68+
```
69+
70+
At this point, the `reactSparklines` contains a bare-bones widget in a package called `reactSparklines`.
71+
72+
## Building and installing
73+
74+
### Building the JavaScript
75+
76+
The next step is to navigate to the `reactSparklines` directory in your terminal. Then, run the following commands:
77+
78+
```{shell}
79+
yarn install
80+
yarn run webpack --mode=development
81+
```
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**.
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+
86+
For further documentation on `yarn install`, see the [yarn documentation](https://yarnpkg.com/lang/en/docs/cli/install/).
87+
88+
`yarn run webpack` is not strictly a `yarn` command. In fact, `yarn run` simply delegates to the [webpack](https://webpack.js.org/) program. `development` is one of several [webpack modes](https://webpack.js.org/concepts/#mode). webpack's configuration is generated by `scaffoldReactWidget` in the file `webpack.config.js`.
89+
90+
### Building the package
91+
92+
While the JavaScript has been built, the `reactSparklines` package has not. Now that the JavaScript asset has been created, we can build it:
93+
94+
```{r eval = FALSE}
95+
devtools::document()
96+
devtools::install(quick = TRUE)
97+
```
98+
99+
Alternatively, in RStudio, you can use the keyboard shortcuts `Ctrl+Shift+D` and `Ctrl-Shift-B` to document and build the package. (On macOS, the shortcuts are `Cmd+Shift+D` and `Cmd+Shift+B`)
100+
101+
## Run example
102+
103+
Now that the widget's JavaScript has been compiled and the package has been installed, we can run `app.R` to see the widget in action:
104+
105+
```{r eval = FALSE}
106+
shiny::runApp()
107+
```
108+
109+
Alternatively, in RStudio, you can open `app.R` and press `Ctrl-Shift-Enter` (`Cmd-Shift-Enter` on macOS). You should see something like the following appear in the Viewer pane:
110+
111+
```{r echo=FALSE}
112+
knitr::include_graphics('./widget_app.jpg')
113+
```
114+
115+
## Implement Functionality
116+
117+
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+
119+
To expose that functionality through our widget, we must modify the JavaScript in `srcjs/reactSparklines.js`. After scaffolding, it looks like this:
120+
121+
```{js eval=FALSE}
122+
import { reactWidget } from 'reactR';
123+
124+
reactWidget('reactSparklines', 'output', {});
64125
```

vignettes/widget_app.jpg

18.7 KB
Loading

0 commit comments

Comments
 (0)