generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path01_shiny-and-the-web.Rmd
379 lines (272 loc) · 8.93 KB
/
01_shiny-and-the-web.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# Shiny and the Web
**Learning objectives:**
- Basic introduction to web apps and shiny
- Purpose of HTML, CSS, JavaScript
- How to create HTML in R
- How to add HTML, CSS, JavaScript to a shiny app
## Introduction {.unnumbered}
The data scientist route to web apps:
- Learn R/Python ==\> use `{shiny}` ==\> make an apps in **minutes**
The typical route
- Learn HTML ==\> learn CSS ==\> learn JS ==\> learn frontend/backend libraries
- Make web apps at each stage
## Shiny generates HTML code {.unnumbered}
```{r hello_element, results="asis"}
shiny::h1("Hello world") |> message()
```
- We just made HTML from R.
- Being able to generate HTML code from R allows the developer to **remain focused** on the main task.
Shiny apps can be deeply **customized** with
- Custom HTML (for content)
- Custom CSS (for styling)
- Custom JavaScript (for interactivitiy)
- ... and make use of many templates and packages that are already written
## Be a DJ {.unnumbered}
Here is [36 lines of shiny code](https://github.com/DivadNojnarg/OSUICode/blob/master/inst/intro/dj-system/app.R) (plus a few images and styles etc).
``` r
remotes::install_github("Athospd/wavesurfer")
OSUICode::run_example("intro/dj-system", package = "OSUICode")
```
<p style="text-align:center;">
![](image/01-shiny-and-the-web/01-dj-app.png){width="50%"}
</p>
## HTML tags basic types {.unnumbered}
HTML = "HyperText Markup Language"
- **Paired**
```
<!-- paired-tags -->
<p>A paragraph</p>
<div></div>
```
- **Self-closing**
```
<img src="url-for-the-picture.jpg" alt="description of the picture"/>
<iframe/>
<input/>
<br/>
```
## HTML tags role types {.unnumbered}
- **Structure** tags to constitute the skeleton of the HTML page
``` html
<head></head>
<title></title>
<body></body>
```
- **Control** tags to include external resources, provide interactivity with the user.
- script
- inputs
- buttons
- **Formatting** tags to change wrapped text properties like its *size* and *font*.
``` html
<b></b>
<i></i>
```
## Box Model {.unnumbered}
All elements displayed are contained by a box with properties like:
- Padding: Internal margins.
- Margins: Space between multiple elements.
- Width
- Height
<p style="text-align:center;">
![](image/01-shiny-and-the-web/03-box-model.png){width="65%"}
</p>
## Flow Layout {.unnumbered}
Defines how the elements are displayed.
| **Block elements** | **Inline elements** |
|:-----------------------------------|:-----------------------------------|
| \- Take the full width <br> - May contain other tags <br> - `<div></div>` | \- Are printed on the same line <br> - Can not contain block tags <br> - `<span></span>`, `<a></a>` |
<p style="text-align:center;">
![](image/01-shiny-and-the-web/02-normal-flow-layout.png){width="80%"}
</p>
> **Inline-block** can contain block tags in an inline.
## Semantic tags {.unnumbered}
They allow developers to **structure the HTML page**.
``` html
<header></header>
<footer></footer>
# In a basic HTML document, it will not automatically create a sidebar.
<aside></aside>
```
<p style="text-align:center;">
![](image/01-shiny-and-the-web/04-html-semantic-tags.png){width="80%"}
</p>
## Tag attributes {.unnumbered}
Add properties to your HTML elements!
- **id**: Must be unique
- **class**: May be shared by multiple elements
``` html
<div class="awesome-item" id="this-item"></div>
<span class="awesome-item"></span>
```
> Both attributes are widely used by **CSS** and **JavaScript** to apply a **custom style** to a web page.
## The simplest HTML skeleton {.unnumbered}
- `<html>` is the main wrapper
- `<head>` and `<body>` are the two main children
- `<head>` contains dependencies like styles and JavaScript files
- `<body>` contains the page content and it is displayed on the screen.
``` html
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- head content here -->
<title>A title</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
```
::: {style="border: 1px solid black;height: 100px"}
<p>Hello World</p>
:::
## The simplest Shiny skeleton {.unnumbered}
``` r
ui <- fluidPage(p("Hello, World!"))
server <- function(input, output, session) {}
shinyApp(ui, server)
```
::: {style="border: 1px solid black;height: 100px"}
<p>Hello World</p>
:::
## Document Object Model (DOM) {-}
- Convenient representation of the HTML document
- Can be viewed by opening the HTML inspector
- For selected elements we can also see the associated style (CSS) and Event Listeners (JavaScript)
<p style="text-align:center;">
![](image/01-shiny-and-the-web/05-dom.png){width="80%"}
</p>
## Web Inspector 101 {-}
Open the "Hello, World!" app in the browser and click "Inspect"
The inspector allows:
- Dynamic changes to CSS
- Debugging JS
- Run JS from a console
- Monitor warnings / errors
- Switch devices (e.g., mimic mobile)
- Do performance audit (Google LightHouse)
Exercise:
- Open "Hello, World!"
- Open the inspector
- Edit the paragraph text
- Add children to paragraph tag
## First Website {-}
- First website in August 1991
- Not Cascading Style Sheets (CSS), introduced in December 1996
<p style="text-align:center;">
![](image/01-shiny-and-the-web/06-www-first.png)
</p>
## CSS Introduction {-}
CSS allows you to **deeply customize** the appearance of any web page.
<p style="text-align:center;">
![](image/01-shiny-and-the-web/07-www-rinterface.png){width="80%"}
</p>
## HTML and CSS {-}
CSS changes the **style** of HTML tags by targeting specific **tags**, **classes** or **ids**.
if we want all p tags to have red color.
```html
<!DOCTYPE HTML>
<html lang="en">
<head>
<style type="text/css">
p {
color: red;
}
</style>
<title>A title</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
```
::: {style="border: 1px solid black;height: 100px"}
<p style="color:red;">Hello World</p>
:::
## HTML and CSS in Shiny {-}
```r
ui <- fluidPage(
tags$style("p { color: red;}"),
p("Hello World")
)
server <- function(input, output, session) {}
shinyApp(ui, server)
```
::: {style="border: 1px solid black;height: 100px"}
<p style="color:red;">Hello World</p>
:::
## HTML and JavaScript {-}
**JavaScript**
- Introduced in 1995
- Object-oriented programming (OOP)
- Allows interaction with the HTML elements
In the next example the text change to green after clicking the text.
```html
<!DOCTYPE HTML>
<html lang="en">
<head>
<style type="text/css">
p {
color: red;
}
</style>
<script language="javascript">
// displays an alert
alert('Click on the Hello World text!');
// change text color
function changeColor(color){
document.getElementById('hello').style.color = color;
}
</script>
<title>A title</title>
</head>
<body>
<!-- onclick attributes applies the JavaScript
function changeColor define above -->
<p id="hello" onclick="changeColor('green')">Hello World</p>
</body>
</html>
```
## HTML and JavaScript in Shiny {-}
```r
ui <- fluidPage(
tags$style("p { color: red;}"),
tags$script(
"alert('Click on the Hello World text!');
// change text color
function changeColor(color){
document.getElementById('hello').style.color = color;
}
"
),
p(id = "hello", onclick="changeColor('green')", "Hello World")
)
server <- function(input, output, session) {}
shinyApp(ui, server)
```
## Summary {-}
Shiny builds interactive web apps from R.
You can add custom CSS, JS and HTML to your shiny apps.
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/eabiEX8MNmQ")`
<details>
<summary>Meeting chat log</summary>
```
00:04:28 Arthur Shaw: @russ, you audio may not be hooked up yet
00:04:45 Arthur Shaw: In Zoom, we can see "Connecting to audio" under your video
00:05:09 Arthur Shaw: You may have a poor physical connection. Lots of static
00:05:27 Arthur Shaw: Same issue
00:17:47 Trevin: I'm also in the Mastering Shiny book club with Lucio 🙂
00:20:09 Federica Gazzelloni: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
00:20:57 Lucio Cornejo: This book seems more robust in the frontdns side. JavaScript for R felt mostly R like
00:21:02 Lucio Cornejo: frontend*
00:38:53 Jack Penzer: When writing custom html, the inputId in a shiny function is defined as id = in the html?
00:39:46 Lucio Cornejo: yes, it mainly copies the usual html attributes.
00:40:36 Lucio Cornejo: so something like "class = 'some-name'" also work as an argument
00:51:44 Jack Penzer: 👍
01:02:46 Lucio Cornejo: I'll be doing chapter 2
01:03:45 Trevin: Thanks for starting us off Russ!
01:03:53 Lucio Cornejo: No questions from me. Thank you Russ
01:04:12 Jack Penzer: Away skiing next week but will be back the following week!
```
</details>