Skip to content

Commit f928f26

Browse files
committed
rebuilt
1 parent 674aaab commit f928f26

File tree

345 files changed

+1109
-1054
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

345 files changed

+1109
-1054
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ _book/bookdown.pdf
99
.vscode
1010
_bookdown_files
1111
bookdown.rds
12+
corrector-note.md

3-25-htmlwidgets-advanced.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ is.null(g$dependencies)
278278

279279
As shown above, the object created by `gio` includes dependencies, currently `NULL` as no such extra dependency is specified. One can therefore append those to that object in a fashion similar to what the `gio_style` function does.
280280

281-
From the root of the gio package, we create a new directory for the stats.js dependency and download the latest version from Github.
281+
From the root of the gio package, we create a new directory for the stats.js dependency and download the latest version from GitHub.
282282

283283
```r
284284
dir.create("htmlwidgets/stats")

7-01-webpack-npm-intro.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Combine all of the above and software that involves JavaScript can quickly becom
2424

2525
There are admittedly few R packages that make use of such technology though it must be said that many could greatly benefit from it. Given its size and complexity a package such as shiny, however, could probably not do without it.
2626

27-
Shiny makes use of Grunt, the source code that comprises all of the JavaScript required to run the front-end (inputs, outputs, modals, etc.) is in the `srcjs` directory which can be seen on [official Github repository](https://github.com/rstudio/shiny). This folder includes a multitude of JavaScript files the name of which indicates the code they encompass; `input_binding_checkbox.js`, `modal.js`, etc.
27+
Shiny makes use of Grunt, the source code that comprises all of the JavaScript required to run the front-end (inputs, outputs, modals, etc.) is in the `srcjs` directory which can be seen on [official GitHub repository](https://github.com/rstudio/shiny). This folder includes a multitude of JavaScript files the name of which indicates the code they encompass; `input_binding_checkbox.js`, `modal.js`, etc.
2828

2929
These files are processed by Grunt which, based on the `Gruntfile.js` configuration file in the `tools` directory, into different bundles that go in the `inst` folder of the package.
3030

7-02-webpack-npm-discover.Rmd

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ As mentioned, it is rarely a good idea to install packages globally at the excep
9595

9696
There are two other scopes on which packages can be installed. NPM allows distinguishing between packages that are needed to develop the project and packages that are needed in the final product being built.
9797

98-
R does not come with such convenience but could perhaps be useful, for instance throughout the book we used the usethis package to develop packages from setting it up to adding packages to the `DESCRIPTION` file, and more. Perhaps one would like to make this a "developer" dependency so that other developers that pull the package from Github have usethis installed and readily available. The advantage is that this dependency would not be included in the final product, that is, usethis is not required to use the package (only to develop it) and therefore is not installed by the user.
98+
R does not come with such convenience but could perhaps be useful, for instance throughout the book we used the usethis package to develop packages from setting it up to adding packages to the `DESCRIPTION` file, and more. Perhaps one would like to make this a "developer" dependency so that other developers that pull the package from GitHub have usethis installed and readily available. The advantage is that this dependency would not be included in the final product, that is, usethis is not required to use the package (only to develop it) and therefore is not installed by the user.
9999

100100
As stated in the previous chapter, file size matters in JavaScript; it is, therefore, crucial that dependencies that are used only for development are not included in the final JavaScript file. With NPM this can be done by using the `--save-dev` flag, e.g.: `npm install webpack --save-dev` to install webpack. This is how it will be eventually installed as it is needed to prepare the final product (minify, bundle, etc.) but is not required to run the bundled file(s).
101101

@@ -117,7 +117,7 @@ Notice that this updated the `package.json` file, created the `package-lock.json
117117
└── package.json
118118
```
119119

120-
The directory `node_modules` actually holds all the dependencies, it will grow in size as you add more, it's important that this directory is not pushed to whatever version control system you happen use (Github, Bitbucket, Gitlab).
120+
The directory `node_modules` actually holds all the dependencies, it will grow in size as you add more, it's important that this directory is not pushed to whatever version control system you happen use (GitHub, Bitbucket, Gitlab).
121121

122122
```{block, type='rmdnote'}
123123
Exclude the `node_modules` directory from your version control (Git or otherwise)
@@ -279,7 +279,7 @@ This is by no means a safe way to secure an application!
279279

