Skip to content

Commit 62d13a9

Browse files
authored
Merge pull request #10 from cpsievert/htmlwidgets-vignette2
fix R code examples in htmlwidgets tutorial vignette
2 parents 6157952 + a8bbf93 commit 62d13a9

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

vignettes/intro_htmlwidgets.Rmd

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@ To create a new widget you can call `scaffoldReactWidget` to generate the basic
4141
* Create the .R, .js, .yaml, and .json files required by your widget;
4242
* 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

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:
44+
The following R code will create an R package named **sparklines**, then provide the templating for creating an htmlwidget powered by the `react-sparklines` npm package:
4545

4646
```{r}
47-
# Set the current working directory to your home directory. The new widget will
48-
# be created in ~/sparklines
49-
setwd("~")
50-
# Create a directory 'sparklines' and populate it with skeletal package
51-
# If run within RStudio, this will create a new RStudio session
52-
usethis::create_package("sparklines")
53-
# Generate skeletal reactR widget code and supporting build configuration
54-
reactR::scaffoldReactWidget("sparklines", list(name = "react-sparklines", version = "^1.7.0"))
47+
# Create the R package
48+
usethis::create_package("~/sparklines")
49+
# Inject the widget templating
50+
withr::with_dir(
51+
"~/sparklines",
52+
reactR::scaffoldReactWidget("sparklines", list(name = "react-sparklines", version = "^1.7.0"))
53+
)
5554
```
5655

5756
## Building and installing
@@ -60,9 +59,9 @@ reactR::scaffoldReactWidget("sparklines", list(name = "react-sparklines", versio
6059

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

63-
```{bash eval = FALSE}
64-
yarn install
65-
yarn run webpack
62+
```{r}
63+
system("yarn install")
64+
system("yarn run webpack")
6665
```
6766

6867
* `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**. For further documentation on `yarn install`, see the [yarn documentation](https://yarnpkg.com/lang/en/docs/cli/install/).
@@ -232,14 +231,16 @@ reactWidget('sparklines', 'output', {
232231

233232
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:
234233

235-
```bash
236-
# TODO: maybe we could provide a Makefile to compile JS and install R pkg?
237-
yarn install
238-
yarn run webpack --mode=development
239-
Rscript -e "devtools::document(); devtools::install(); sparklines::sparklines(rnorm(10))"
234+
```{r}
235+
system("yarn install")
236+
system("yarn run webpack")
237+
devtools::document()
238+
devtools::install()
239+
library(sparklines)
240+
sparklines(rnorm(10), sparklinesLine())
240241
```
241242

242-
This should open up the `sparklines()` widget in your browser. If it does, congratulations, you just wrote your first React-based widget!
243+
This should open up the `sparklines()` widget in your browser. If it does, congratulations, you created a React-based htmlwidget!
243244

244245
### Shiny integration
245246

@@ -250,24 +251,24 @@ library(shiny)
250251
library(sparklines)
251252
252253
ui <- fluidPage(
253-
titlePanel("reactR HTMLWidget Example"),
254-
sparklinesOutput('myWidget')
254+
titlePanel("Sparklines library"),
255+
sliderInput("n", label = "Number of samples", min = 2, max = 1000, value = 100),
256+
sparklinesOutput("myWidget")
255257
)
256258
257259
server <- function(input, output, session) {
258-
output$myWidget <- renderSparklines({
259-
sparklines(
260-
data = rnorm(10),
261-
sparklinesLine(color = "#56b45d"),
262-
sparklinesSpots(style = list(fill = "#56b45d"))
263-
)
264-
})
260+
output$myWidget <- renderSparklines({
261+
sparklines(
262+
rnorm(input$n),
263+
sparklinesLine()
264+
)
265+
})
265266
}
266267
267268
shinyApp(ui, server)
268269
```
269270

270-
Now, when you run `shiny::runApp()`, you should see your vision become reality!
271+
Now, when you run `shiny::runApp()`, you should see your react-based htmlwidget rendering in **shiny** app!
271272

272273
## Further learning
273274

0 commit comments

Comments
 (0)