-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.Rmd
164 lines (133 loc) Β· 8.37 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
title: "JavaScript for R"
author: "John Coene"
date: "`r Sys.Date()`"
documentclass: krantz
bibliography: [book.bib, packages.bib]
biblio-style: apalike
fontsize: 10pt
monofont: "Source Code Pro"
monofontoptions: "Scale=0.7"
link-citations: yes
colorlinks: yes
lot: false
lof: yes
site: bookdown::bookdown_site
description: "Invite JavaScript into your Data Science workflow."
github-repo: JohnCoene/javascript-for-r
graphics: yes
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(
comment = "",
cache = TRUE,
fig.align = "center",
# https://github.com/gadenbuie/grkstyle
tidy = 'grkstyle::grk_style_transformer',
tidy.opts=list(width.cutoff=10)
)
options(
htmltools.dir.version = FALSE,
formatR.indent = 2,
width = 55,
digits = 4,
dplyr.summarise.inform = FALSE
)
if(is_latex_output()){
opts_chunk$set(
out.width = "\\textwidth",
fig.width = 8,
fig.asp = 0.618,
fig.pos = "H"
)
}
# otherwise output in PDF blurry
library(DiagrammeRsvg)
pkgs <- c(
'htmlwidgets',
'htmltools',
'rmapshaper',
'plotly',
'waiter',
'shiny',
'drake',
'shinyjs',
'bookdown',
'V8',
'DT',
'R6',
'cli',
'vueR',
'renv',
'packrat',
'sparkline',
'devtools',
'highcharter',
'reactR',
'r2d3',
'targets', # https://github.com/wlandau/targets
'purrr',
'jsonlite',
'usethis',
'reticulate',
'rmarkdown',
'reactable',
'remotes',
'roxygen2',
'golem',
'magrittr',
'jsonify',
'ggplot2',
'echarts4r',
'testthat',
'stringr',
'crosstalk',
'shinydashboard',
'websocket',
'httpuv',
'rCharts',
'xaringan',
'packer',
'flexdashboard'
)
# install the packages needed by this book; you fill out c(), e.g. c('ggplot2', 'dplyr')
lapply(pkgs, function(pkg) {
if (system.file(package = pkg) == '') install.packages(pkg, repos = "https://cran.rstudio.com")
})
write_bib(c(.packages(), pkgs), 'packages.bib')
# fix remotes bib
bib <- readLines("packages.bib")
bib <- gsub("#readme", "", bib)
writeLines(bib, "packages.bib")
```
# Preface {-}
```{r, results='asis', echo=FALSE}
if(!is_latex_output()){
cat('_This is the online version of JavaScript for R, a book currently under development and intended for release as part of the [R series by CRC Press](https://www.routledge.com/Chapman--HallCRC-The-R-Series/book-series/CRCTHERSER)._
<div style = "float:left; width:400px; max-width:100%; margin-right:1em;">
<img src="images/cover.jpg" width = "100%">
<small><span>Photo by <a href="https://unsplash.com/@davisuko?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Cody Davis</a> on <a href="https://unsplash.com/s/photos/paint?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span></small>
</div>')
}
```
The R programming language has seen the integration of many languages; C, C++, Python, to name a few, can be seamlessly embedded into R so one can conveniently call code written in other languages from the R console. Little known to many, R works just as well with JavaScript---this book delves into the various ways both languages can work together.
The ultimate aim of this work is to demonstrate to readers the many great benefits can reap by inviting JavaScript into their data science workflow. In that respect, the book is not teaching one JavaScript but instead demonstrates how little JavaScript can significantly support and enhance R code. Therefore, the focus is on integrating external JavaScript libraries and only limited knowledge of JavaScript is required in order to learn from the book. Moreover, the book focuses on generalisable learnings so the reader can transfer takeaways from the book to solve real-world problems.
Throughout the book, several Shiny applications and R packages are put together as examples. All of these, along with the code for the entire book, can be found on the GitHub repository: [github.com/JohnCoene/javascript-for-r](https://github.com/JohnCoene/javascript-for-r).
## Premise {-}
The R programming language has been propelled into web browsers with the introduction of packages such as [Shiny](https://shiny.rstudio.com/) [@R-shiny] and [rmarkdown](https://rmarkdown.rstudio.com/) [@R-rmarkdown] which have greatly improved how R users can communicate complex insights by building interactive web applications and interactive documents. Yet most R developers are not familiar with one of web browsers' core technology: JavaScript. This book aims to remedy that by revealing how much JavaScript can greatly enhance various stages of data science pipelines from the analysis to the communication of results.
Notably, the focus of the book truly is the integration of JavaScript with R, where both languages either actively interact with one another, or where JavaScript enables doing things otherwise not accessible to R users. It is not merely about including JavaScript code that works alongside R.
## Book Structure {-}
1. The book opens with an introduction to illustrate its premise better it provides rationales for using JavaScript in conjunction with R, which it supports with existing R packages that use JavaScript and are available on CRAN. Then it briefly describes concepts essential to understanding the rest of the book to ensure the reader can follow along. Finally, this part closes by listing the various methods with which one might make JavaScript work with R.
2. We explore existing integrations of JavaScript and R namely by exploring packages to grasp how these tend to work and the interface to JavaScript they provide.
3. A sizeable part of the book concerns data visualisation it plunges into creating interactive outputs with the htmlwidgets package. This opens with a brief overview of how it works and libraries that make great candidates to integrate with the htmlwidgets package. Then a first, admittedly unimpressive, widget is built to look under the hood and observe the inner workings of such outputs to grasp a better understanding of how htmlwidgets work. Next, we tackle a more substantial library that allows drawing arcs between countries on a 3D globe, which we cover in great depth. The last two chapters go into more advanced topics, such as security and resizing.
4. The fourth part of the book details how JavaScript can work with Shiny. Once the basics are out of the way, the second chapter builds the first utility to display notifications programmatically. Then we create a Shiny application that runs an image classification algorithm in the browser. This is then followed by the creation of custom Shiny inputs and outputs. Finally, Shiny and htmlwidgets are (literally) connected by including additional functionalities in interactive visualisations when used with the Shiny framework.
5. Then the book delves into using JavaScript for computations, namely via the V8 engine and Node.js. After a short introduction, chapters will walk the reader through various examples: a fuzzy search, a time format converter, and some basic natural language operations.
6. Finally, we look at how one can use some of the more modern JavaScript technologies such as Vue\index{Vue}, React, and webpack with R---these can make the use of JavaScript more agile and robust.
7. Next the book closes with examples of all the integrations explored previously. This involves recreating (a part of) the plotly package, building an image classifier, adding progress bars to a Shiny application, building an app with HTTP cookies, and running basic machine learning operations in JavaScript.
8. Finally, the book concludes with some noteworthy remarks on where to go next.
## Acknowledgement {-}
Many people in the R community have inspired me and provided the knowledge to write this book, amongst them ultimately are [Ramnath Vaidyanathan,](https://github.com/ramnathv/) for his amazing work on the htmlwidgets [@R-htmlwidgets] package [Kent Russell,](https://github.com/timelyportfolio) from whom I have learned a lot via his work on making Vue and React accessible in R and [Carson Sievert,](https://github.com/cpsievert) for pioneering probably the most popular integration of R and JavaScript with the plotly [@R-plotly] package.
Early reviewers also shared precious feedback that helped make the book dramatically better, thanks to [Maya Gans,](@mayacelium) [
Felipe Mattioni Maturana,](@felipe_mattioni) and [Wei Su](@Wei_Su) for thoroughly going through every line of the book.
Thanks to [Cody Davis](https://www.davisuko.com/) for the cover image of the book.