280280
Though it is certainly not a real-world example, it is educational and quite a bit of fun.
281281

282-
The first thing to do is to install the mousetrap dependency, as indicated on the [Github README](https://github.com/ccampbell/mousetrap) it can be obtained from NPM.
282+
The first thing to do is to install the mousetrap dependency, as indicated on the [GitHub README](https://github.com/ccampbell/mousetrap) it can be obtained from NPM.
283283

284284
```bash
285285
npm install mousetrap --save

_book/1-01-intro-introduction.md

+25-30
Large diffs are not rendered by default.

_book/1-02-basics.md

+44-66
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@ There are multiple ways to create a package. One could manually create every fil
2626

2727
From the RStudio IDE go to `File > New Project > New Directory > R Package` then select "R package" and fill in the small form, namely name the package and specify the directory where it should be created.
2828

29-
\begin{figure}[H]
30-
31-
{\centering \includegraphics[width=1\linewidth]{images/rstudio-create-package}
32-
33-
}
34-
35-
\caption{Package creation wizard}(\#fig:rstudio-create-package)
36-
\end{figure}
29+
<div class="figure" style="text-align: center">
30+
<img src="images/rstudio-create-package.png" alt="Package creation wizard" width="100%" />
31+
<p class="caption">(\#fig:rstudio-create-package)Package creation wizard</p>
32+
</div>
3733

3834
But it could be argued that it's actually more accessible from the R console with the usethis package. The `create_package` function takes as first argument the path to create the package. If you run it from RStudio a new project window should open.
3935

@@ -96,10 +92,9 @@ string_length <- function(string) {
9692
}
9793
```
9894

99-
\begin{rmdnote}
100-
Note that the function is preceded by its namespace with \texttt{::}
101-
(more on this later).
102-
\end{rmdnote}
95+
<div class="rmdnote">
96+
<p>Note that the function is preceded by its namespace with <code>::</code> (more on this later).</p>
97+
</div>
10398

10499
The `DESCRIPTION` file does this, it will make sure that the dependencies of the package are met by users who install it. We can specify such dependencies under `Imports` where we can list packages required separated by a comma.
105100

@@ -201,11 +196,9 @@ Above we import the function `str_length` from the `stringr` package using the `
201196

202197
Finally, one can actually generate the `.Rd` documentation files and populate the `NAMESPACE` with either the `devtools::document()` function or `roxygen2::roxygenise()`.
203198

204-
\begin{rmdnote}
205-
Remember to run \texttt{devtools::document()} after changing roxygen2
206-
tags otherwise changes are not actually reflected in the
207-
\texttt{NAMESPACE} and documentation.
208-
\end{rmdnote}
199+
<div class="rmdnote">
200+
<p>Remember to run <code>devtools::document()</code> after changing roxygen2 tags otherwise changes are not actually reflected in the <code>NAMESPACE</code> and documentation.</p>
201+
</div>
209202

210203
### Installed files {#basics-installed-files}
211204

@@ -233,9 +226,9 @@ Note whilst this short guide will help you develop packages good enough for your
233226

234227
JSON (JavaScript Object Notation) is a prevalent data _interchange_ format with which we will work extensively throughout this book; it is thus crucial that we have a good understanding of it before we plunge into the nitty-gritty. As one might foresee, if we want two languages to work together, we must have a data format that can be understood by both---JSON lets us harmoniously pass data from one to the other. While it is natively supported in JavaScript, it can be graciously handled in R with the [jsonlite package](https://CRAN.R-project.org/package=jsonlite) [@R-jsonlite] it is the serialiser used internally by all the packages we shall explore in this book.
235228

236-
\begin{rmdnote}
237-
``To serialise'' is just jargon for converting data to JSON.
238-
\end{rmdnote}
229+
<div class="rmdnote">
230+
<p>“To serialise is just jargon for converting data to JSON.</p>
231+
</div>
239232

240233
### Serialising {#serialising}
241234

@@ -315,14 +308,10 @@ toJSON(df, pretty = TRUE)
315308

316309
What jsonlite does internally is essentially turning the data.frame into a list _rowwise_ to produce a sub-list for every row then it serialises to JSON. This is generally how rectangular data is represented in lists. For instance, `purrr::transpose` does the same. Another great example is to use `console.table` in the JavaScript console (more on that later) to display the table JSON as a table.
317310

318-
\begin{figure}[H]
319-
320-
{\centering \includegraphics[width=1\linewidth]{images/console-table}
321-
322-
}
323-
324-
\caption{console.table output}(\#fig:console-table)
325-
\end{figure}
311+
<div class="figure" style="text-align: center">
312+
<img src="images/console-table.png" alt="console.table output" width="100%" />
313+
<p class="caption">(\#fig:console-table)console.table output</p>
314+
</div>
326315

327316
We can reproduce this with the snippet below, we remove row names and use apply to turn every row into a list.
328317

@@ -391,34 +380,25 @@ The easiest way to run JavaScript interactively is probably to create an HTML fi
391380
</html>
392381
```
393382

394-
\begin{figure}[H]
395-
396-
{\centering \includegraphics[width=1\linewidth]{images/tryingjs}
397-
398-
}
399-
400-
\caption{Trying JavaScript}(\#fig:trying-js)
401-
\end{figure}
383+
<div class="figure" style="text-align: center">
384+
<img src="images/tryingjs.png" alt="Trying JavaScript" width="100%" />
385+
<p class="caption">(\#fig:trying-js)Trying JavaScript</p>
386+
</div>
402387

403388
### Developer Tools {#basics-chrome-devtools}
404389

405390
Most of the JavaScript code written in this book is intended to be run in web browsers; it is thus vital that you have a great understanding of your web browser and its developer tools (devtools). In this section, we discuss those available in Google Chrome and Chromium but such tools, albeit somewhat different, also exist in Mozilla Firefox and Safari.
406391

407-
\begin{rmdnote}
408-
The RStudio IDE is built on Chromium, some of these tools will therefore
409-
also work in RStudio.
410-
\end{rmdnote}
392+
<div class="rmdnote">
393+
<p>The RStudio IDE is built on Chromium, some of these tools will therefore also work in RStudio.</p>
394+
</div>
411395

412396
The easiest way to access the developer tools from the browser is by "inspecting": right-click on an element on a webpage and select "inspect". This will open the developer tools either at the bottom or on the right of the page.
413397

414-
\begin{figure}[H]
415-
416-
{\centering \includegraphics[width=1\linewidth]{images/devtools}
417-
418-
}
419-
420-
\caption{Google Chrome Devtools}(\#fig:chrome-devtools)
421-
\end{figure}
398+
<div class="figure" style="text-align: center">
399+
<img src="images/devtools.png" alt="Google Chrome Devtools" width="100%" />
400+
<p class="caption">(\#fig:chrome-devtools)Google Chrome Devtools</p>
401+
</div>
422402

423403
The developer tools pane consists of several tabs but we will mainly use:
424404

@@ -501,23 +481,23 @@ function foo(){
501481
foo();
502482
```
503483

504-
\begin{rmdnote}
505-
Accessing variables from the parent environment (context) is useful in
506-
JavaScript but should not be done in R
507-
\end{rmdnote}
484+
<div class="rmdnote">
485+
<p>Accessing variables from the parent environment (context) is useful in JavaScript but should not be done in R</p>
486+
</div>
508487

509488
### Document Object Model {#basics-object-model}
510489

511490
One concept which does not exist in R is that of the "DOM" which stands for Document Object Model; this is also often referred to as the DOM tree as it very much follows a tree-like structure.
512491

513-
\begin{figure}[H]
514-
515-
{\centering \includegraphics[width=1\linewidth]{images/02-dom-viz}
492+
<div class="figure" style="text-align: center">
516493

517-
}
494+
```{=html}
495+
<div id="htmlwidget-4c9f283262b9ae6159b1" style="width:100%;height:250px;" class="grViz html-widget"></div>
496+
<script type="application/json" data-for="htmlwidget-4c9f283262b9ae6159b1">{"x":{"diagram":"\ngraph {\n graph [rankdir = TD]\n\n node [shape=box]\n \"document\"\n \"<html>\"\n \"<head>\"\n \"<body>\"\n \"<title>\"\n \"<h1>\"\n \"<div>\"\n \"<a>\"\n\n \"document\" -- \"<html>\"\n \"<html>\" -- \"<head>\"\n \"<html>\" -- \"<body>\"\n \"<head>\" -- \"<title>\"\n \"<body>\" -- \"<h1>\"\n \"<body>\" -- \"<div>\"\n \"<div>\" -- \"<a>\"\n\n}\n","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]}</script>
497+
```
518498

519-
\caption{Document Object Model visualisation}(\#fig:dom-viz)
520-
\end{figure}
499+
<p class="caption">(\#fig:dom-viz)Document Object Model visualisation</p>
500+
</div>
521501

522502
When a web page is loaded, the browser creates a Document Object Model of the web page which can be accessed in JavaScript from the `document` object. This lets the developer programmatically manipulate the page itself so one can, for instance, add an element (e.g., a button), change the text of another, and plenty more.
523503

@@ -631,10 +611,9 @@ shinyApp(ui, server)
631611

632612
If you then run the application and open it at the `/files/script.js` path (e.g.: `127.0.0.1:3000/files/script.js`) you should see the content of the JavaScript file (`console.log('Hello JS!')`), commenting the `addResourcePath` line will have a "Not Found" error displayed on the page instead.
633613

634-
\begin{rmdnote}
635-
All files in your asset directory will be served online and accessible
636-
to anyone: do not place sensitive files in it.
637-
\end{rmdnote}
614+
<div class="rmdnote">
615+
<p>All files in your asset directory will be served online and accessible to anyone: do not place sensitive files in it.</p>
616+
</div>
638617

639618
Though one may create multiple such directories and correspondingly use `addResourcePath` to specify multiple paths and prefixes, one will routinely specify a single one, named "assets" or "static," which contains multiple subdirectories, one for each type of static file to obtain a directory that looks something like the tree below. This is, however, an unwritten convention which is by no means forced upon the developer: do as you wish.
640619

@@ -753,10 +732,9 @@ dependency <- htmltools::htmlDependency(
753732

754733
About the above, the `src` argument points to the directory that contains the dependencies (`script` and `stylesheet`), this is done with a named vector where `file` indicates the path is a local directory and `href` indicates it is a remote server, generally a CDN. Note that one can also pass multiple `script` and `stylesheet` by using vectors, e.g.: `c("script.js", "anotherScript.js")`
755734

756-
\begin{rmdnote}
757-
CDN stands for Content Delivery Network, a geographically distributed
758-
group of servers that provide fast transfer of dependencies.
759-
\end{rmdnote}
735+
<div class="rmdnote">
736+
<p>CDN stands for Content Delivery Network, a geographically distributed group of servers that provide fast transfer of dependencies.</p>
737+
</div>
760738

761739
```r
762740
# dependency to the latest jQuery

_book/3-20-htmlwidgets-intro.md

+36-40
Large diffs are not rendered by default.

_book/3-21-htmlwidgets-basics.md

+19-30
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ Looking at the code presented in the "Get Started" guide reveals just how conven
4747
</html>
4848
```
4949

50-
\begin{figure}[H]
51-
52-
{\centering \includegraphics[width=1\linewidth]{images/candidate-plotly}
53-
54-
}
55-
56-
\caption{Plotly example}(\#fig:candidate-plotly)
57-
\end{figure}
50+
<div class="figure" style="text-align: center">
51+
<img src="images/candidate-plotly.png" alt="Plotly example" width="100%" />
52+
<p class="caption">(\#fig:candidate-plotly)Plotly example</p>
53+
</div>
5854

5955
Now let's look at how another popular library does it.
6056

@@ -90,14 +86,10 @@ Now let's look at how another popular library does it.
9086
</html>
9187
```
9288

93-
\begin{figure}[H]
94-
95-
{\centering \includegraphics[width=1\linewidth]{images/candidate-highcharts}
96-
97-
}
98-
99-
\caption{Highcharts example}(\#fig:candidate-highcharts)
100-
\end{figure}
89+
<div class="figure" style="text-align: center">
90+
<img src="images/candidate-highcharts.png" alt="Highcharts example" width="100%" />
91+
<p class="caption">(\#fig:candidate-highcharts)Highcharts example</p>
92+
</div>
10193

10294
The above is very similar to what plotly.js requires: import libraries, create a `<div>` where to put the visualisation. Then, to create the chart, run a function which also takes the id of the div where to place said chart and a JSON of options defining the actual chart, including the data.
10395

@@ -134,14 +126,10 @@ The above is very similar to what plotly.js requires: import libraries, create a
134126
</html>
135127
```
136128

137-
\begin{figure}[H]
138-
139-
{\centering \includegraphics[width=1\linewidth]{images/candidate-chartjs}
140-
141-
}
142-
143-
\caption{Chart.js example}(\#fig:candidate-chartjs)
144-
\end{figure}
129+
<div class="figure" style="text-align: center">
130+
<img src="images/candidate-chartjs.png" alt="Chart.js example" width="100%" />
131+
<p class="caption">(\#fig:candidate-chartjs)Chart.js example</p>
132+
</div>
145133

146134
We again observe a very similar structure as with previous libraries. The library is imported, instead of a `div` chart.js uses a `canvas`, the visualisation is also created from a single function which takes the canvas as first argument and a JSON of options as second.
147135

@@ -155,13 +143,14 @@ As observed, an interactive visualisation using JavaScript will be contained wit
155143

156144
This gives the following basic diagram; it will be broken down further in the next chapter as the first widget is built.
157145

158-
\begin{figure}[H]
146+
<div class="figure" style="text-align: center">
159147

160-
{\centering \includegraphics[width=1\linewidth]{images/03-htmlwidget-viz}
161-
162-
}
148+
```{=html}
149+
<div id="htmlwidget-e4c68eb61b801bb14155" style="width:100%;height:250px;" class="grViz html-widget"></div>
150+
<script type="application/json" data-for="htmlwidget-e4c68eb61b801bb14155">{"x":{"diagram":"\ndigraph {\n graph [rankdir = LR]\n\n subgraph cluster_0 {\n node [shape=box]\n \"HTML element\"\n \"JSON\"\n \"JavaScript\"\n \"Dependencies\"\n label=\"HTML\"\n color=gold\n }\n\n subgraph cluster_1 {\n node [shape=box]\n \"Data\"\n \"Chart options\"\n label = \"R environment\"\n color=royalBlue\n }\n\n \"Data\" -> \"JSON\" \n \"Chart options\" -> \"JSON\" [label=\"serialise\" constraint=false]\n \"JSON\" -> \"JavaScript\"\n \"JavaScript\" -> \"HTML element\"\n \"Dependencies\" -> \"JavaScript\"\n}\n","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]}</script>
151+
```
163152

164-
\caption{htmlwidgets inner-workings visualised}(\#fig:widget-inner-diagram)
165-
\end{figure}
153+
<p class="caption">(\#fig:widget-inner-diagram)htmlwidgets inner-workings visualised</p>
154+
</div>
166155

167156
Thankfully the htmlwidgets package is there to handle most of this. Nonetheless, it is essential to understand that these operations are undertaken (to some degree) by htmlwidgets.

0 commit comments

Comments
 (0)