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: 3-25-htmlwidgets-advanced.Rmd
+1-1
Original file line number
Diff line number
Diff line change
@@ -278,7 +278,7 @@ is.null(g$dependencies)
278
278
279
279
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.
280
280
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.
Copy file name to clipboardExpand all lines: 7-01-webpack-npm-intro.Rmd
+1-1
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Combine all of the above and software that involves JavaScript can quickly becom
24
24
25
25
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.
26
26
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.
28
28
29
29
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.
Copy file name to clipboardExpand all lines: 7-02-webpack-npm-discover.Rmd
+3-3
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,7 @@ As mentioned, it is rarely a good idea to install packages globally at the excep
95
95
96
96
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.
97
97
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.
99
99
100
100
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).
101
101
@@ -117,7 +117,7 @@ Notice that this updated the `package.json` file, created the `package-lock.json
117
117
└── package.json
118
118
```
119
119
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).
121
121
122
122
```{block, type='rmdnote'}
123
123
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!
279
279
280
280
Though it is certainly not a real-world example, it is educational and quite a bit of fun.
281
281
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.
Copy file name to clipboardExpand all lines: _book/1-02-basics.md
+44-66
Original file line number
Diff line number
Diff line change
@@ -26,14 +26,10 @@ There are multiple ways to create a package. One could manually create every fil
26
26
27
27
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.
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.
Note that the function is preceded by its namespace with \texttt{::}
101
-
(more on this later).
102
-
\end{rmdnote}
95
+
<divclass="rmdnote">
96
+
<p>Note that the function is preceded by its namespace with <code>::</code> (more on this later).</p>
97
+
</div>
103
98
104
99
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.
105
100
@@ -201,11 +196,9 @@ Above we import the function `str_length` from the `stringr` package using the `
201
196
202
197
Finally, one can actually generate the `.Rd` documentation files and populate the `NAMESPACE` with either the `devtools::document()` function or `roxygen2::roxygenise()`.
203
198
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
+
<divclass="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>
209
202
210
203
### Installed files {#basics-installed-files}
211
204
@@ -233,9 +226,9 @@ Note whilst this short guide will help you develop packages good enough for your
233
226
234
227
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.
235
228
236
-
\begin{rmdnote}
237
-
``To serialise'' is just jargon for converting data to JSON.
238
-
\end{rmdnote}
229
+
<divclass="rmdnote">
230
+
<p>“To serialise” is just jargon for converting data to JSON.</p>
231
+
</div>
239
232
240
233
### Serialising {#serialising}
241
234
@@ -315,14 +308,10 @@ toJSON(df, pretty = TRUE)
315
308
316
309
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.
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.
406
391
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
+
<divclass="rmdnote">
393
+
<p>The RStudio IDE is built on Chromium, some of these tools will therefore also work in RStudio.</p>
394
+
</div>
411
395
412
396
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.
The developer tools pane consists of several tabs but we will mainly use:
424
404
@@ -501,23 +481,23 @@ function foo(){
501
481
foo();
502
482
```
503
483
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
+
<divclass="rmdnote">
485
+
<p>Accessing variables from the parent environment (context) is useful in JavaScript but should not be done in R</p>
486
+
</div>
508
487
509
488
### Document Object Model {#basics-object-model}
510
489
511
490
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.
\caption{Document Object Model visualisation}(\#fig:dom-viz)
520
-
\end{figure}
499
+
<pclass="caption">(\#fig:dom-viz)Document Object Model visualisation</p>
500
+
</div>
521
501
522
502
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.
523
503
@@ -631,10 +611,9 @@ shinyApp(ui, server)
631
611
632
612
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.
633
613
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
+
<divclass="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>
638
617
639
618
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.
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")`
755
734
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
+
<divclass="rmdnote">
736
+
<p>CDN stands for Content Delivery Network, a geographically distributed group of servers that provide fast transfer of dependencies.</p>
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.
103
95
@@ -134,14 +126,10 @@ The above is very similar to what plotly.js requires: import libraries, create a
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.
147
135
@@ -155,13 +143,14 @@ As observed, an interactive visualisation using JavaScript will be contained wit
155
143
156
144
This gives the following basic diagram; it will be broken down further in the next chapter as the first widget is built.
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