-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathintroduction_to_shiny_modules.Rmd
936 lines (687 loc) · 18.5 KB
/
introduction_to_shiny_modules.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
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
---
title: "Structure your app"
subtitle: "Introduction to Shiny modules"
author: "Jonas Hagenberg"
institute: "useR!2021"
date: "07.07.2021"
output:
xaringan::moon_reader:
css: xaringan-themer.css
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```
```{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
style_duo_accent(
primary_color = "#1a5f96",
secondary_color = "#03A696",
code_inline_background_color = "#f8f8f8"
)
```
class: inverse, center, middle
# Better shiny code
???
- who doesn't know it? your app.R files gets longer and longer; you need to
write code repeatedly; you'd like to use this nifty functionality from the one
app you've written half a year ago in your current app...
- I'll show you how to use modules to solve these problems
- first: recap of reactive programming, then introduction to modules, pass
values to modules & return values, dynamically generate modules
- please ask questions in between, you can use the feature "raise hand"
---
class: inverse, center
# Your shiny skills
<br/><br/>
<br/><br/>
<br/><br/>
beginner --------------------------------------------------------------------------------------- expert
---
# Recap: reactive programming
- usual way in `R`: procedural
```{r, eval = FALSE}
n <- 1
result <- 1 + n
n <- 2
result <- 1 + n
```
$\rightarrow$ tell what to do and when
--
- *reactive programming*: define how to do something
---
# `reactive`s
- define a rule how something is calculated
```{r, eval = FALSE}
result <- reactive({
1 + input$n
})
```
--
- one defines rules how code is executed
- updates itself when a reactive value is changed (lazy evaluation)
- can think of a homework assignment
```{r, eval = FALSE}
latest_homework <- reactive({
# do the homework on input$page
})
```
???
the teacher will tell the students on which page they have to do the
homework, the students don't have to check by themselves and won't do anything
until the teacher changes input$page
---
# `observe`/`observeEvent`
- monitor one/several reactive values
- reexecute the code when something changed
- can think of a student checking if the teacher is approaching
```{r, eval = FALSE}
observeEvent(input$lookout, {
# all students back to their place
})
```
---
# Comparison
`reactive()` | `observeEvent()`
--------------------- | ---------------------
Callable | Not callable
Returns a value | No return value
Lazy | Eager
- action with side effect: `observeEvent()`
- calculation with return value: `reactive()`
.footnote[
***
[1] introduction to reactivity: https://www.rstudio.com/resources/shiny-dev-con/reactivity-pt-1-joe-cheng/
]
???
taken from Joe Cheng's reactivity talk, still good introduction
---
# Ways to reduce code complexity
- use a style guide (e.g. tidyverse style guide)
--
- use meaningful names, e.g. `ns` < `new_students` < `table_new_students`
--
- use meaningful comments (why did you do something this way?), e.g. when you
define in `renderDT` which columns are editable by its column number, specify
the column names in the comment
--
- code reuse -> write functions
--
- document the functions -> let you help you by `roxygen2`
--
- don't put too much functionality into one function/`reactive`/`render*` call
--
- use modules
---
# What are Shiny modules?
.center[![An image of a circuit board with some electronic components and a connector on the left, as well as buttons and a display on the right.](images/complete_circuitboard.svg)]
---
## Split your code up
.center[![A diagram; on the left: a purple rectangle that stands for the app.R which is separated into ui and server areas; in the middle: an orange rectangle that stands for the module code which is separated into ui and server areas; on the right a purple rectangle that denotes the app.R with ui and server areas, with the orange module ui in the app ui area and the orange module server in the app server area](images/app_to_module.svg)]
???
- split up the code into several parts with both ui and server parts and manage
the modules from the main app
- can think of it as a "mini shiny app" with a few additional nuts and bolts to
make it work
---
## General structure
main app:
.pull-left[
```{r, eval = FALSE}
# define the UI part
ui <- fluidPage(
# other UI elements
# ...
# module UI
* module_UI(id = "id_1")
)
# define the server part
server <- function(input, output,
session) {
# other logic
# ...
# module logic
* module_server(id = "id_1")
}
shinyApp(ui, server)
```
]
---
## General structure
main app:
.pull-left[
```{r, eval = FALSE}
# define the UI part
ui <- fluidPage(
# other UI elements
# ...
# module UI
module_UI(id = "id_1")
* module_UI(id = "id_2")
)
# define the server part
server <- function(input, output,
session) {
# other logic
# ...
# module logic
module_server(id = "id_1")
* module_server(id = "id_2")
}
shinyApp(ui, server)
```
]
.pull-right[
<img src="images/several_modules.svg" width=70%>
]
???
- here you see the UI and server part of the module being called.
The code within a module can be reused by calling it several times with different IDs
- the IDs define the namespace for the module; I will talk about this in a minute
---
## Module structure
```{r, eval = FALSE}
# define the UI part
module_UI <- function(id) {
ns <- NS(id)
tagList(
# your UI elements here
)
}
# define the server part
module_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
# your logic here
}
)
}
```
???
- this is what the scaffold of a module looks like
- a `tagList` creates a HTML tag object out of other R code/elements that
constitute a HTML tag object
- is needed so that several HTML tag objects are correctly translated in HTML
from the R code
- you could also use other high level UI defining elements, e.g.
---
## Module structure
```{r, eval = FALSE}
# define the UI part
module_UI <- function(id) {
* ns <- NS(id)
tagList(
# your UI elements here
)
}
# define the server part
module_server <- function(id) {
* moduleServer(
id,
function(input, output, session) {
# your logic here
}
)
}
```
???
here you see the setup for the namespacing
---
## Namespacing - 1
- all IDs in a shiny app need to be unique
--
- use namespaces to make ID names unique for every module instance
--
- known from packages, e.g. `dplyr::filter` vs. `stats::filter`
--
- `ns <- NS(id)` generates a function that assigns a namespace for every module
instance based on its `id`
```{r, eval = FALSE}
ns <- NS("my_id")
ns("selected_col")
# my_id-selected_col
```
???
- namespace: can basically think of it as a named room that makes it possible to
reuse names, e.g. room 1 Jonas and room 2 Jonas
- now this is needed for every instance (different id) of a module
---
## Namespacing - 2
- in the `ui` part all `inputId`s/`outputId`s need to be wrapped with `ns`
```{r, eval = FALSE}
module_UI <- function(id) {
ns <- NS(id)
tagList(
actionButton(
* inputId = ns("start_calculation"),
label = "calculate"
)
)
}
```
--
- in the `server` part **no** extra work
---
class: inverse, center, middle
### Livecoding: my first Shiny app
???
- the `input` in the module is in the namespace of the module (but you don't)
have to write it
- it is different from the `input` you use in the main app
---
## Input values - 1
To input values into a module, just add arguments to the `ui` or `server` function
```{r, eval = FALSE}
calculate_UI <- function(id, button_label) { #<<
ns <- NS(id)
tagList(
actionButton(
inputId = ns("calculate"),
label = paste0("calculate ", button_label)
)
)
}
```
---
## Input values - 2
```{r, eval = FALSE}
table_server <- function(id, data) { #<<
moduleServer(
id,
function(input, output, session) {
output$table_1 <- renderTable({
data
})
}
)
}
```
--
How to pass *reactive objects* and return values see more later
???
later I will go into more detail about how to communicate with modules
---
## When to use modules?
- you need **both** the `ui` and `server` part
--
- you want to test parts of your reactivity with `testServer`
--
- if you want to encapsulate code that only lives on the `ui` side or `server`
side, simple functions are fine
???
while for `server` code, it's easier to write functions that don't have `reactive`s
as inputs/outputs, otherwise it can get a bit complicated
---
class: inverse, center, middle
### Exercise 1: write your first module
???
best way is when one person can share their screen and the group does the
exercise together
---
# Module communication
- pass `reactive`s as inputs to modules
- return `reactive`s as output of modules
---
## `reactive` inputs - 1
What's wrong with this?
```{r, eval = FALSE}
module_server <- function(id, result) {
moduleServer(
id,
function(input, output, session) {
output$text <- renderText({result})
}
)
}
server <- function(input, output, session) {
test <- reactiveVal(1)
observeEvent(input$add, {
test(test() + 1)
})
module_server(id = "my_id", result = test())
}
```
---
## `reactive` inputs - 1
What's wrong with this?
```{r, eval = FALSE}
module_server <- function(id, result) {
moduleServer(
id,
function(input, output, session) {
output$text <- renderText({result})
}
)
}
server <- function(input, output, session) {
test <- reactiveVal(1)
observeEvent(input$add, {
test(test() + 1)
})
* module_server(id = "my_id", result = test())
}
```
---
## `reactive` inputs - 2
.center[Always input **unevaluated** reactive values into modules!]
???
I can't stress this enough, if you only take one message with you from the
tutorial, then this!
---
## `reactive` inputs - 3
```{r, eval = FALSE}
server <- function(input, output, session) {
test <- reactiveVal(1)
morevals <- reactiveValues(test = 1)
module_server(id = "id_1", result = test) # works
module_server(id = "id_2", result = test()) # doesn't work
module_server(id = "id_3", result = morevals) # works
module_server(id = "id_4", result = morevals$test) # doesn't work
module_server(id = "id_5",
result = reactive({input$test})) # works
module_server(id = "id_6", result = input$test) # doesn't work
}
```
???
- reactives are just functions, and when you evaluate them they determine their
current value (therefore you need the brackets)
- this means that just input the name of the reactive without the brackets
- `input$name` is a special case
---
## `reactive` inputs - 4
You need to correctly evaluate the reactive value in the module:
```{r, eval = FALSE}
# reactiveVal
module_server <- function(id, result) {
moduleServer(
id,
function(input, output, session) {
output$text <- renderText({result()}) #<<
}
)
}
server <- function(input, output, session) {
test <- reactiveVal(1)
observeEvent(input$add, {
test(test() + 1)
})
module_server(id = "my_id", result = test) #<<
}
```
---
## `reactive` inputs - 5
You need to correctly evaluate the reactive value in the module:
```{r, eval = FALSE}
# reactiveValues
module_server <- function(id, result) {
moduleServer(
id,
function(input, output, session) {
output$text <- renderText({result$test}) #<<
}
)
}
server <- function(input, output, session) {
morevals <- reactiveValues(test = 1)
observeEvent(input$add, {
morevals$test <- morevals$test + 1
})
module_server(id = "my_id", result = morevals) #<<
}
```
---
## `reactive` inputs - 6
You need to correctly evaluate the reactive value in the module:
```{r, eval = FALSE}
# input
module_server <- function(id, result) {
moduleServer(
id,
function(input, output, session) {
output$text <- renderText({result()}) #<<
}
)
}
server <- function(input, output, session) {
module_server(id = "my_id", result = reactive({input$test})) #<<
}
```
---
class: inverse, center, middle
### Exercise 2: reactive inputs
---
## Output values from modules - 1
Use `return` in the module and assign the value in the calling environment:
```{r, eval = FALSE}
module_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
module_val <- reactiveVal(10)
# at the end of your module server code:
return(module_val) #<<
}
)
}
server <- function(input, output, session) {
res_from_module <- module_server(id = "my_id") #<<
output$text <- renderText({res_from_module()}) #<<
}
```
Again: only return **unevaluated** `reactive`s
???
- the same as for inputs: reactives needs to be unevaluated
- put the `return` at the end of your module server code
---
## Output values from modules - 2
- for more return values use `list`
- elements from `input` need to be wrapped in `reactive`
```{r, eval = FALSE}
module_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
return(
list(
a = reactive({input$a}), #<<
b = reactive({input$b}) #<<
))
}
)
}
server <- function(input, output, session) {
res_from_module <- module_server(id = "my_id") #<<
output$text <- renderText({
res_from_module$a() + res_from_module$b() #<<
})
}
```
---
class: inverse, center, middle
### Exercise 3a/b/c: reactive outputs
Choose 3a and then 3b for an introduction, choose 3c if you already feel
confident about the concepts.
---
class: inverse, center, middle
# Break
---
# Dynamically add modules
- need to add both `server` and `ui` parts
- in `observeEvent`:
- call module server function
- add module ui with `insertUI`
- `insertUI` adds a `ui` part to a certain place on the page
```{r, eval = FALSE}
server <- function(input, output, session) {
observeEvent(input$add_module, {
module_server(id = paste0("id_", input$add_module)) #<<
insertUI(
selector = "#add_here", #<<
ui = module_UI(id = paste0("id_", input$add_module)) #<<
)
})
}
```
???
- `insertUI` needs a selector where the UI gets added -> more on next slide
- the easiest part is to place a `div` with an `id` in the main app `ui` (see exercise)
---
## `insertUI`/`removeUI`
- the `selector` tells where to add the UI
- shiny uses jQuery -> jQuery selector syntax
- easiest: `#myid` where `myid` is the `id` of a HTML element, e.g. from `fluidPage`
or extra `div(id = "myid")` in the UI
- can make sense to wrap the complete module UI in a `div` instead of `tagList`:
```{r, eval = FALSE}
module_UI <- function(id) {
ns <- NS(id)
div(
id = id,
# your UI elements here
)
}
```
- `where`: where to place the UI relative to the selector
???
- tell the functions where to add the UI
- for modules I find it the easiest to define a `div` in the main app where
the modules are added
- wrap module UI in `div` so that the complete UI can be accessed by the UI
(important for removal)
- important: `removeUI` does not remove any observers registered from the module
code which can be a performance problem if too many modules were added
- would need more sophisticated methods for this, don't have time to address it
---
class: inverse, center, middle
### Exercise 4: dynamically add modules
### Exercise 5: dynamically remove modules
---
## Store outputs of generated modules
- return unevaluated reactives from module
- store them in a `reactiveValues` object (works like a list)
- evaluate every list entry
```{r, eval = FALSE}
server <- function(input, output, session) {
mod_results <- reactiveValues() #<<
observeEvent(input$add_module, {
current_id <- paste0("id_", input$add_module)
mod_results[[current_id]] <- module_server(id = current_id) #<<
insertUI(
selector = "#add_here",
ui = module_UI(id = current_id)
)
})
test <- reactive({
lapply(reactiveValuesToList(mod_results), #<<
function(current_module_output) { #<<
current_module_output() #<<
})
})
}
```
???
- I think that this solution is not optimal, but haven't found anything else
that works so far
- I have an example of a complete app in the examples folder
---
# Nesting
- you can & should nest modules
- code reuse
- modules that define bigger chunks of your app
- module UI calls within another module needs `ns`
```{r, eval = FALSE}
outer_module_UI <- function(id) {
ns <- NS(id)
tagList(
inner_module_UI(id = ns("my_id")) #<<
)
}
```
???
- nesting is good when you have some functionality you want to use in several
parts/modules
- create modules that include bigger chunks of your app (e.g. a tab/page), then
you can have other modules in this module
- SHOW example app
---
# Where does a module live?
- in `app.R` just above `ui`/`server`
- in an extra folder, `source` the scripts in the main app before calling the
main `ui`/`server` functions
- in a package
---
## API change - 1
Prior to `shiny` 1.5.0 there was a different way to write & call the `server` part
--
- current:
```{r, eval = FALSE}
module_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
output$text <- renderText({"This is a new module."})
}
)
}
server <- function(input, output, session) {
* module_server(id = "my_new_module")
}
```
---
## API change - 2
- old:
```{r, eval = FALSE}
module_server <- function(input, output, session) {
output$text <- renderText({"This is an old module."})
}
server <- function(input, output, session) {
* callModule(module_server, id = "my_old_module")
}
```
---
## `renderUI` within a module
- usually no namespaceing within the module `server` needed
- exemption: `renderUI`
- access the `ns` function from the `session` object
```{r, eval = FALSE}
module_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- session$ns #<<
output$column_UI <- renderUI({
selectInput(
inputId = ns("column"), #<<
label = "show column",
choices = colnames(mtcars)
)
})
}
)
}
```
---
# What should be a module?
--
- something you repeat often (and needs UI & server)
--
- parts where the reactivity should be tested separately
--
- large self-contained parts of an app (e.g. a tab)
--
- don't wrap single UI elements in a module
---
# Summary
- don't forget `ns` in the UI
--
- always input/output **unevaluated** reactives (if something should react)
--
- to dynamically add modules: `insertUI`/`removeUI`
---
class: inverse, middle, center
# Questions?