-
Notifications
You must be signed in to change notification settings - Fork 5
/
lecture_lab09.qmd
727 lines (516 loc) · 15.5 KB
/
lecture_lab09.qmd
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
---
title: "Lecture Lab 9"
author: "Leon Eyrich Jessen"
format:
revealjs:
embed-resources: true
theme: moon
slide-number: c/t
width: 1600
height: 900
mainfont: avenir
logo: images/r4bds_logo_small.png
footer: "R for Bio Data Science"
---
# Creating a Simple Shiny App
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## But... What is Shiny? ...and how does it relate to a package?
_What if you wanted to "share your code" with someone who don't know how to code? 🤔_
:::: {.columns}
::: {.column width="50%"}
![](images/logo_pkg_down_hex.png){fig-align="center"}
- A package is an ordered and documented collection of functions
:::
::: {.column width="50%"}
![](images/logo_shiny_hex.png){fig-align="center"}
- A Shiny app is an interface on top of an ordered and documented collection of functions
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## General Idea is to...
:::: {.columns}
::: {.column width="50%"}
- Shiny enables writing powerful interactive web apps in R, thereby:
- Connecting non-data literate people with data
- Automating time consuming tasks
- Exhibiting data to relevant stakeholders
- Generating value by facilitating extraction of insights
:::
::: {.column width="50%"}
<br>
<br>
<br>
![](images/shiny_data_to_action.png)
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Seeing is believing
- Let us visit [the Posit Shiny Gallery](https://shiny.posit.co/r/gallery/)
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Hello Shiny!
:::: {.columns}
::: {.column width="50%"}
```{r}
#| echo: true
#| eval: false
# Load the Shiny library
library("shiny")
# Define the User Interface (Frontend)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
# Define the Server (Backend)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
# Launch the shiny app
shinyApp(ui = ui, server = server)
```
_Note, the use of `$` is base dialect for the variable `bins` in the `input`-data_
:::
::: {.column width="50%"}
![](images/shiny_hello_shiny_example.png)
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Hello Shiny!
:::: {.columns}
::: {.column width="50%"}
```{r}
#| echo: true
#| eval: false
# Load the Shiny library
library("shiny")
# Define the User Interface (Frontend)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
# Define the Server (Backend)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
# Launch the shiny app
shinyApp(ui = ui, server = server)
```
:::
::: {.column width="50%"}
![](images/shiny_01_hello_app_anatomy.png)
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Hello Shiny!
:::: {.columns}
::: {.column width="50%"}
- Frontend, the user interface
- This is where the user interacts with your app
- Chooses arguments to function parameters
- Choices are "requests"
:::
::: {.column width="50%"}
- Backend, the server
- This is where the computations are made in response to the user's choices
- Chosen arguments are passed to function parameters, producing an output
- The output is the "response"
:::
::::
_This frontend-to-backend continuous communication is key!_
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Hello Shiny!
:::: {.columns}
::: {.column width="50%"}
- Frontend, the user interface
```{r}
#| echo: true
#| eval: false
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
```
- The `sliderInput` has a specific `inputId` set to `"bins"`
:::
::: {.column width="50%"}
- Backend, the server
```{r}
#| echo: true
#| eval: false
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
```
- `inputId` from the `sliderInput` set to `"bins"` exists in the variable `input`
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Hello Shiny!
:::: {.columns}
::: {.column width="50%"}
- Frontend, the user interface
```{r}
#| echo: true
#| eval: false
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
```
- The `plotOutput()` has a specific `outputId` where we can name the `distPlot` we defined in the `server()`-function
:::
::: {.column width="50%"}
- Backend, the server
```{r}
#| echo: true
#| eval: false
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
```
- The plot in the `server`-function is saved in the special variable `output`, which is sent back to the `ui` and can be used as `outputId`
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Defining the Input
![](images/shiny_inputs.png){fig-align="center"}
E.g. `sliderInput(inputId = "bins", ...)` in "Hello Shiny!"
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Defining the Input
![](images/shiny_inputs_w_functions.png){fig-align="center"}
E.g. `sliderInput(inputId = "bins", ...)` in "Hello Shiny!"
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Defining the Input
![](images/shiny_inputs_row1.png)
:::: {.columns}
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
actionButton(
inputId = "action",
label = "Action")
submitButton(
text = "Submit")
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
checkboxInput(
inputId = "checkbox",
label = "Choice A",
value = TRUE)
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
checkboxGroupInput(
inputId = "checkGroup",
label = "Select all that apply",
choices = list(
"Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3),
selected = 1)
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
dateInput(
inputId = "date",
label = "Select date",
value = "2014-01-01")
```
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Defining the Input
![](images/shiny_inputs_row2.png)
:::: {.columns}
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
dateRangeInput(
inputId = "dates",
label = "Select dates")
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
fileInput(
inputId = "file",
label = NULL)
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
helpText(
"Note: help text isn't a true",
"widget, but it provides an easy",
"way to add text to accompany",
"other widgets.")
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
numericInput(
inputId = "num",
label = "Input number",
value = 1)
```
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Defining the Input
![](images/shiny_inputs_row3.png)
:::: {.columns}
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
radioButtons(
inputId = "radio",
label = "Select option",
choices = list(
"Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3),
selected = 1)
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
selectInput(
inputId = "select",
label = "Select option",
choices = list(
"Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3),
selected = 1)
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
sliderInput(
inputId = "slider1",
label = "Set value",
min = 0,
max = 100,
value = 50)
sliderInput(
inputId = "slider2",
label = "Set value range",
min = 0,
max = 100,
value = c(25, 75))
```
:::
::: {.column width="25%"}
```{r}
#| echo: true
#| eval: false
textInput(
inputId = "text",
label = NULL,
value = "Enter text...")
```
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Defining the Output
| Output function | Creates |
|--------------------|-----------|
| dataTableOutput | DataTable |
| htmlOutput | raw HTML |
| imageOutput | image |
| plotOutput | plot |
| tableOutput | table |
| textOutput | text |
| uiOutput | raw HTML |
| verbatimTextOutput | text |
E.g. `plotOutput(outputId = "distPlot")` in "Hello Shiny!"
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Reactive versus Event Driven
:::: {.columns}
::: {.column width="50%"}
### Reactive
_The app is automatically updated, when a user chooses an option_
- Real-time Updates
- More simple interaction
- Improved User Experience
- Performance Overhead
- Works best with smaller data
:::
::: {.column width="50%"}
### Event-Driven
_The app is only updated, when the user clicks a button_
- Controlled updates after all choices have been made
- Gives an overall better performance
- May seem old-fashioned to some users
- Works better with larger data
:::
::::
_Today, we will work with creating a reactive app_
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## In essense...
![](images/shiny_is_like_a_pile_of_legos.png){fig-align="center"}
_Once you get your head around the syntax and functionality of the Shiny, it’s no longer about what you can build, but what you should build. Dashboards only have value, if they create value!_
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Summary
:::: {.columns}
::: {.column width="50%"}
- Shiny facilitates seamless creation of powerful interactive applications
- The analysis becomes interactive
- Provides a user interface for dynamic data interaction
- Connects non-data literate professionals directly to the data
- Fully customisable
- Allow user complete free exploration
- Or restrict to “meaningful data interaction”
- Used in production across industries!
:::
::: {.column width="50%"}
<br>
<br>
<br>
![](images/logo_shiny_hex.png){fig-align="center"}
:::
::::
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Once more unto the breach, dear friends, once more!
<!--# ---------------------------------------------------------------------- -->
<!--# SLIDE ---------------------------------------------------------------- -->
<!--# ---------------------------------------------------------------------- -->
## Once more unto the breach, dear friends, once more!
:::: {.columns}
::: {.column width="50%"}
- One last time in fact!
- Break and then the very last exercises of this year's R for Bio Data Science course!
:::
::: {.column width="50%"}
<br>
<br>
<br>
![](images/the_office_sad_day.gif){fig-align="center"}
:::
::::