-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_data.qmd
920 lines (670 loc) · 26.3 KB
/
app_data.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
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
# App data {#sec-app-data}
<!--
https://shiny.posit.co/r/articles/improve/debugging/
https://debruine.github.io/shinyintro/debugging.html
-->
```{r}
#| label: _common
#| eval: true
#| echo: false
#| include: false
source("_common.R")
library(lobstr)
library(shiny)
```
This chapter covers using `session$userData` vs. `reactiveValues()` in an application to store values and objects. Whether to use `session$userData` or `reactiveValues()` will depend on the purpose you want them to serve (and what you want to store/access) in your application.
:::: {.callout-tip collapse='true' appearance='default'}
## [Accessing applications]{style='font-weight: bold; font-size: 1.15em;'}
::: {style='font-size: 0.95em; color: #282b2d;'}
I've created the [`shinypak` R package](https://mjfrigaard.github.io/shinypak/) In an effort to make each section accessible and easy to follow:
Install `shinypak` using `pak` (or `remotes`):
```{r}
#| code-fold: false
#| message: false
#| warning: false
#| eval: false
# install.packages('pak')
pak::pak('mjfrigaard/shinypak')
```
Review the chapters in each section:
```{r}
#| code-fold: false
#| message: false
#| warning: false
#| collapse: true
library(shinypak)
list_apps(regex = '^26')
```
Launch the app:
```{r}
#| code-fold: false
#| eval: false
launch(app = "26.1.0_reactive-values")
```
Download the app:
```{r}
#| code-fold: false
#| eval: false
get_app(app = "26.1.0_reactive-values")
```
:::
::::
## `reactiveValues()` {#sec-reactiveValues}
```{r}
#| label: git_box_26.1.0_reactive-values
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.1.0_reactive-values",
repo = 'sap')
```
Calling `reactiveValues()` creates *"an object for storing reactive values."* We've been storing the reactive values returned from the `var_input` module in the `selected_vars` object, then passing these values into the `scatter_display` module. [^reactive-values-intro]
[^reactive-values-intro]: Mastering Shiny also has a [great section](https://mastering-shiny.org/reactivity-objects.html#reactive-values) on `reactiveVal()` and `reactiveValues()`
```{r}
#| eval: false
#| code-fold: false
movies_server <- function(input, output, session) {
selected_vars <- mod_var_input_server("vars") # <1>
mod_scatter_display_server("plot", var_inputs = selected_vars) # <2>
}
```
1. reactive values returned from `var_input` module
2. reactive values passed to `scatter_display` module
In the steps below we'll walk through an example of using `reactiveValues()` to capture the `selected_vars` values returned from the `var_input` module and passed to the `scatter_display` module.
You should note a series of changes made to `movies_server()` in this branch:
- `reactiveValues()` is used to create `rVals`
- Instead of creating the `selected_vars`, the output from `mod_var_input_server()` is assigned to `rVals` as `inputs`
- `rVals` is then passed as an input argument to `mod_scatter_display_server()`
- `.dev` arguments have been added to both module server functions (and have been set to `FALSE`)
```{r}
#| eval: false
#| code-fold: false
# assign inputs to rVals
movies_server <- function(input, output, session) {
# create reactive values
rVals <- reactiveValues() # <1>
# assign inputs to rVals
rVals$inputs <- mod_var_input_server("vars", .dev = FALSE) # <2>
# view output in the UI
output$vals <- renderPrint({ # <3>
}) # <3>
# pass reactive values to display
mod_scatter_display_server("plot", rVals = rVals, .dev = FALSE) # <4>
}
```
1. New `reactiveValues()` object
2. Returned values from `mod_var_input_server()` assigned to `rVals$inputs`
3. `renderPrint()` for displaying reactive values in UI
4. `rVals` object passed to `mod_scatter_display_server()`
In the steps below, we'll view the structure and function of `rVals` and the reactive values in the application using methods covered in the [Debug section](debug.qmd).
### Step 1
In `movies_server()`, the `.dev` in `mod_var_input_server()` is set to `TRUE` (the updated `movies_server()` function is below):
```{r}
#| label: git_box_26.1.1_step_01
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.1.1_step_01",
repo = 'sap')
```
```{r}
#| eval: false
#| code-fold: false
# assign inputs to rVals
movies_server <- function(input, output, session) {
# create reactive values
rVals <- reactiveValues()
# assign inputs to rVals
rVals$inputs <- mod_var_input_server("vars", .dev = TRUE) # <1>
# pass reactive values to display
mod_scatter_display_server("plot", rVals = rVals, .dev = FALSE)
}
```
1. `.dev` has been set to `TRUE`
When we load the package and run the application, we see the following:[^reactives-debugging-ref]
[^reactives-debugging-ref]: The methods used in this chapter can be found in the section on [Debug](debug.qmd)
::::{.column-body-outset-right}
:::{#fig-reactive_values_input_dev}
![`.dev = TRUE` in `mod_var_input_server()`](images/reactive_values_input_dev.png){#fig-reactive_values_input_dev width='100%' align='center'}
reactive values from `mod_var_input_server()`
:::
::::
The output in the sidebar are the reactive values from the variable input module (`mod_var_input_server()`). The server function has been simplified to return the output from `reactiveValuesToList()`, and the output is being rendered in the sidebar when `.dev` is set to `TRUE`:
```{r}
#| eval: false
#| code-fold: false
mod_var_input_server <- function(id, .dev = TRUE) {
moduleServer(id, function(input, output, session) {
if (.dev) {
# view output in the UI
output$vals <- renderPrint({
x <- reactiveValuesToList(input, all.names = TRUE)
str(x)
})
}
# return reactives
return(
reactive({
reactiveValuesToList(input, all.names = TRUE)
})
)
})
}
```
### Step 2
The `renderPrint()` in `movies_server()` displays the structure of `rVals` in the `mainPanel()` (the updated `movies_server()` function is below):
```{r}
#| label: git_box_26.1.2_step_02
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.1.2_step_02",
repo = 'sap')
```
```{r}
#| eval: false
#| code-fold: false
# assign inputs to rVals
movies_server <- function(input, output, session) {
# create reactive values
rVals <- reactiveValues()
# assign inputs to rVals
rVals$inputs <- mod_var_input_server("vars", .dev = TRUE) # <1>
# view output in the UI
output$vals <- renderPrint({ # <2>
str(rVals)
}) # <2>
# pass reactive values to display
mod_scatter_display_server("plot", rVals = rVals, .dev = FALSE)
}
```
1. Set `.dev` to `TRUE`
2. Display output from `str(rVals)`
::::{.column-body-outset-right}
:::{#fig-reactive_values_input_server_dev_01}
![`str(rVals) `from `movies_server()`](images/reactive_values_input_server_dev_01.png){#fig-reactive_values_input_server_dev_01 width='100%' align='center'}
`rVals$inputs()` from `movies_server()`
:::
::::
#### What is `reactiveValues()`?
> *"When you read a value from it, the calling reactive expression takes a reactive dependency on that value, and when you write to it, it notifies any reactive functions that depend on that value. **Note that values taken from the `reactiveValues()` object are reactive, but the `reactiveValues()` object itself is not**."* [Shiny Documentation](https://shiny.posit.co/r/reference/shiny/1.7.2/session.html)
I've added emphasis to the quote above because it's important to remember that any object assign to `reactiveValue()` should be treated like any reactive object (i.e., and `inputId` or object returned from `reactive()` or `observe()`).[^reactives-shiny-doc-reactive-values]
[^reactives-shiny-doc-reactive-values]: Read more in the [Shiny documentation.](https://shiny.posit.co/r/reference/shiny/0.11/reactivevalues)
For example, if we try to access the input values as a list outside `movies_server()` or the module server function, we see the following error:[^reactives-isolate]
[^reactives-isolate]: We *can* access the values by wrapping the assigned object in `isolate()`. Read more in the [documentation on `reactiveValues()`](https://shiny.posit.co/r/reference/shiny/1.7.2/session.html).
```{r}
#| eval: false
#| error: true
#| code-fold: false
#| collapse: true
x <- reactiveValues(
inputs = list(x = "imdb_rating",
y = "audience_score",
z = "mpaa_rating",
alpha = 0.5,
size = 2,
plot_title = "Enter Plot Title")
)
x$inputs()
```
```{r}
#| eval: false
#| code-fold: false
## Error in `x$inputs`:
## ! Can't access reactive value 'inputs' outside of reactive consumer.
## ℹ Do you need to wrap inside reactive() or observe()?
```
### Step 3
In this branch, the `renderPrint()` displays the structure of `rVals$inputs()` (the updated `movies_server()` function is below):
```{r}
#| label: git_box_26.1.3_step_03
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.1.3_step_03",
repo = 'sap')
```
```{r}
#| eval: false
#| code-fold: false
# assign inputs to rVals
movies_server <- function(input, output, session) {
# create reactive values
rVals <- reactiveValues()
# assign inputs to rVals
rVals$inputs <- mod_var_input_server("vars", .dev = TRUE) # <1>
# view output in the UI
output$vals <- renderPrint({ # <2>
str(rVals$inputs())
}) # <2>
# pass reactive values to display
mod_scatter_display_server("plot", rVals = rVals, .dev = FALSE)
}
```
1. Set `.dev` to `TRUE`
2. Display `str(rVals$inputs())`
::::{.column-body-outset-right}
:::{#fig-reactive_values_input_server_dev_02}
![`str(rVals) `from `movies_server()`](images/reactive_values_input_server_dev_02.png){#fig-reactive_values_input_server_dev_02 width='100%' align='center'}
`rVals$inputs()` from `movies_server()`
:::
::::
The `rVals$inputs()` being rendered in `movies_server()` are the returned values from the variable input module (and they're identical to the values in the sidebar).
When `rVals` is passed to `mod_scatter_display_server()`, the reactive `inputs()` object (passed inside the function) is built as `rVals$inputs()`:
```{r}
#| eval: false
#| code-fold: false
inputs <- reactive({
plot_title <- tools::toTitleCase(rVals$inputs()[['plot_title']])
list(
x = rVals$inputs()[['x']],
y = rVals$inputs()[['y']],
z = rVals$inputs()[['z']],
alpha = rVals$inputs()[['alpha']],
size = rVals$inputs()[['size']],
plot_title = plot_title
)
})
```
The `mod_scatter_display_server()` function will display the structure of `rVals$inputs()` if the `.dev` argument is set to `TRUE`:
```{r}
#| eval: false
#| code-fold: false
if (.dev) {
# view output in the UI
output$display_vals <- renderPrint({
str(
rVals$inputs()
)
})
}
```
### Step 4
In this final step, the `.dev` argument has been set to `TRUE` in the `mod_scatter_display_server()` (the updates `movies_server()` function is below):
```{r}
#| label: git_box_26.1.4_step_04
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.1.4_step_04",
repo = 'sap')
```
```{r}
#| eval: false
#| code-fold: false
# assign inputs to rVals
movies_server <- function(input, output, session) {
# create reactive values
rVals <- reactiveValues()
# assign inputs to rVals
rVals$inputs <- mod_var_input_server("vars", .dev = TRUE) # <1>
# view output in the UI
output$vals <- renderPrint({ # <2>
rVals$inputs()
}) # <2>
# pass reactive values to display
mod_scatter_display_server("plot", rVals = rVals, .dev = TRUE) # <3>
}
```
1. Set `.dev` to `TRUE`
2. Display `rVals$inputs()`
3. Set `.dev` to `TRUE`
::::{.column-body-outset-right}
:::{#fig-reactive_values_input_server_dev}
![`.dev` set to `TRUE` and reactive values from `movies_server()`](images/reactive_values_all_dev.png){#fig-reactive_values_input_server_dev width='100%' align='center'}
Both module `.dev` arguments set to `TRUE` and `rVals$inputs()` from `movies_server()`
:::
::::
The display we're seeing directly below the graph (i.e., under **`inputs()`** **`from`** **`display`** **`module`**) is the structure of `rVals$inputs()` from `mod_scatter_display_server()`:
```{r}
#| eval: false
#| code-fold: false
if (.dev) {
# view output in the UI
output$display_vals <- renderPrint({
str(
rVals$inputs()
)
})
}
```
An important thing to note is that we can only reference `rVals$inputs()` in a **reactive consumer** (i.e., using `reactive()`, `observe()`, etc.). That's why when we change any of the UI inputs, the values change in `rVals$inputs()` and in the `inputs()` object inside the display module.
You can also view these outputs using `launch_app(run = 'b', bslib = TRUE)`.
### Testing `reactiveValues()` {#sec-testing-reactive-values}
If you decide to use `reactiveValues()` or `session$userData`, you'll need to confirm these objects in your tests. The module tests for `test-mod_scatter_display.R` have been redesigned to handle the `reactiveValues()` input.[^view-reactive-values-test]
[^view-reactive-values-test]: You can view the full `test-mod_scatter_display.R` test file in the [`24.1.4_step_04`](https://github.com/mjfrigaard/sap/tree/24.1.4_step_04) branch.
I'll briefly summarize the changes below:
- `args = list()` in `testServer()` now takes the output from helper functions (stored in `tests/testthat/helper.R` and `R/testthat.R`). [^reactives-test-helpers]
```{r}
#| eval: true
#| code-fold: false
make_initial_rVals_inputs <- function() {
rVals <- reactiveValues(
inputs =
reactive(list(
x = "imdb_rating",
y = "audience_score",
z = "mpaa_rating",
alpha = 0.5,
size = 2,
plot_title = "Enter Plot Title"
))
)
return(rVals)
}
```
- This creates a `reaciveValues()` list that can be used in the test:
```{r}
#| eval: true
#| code-fold: false
#| collapse: true
rVals <- make_initial_rVals_inputs()
rVals
```
- We can view it's contents by wrapping it in `isolate()`.
```{r}
#| eval: true
#| code-fold: false
#| collapse: true
isolate(rVals$inputs())
```
- Passing `make_initial_rVals_inputs()` to the `args` in `testServer()` requires creating `rVals` (which can be referenced in the test as `rVals$inputs()`:
```{r}
#| eval: false
#| code-fold: false
shiny::testServer(app = mod_scatter_display_server,
args = list(rVals = make_initial_rVals_inputs()), expr = {
testthat::expect_equal(
object = rVals$inputs(),
expected = list(
x = "imdb_rating",
y = "audience_score",
z = "mpaa_rating",
alpha = 0.5,
size = 2,
plot_title = "Enter Plot Title"
)
)
})
```
[^reactives-test-helpers]: I resorted to both locations because the `tests/testthat/helper.R` file wasn't loading with `devtools::load_all()`
## `session$userData` {#sec-session-user-data}
Objects stored in `session$userData` are not inherently reactive, which makes it ideal for storing persistent values or data that don't require (or trigger) reactivity. Below is a demonstration of using `session$userData` to store a non-reactive function to be used in the `inst/tidy-data/` application.
```{r}
#| label: git_box_26.2.0_user-data
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.2.0_user-data",
repo = 'sap')
```
#### Non-reactive objects
Objects we want to pass *inside* the server (and modules) but don't need to update or change are perfect for `session$userData`. The example we'll use below is a function (`make_dev_ggp2_movies()`) that prepares the `ggplot2movies::movies` for the application:
```{r}
#| eval: false
#| code-fold: true
#| code-summary: 'View make_dev_ggp2_movies() function'
make_dev_ggp2_movies <- function(con) {
movies_data <- read.csv(file = con)
# specify genre columns
genre_cols <- c(
"Action", "Animation",
"Comedy", "Drama",
"Documentary", "Romance",
"Short"
)
# calculate row sum for genres
movies_data$genre_count <- rowSums(movies_data[, genre_cols])
# create aggregate 'genres' for multiple categories
movies_data$genres <- apply(
X = movies_data[, genre_cols],
MARGIN = 1,
FUN = function(row) {
genres <- names(row[row == 1])
if (length(genres) > 0) {
return(paste(genres, collapse = ", "))
} else {
return(NA)
}
}
)
# format variables
movies_data$genre_count <- as.integer(movies_data$genre_count)
movies_data$genre <- ifelse(test = movies_data$genre_count > 1,
yes = "Multiple genres",
no = movies_data$genres
)
movies_data$genre <- as.factor(movies_data$genre)
movies_data$mpaa <- factor(movies_data$mpaa,
levels = c("G", "PG", "PG-13", "R", "NC-17"),
labels = c("G", "PG", "PG-13", "R", "NC-17")
)
# reduce columns to only those in graph
movies_data[, c(
"title", "year", "length", "budget",
"rating", "votes", "mpaa", "genre_count",
"genres", "genre"
)]
}
```
`make_dev_ggp2_movies()` is designed to take a path or URL (i.e., a `con`nection) as an input and returns a dataset that can be used in the `inst/tidy-data/` application.
In the `inst/tidy-data/app.R` file, the following changes have been made to `devServer()`:
- `session$userData` stores the contents of `make_dev_ggp2_movies()`
- `reactiveValues()` is used to create `rVals` [^userdata-rVals]
- The values returned from `mod_var_input_server()` is assigned to `rVals` as `inputs`
- `dev_mod_scatter_server()` as been updated to include arguments for `rVals`, `userData`, `con`, and `.dev`
[^userdata-rVals]: We'll cover how `reactiveValues()` works in @sec-reactiveValues below.
```{r}
#| eval: false
#| code-fold: false
devServer <- function(input, output, session) {
session$userData$make_dev_ggp2_movies <- make_dev_ggp2_movies # <1>
rVals <- reactiveValues() # <2>
rVals$inputs <- sap::mod_var_input_server("vars", # <3>
.dev = TRUE) # <3>
dev_mod_scatter_server("plot", # <4>
rVals = rVals,
data_fun = session$userData$make_dev_ggp2_movies,
con = "https://bit.ly/3FQYR8j",
.dev = FALSE
) # <4>
}
```
1. Create `userData$make_dev_ggp2_movies` that holds `make_dev_ggp2_movies()`
2. Create `rVals`
3. Assign output from `mod_var_input_server()` to `rVals$inputs`
4. Updated `dev_mod_scatter_server()` function
To view what's happening with `session$userData`, we'll run the application using the **Run App** button at the top of `app.R`
:::{.column-margin}
![](images/Run_App.png){width='50%' fig-align='right'}
:::
::::{.column-body-outset-right}
:::{#fig-reactive_values_dev_step1}
![Initial app in `dev/inst/app.R`](images/reactive_values_dev_step1.png){#fig-reactive_values_dev_step1 width='100%' align='center'}
The reactive values from `mod_var_input_server()` in the sidebar
:::
::::
We're using the same version of `mod_var_input_server()` from above that includes a `.dev` argument, so we know it's displaying the contents from `reactiveValuesToList()` in the sidebar.
### Step 1
In `devServer()`, a `renderPrint()` call has been added to display the structure of `session` in the UI:
```{r}
#| label: git_box_26.2.1_step_01
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.2.1_step_01",
repo = 'sap')
```
```{r}
#| eval: false
#| code-fold: false
devServer <- function(input, output, session) {
# add function to userData
session$userData$make_dev_ggp2_movies <- make_dev_ggp2_movies # <1>
# create reactive values
rVals <- reactiveValues() # <2>
# assign inputs to rVals
rVals$inputs <- sap::mod_var_input_server("vars", # <3>
.dev = TRUE) # <3>
# view output in the UI
output$vals <- renderPrint({ # <4>
str(session)
}) # <4>
dev_mod_scatter_server("plot", # <5>
rVals = rVals,
data_fun = session$userData$make_dev_ggp2_movies,
con = "https://bit.ly/3FQYR8j",
.dev = FALSE
) # <5>
}
```
1. Create `userData$make_dev_ggp2_movies` that holds `make_dev_ggp2_movies()`
2. Create `rVals`
3. Assign output from `mod_var_input_server()` to `rVals$inputs`
4. Print the structure of `session` to UI
5. Updated `dev_mod_scatter_server()` function
::::{.column-body-outset-right}
:::{#fig-reactive_values_dev_step3}
![`str(session)` `dev/inst/app.R`](images/reactive_values_dev_step3.png){#fig-reactive_values_dev_step3 width='100%' align='center'}
The `str(session)` from `devServer()`
:::
::::
#### What is `session`?
Each time the app launches, the `session` list is created and tied to that particular 'session.
> *"An environment for app authors and module/package authors to store whatever session-specific data they want."* [Shiny Documentation](https://shiny.posit.co/r/reference/shiny/1.7.2/session.html)
`session$userData` can store objects that should persist across different reactive contexts, but don't need reactive updating (and won't trigger reactivity). On the other hand, `reactiveValues()` creates objects stored in a reactive 'state', which will trigger reactive updates in the UI.'[^session-r6]
[^session-r6]: Notice `session` has `:Classes 'ShinySession', 'R6'`
`dev_mod_scatter_server()` includes both `reactiveValues()` and `session$userData`. The arguments for `rVals`, `data_fun`, `con`, and `.dev` are described below:
- `rVals` is the `reactiveValues()` object with our input values
- `data_fun` is `session$userData$make_dev_ggp2_movies`
- `con` is the path or URL to the `data_fun` in `session$userData` [^userdata-con]
[^userdata-con]: In this case, `con` is a URL for a .csv version of `ggplot2movies::movies`)
```{r}
#| eval: false
#| code-fold: false
dev_mod_scatter_server("plot",
rVals = rVals, # <1>
data_fun = session$userData$make_dev_ggp2_movies, # <2>
con = "https://bit.ly/3FQYR8j", # <3>
.dev = FALSE) # <4>
```
1. pass reactive values from `reactiveValues()`
2. pass `session$userData` with `make_dev_ggp2_movies()`
3. pass connection to non-reactive object
4. view `userData` value in module
Inside the display module (`dev_mod_scatter_server()`), the `data_fun()` function creates `all_data` with `con`:
```{r}
#| eval: false
#| code-fold: false
# use data_fun() function on con
all_data <- data_fun(con)
```
- The `inputs()` list passed to the plotting function is very similar to the methods used in `mod_scatter_display_server()`:
```{r}
#| eval: false
#| code-fold: false
inputs <- reactive({
plot_title <- tools::toTitleCase(rVals$inputs()[["plot_title"]])
list(
x = rVals$inputs()[["x"]],
y = rVals$inputs()[["y"]],
z = rVals$inputs()[["z"]],
alpha = rVals$inputs()[["alpha"]],
size = rVals$inputs()[["size"]],
plot_title = plot_title
)
})
```
- The structure of `data_fun` is be printed to the UI when the `.dev` argument is set to `TRUE`
```{r}
#| eval: false
#| code-fold: false
# view output in the UI
if (.dev) {
# view output in the UI
output$data <- renderPrint({
data_fun
})
}
```
### Step 2
Change `.dev` argument in `dev_mod_scatter_server()` to `TRUE`:
```{r}
#| label: git_box_26.2.2_step_02
#| echo: false
#| results: asis
#| eval: true
git_margin_box(
contents = "launch",
fig_pw = '65%',
branch = "26.2.2_step_02",
repo = 'sap')
```
```{r}
#| eval: false
#| code-fold: false
dev_mod_scatter_server("plot",
rVals = rVals,
data_fun = session$userData$make_dev_ggp2_movies,
con = "https://bit.ly/3FQYR8j",
.dev = TRUE # <1>
)
```
1. Change to `TRUE`
Load (with `load_all()`) and the app by clicking on the **Run App** icon:
::::{.column-body-outset-right}
:::{#fig-reactive_values_dev_step4}
![`data_fun` `dev_mod_scatter_server()`](images/reactive_values_dev_step4.png){#fig-reactive_values_dev_step4 width='100%' align='center'}
The `data_fun` argument from `dev_mod_scatter_server()` is not reactive
:::
::::
We can see `data_fun()` is passed to `dev_mod_scatter_server()` from `devServer()` and is not a reactive (it's a standard function we can apply to `app_data`).
Tests are more difficult for modules using `session$userData`, because these values are created when a Shiny app object is created (and exist inside the reactive context). This point is covered in more detail in [this blog post](https://appsilon.com/super-solutions-for-shiny-architecture-1-of-5-using-session-data/).
> *"reusing objects passed through `session` violates module independence – there is code inside the module that uses external objects without stating them explicitly as server arguments."*
## Recap {.unnumbered}
```{r}
#| label: co_box_recap_reactiveValues
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "g",
look = "default", size = '1.05', hsize = '1.10',
header = "Recap: reactiveValues() & session$userData",
contents = "
[`session$userData`]{style='font-size: 1.30em; font-weight: bold;'}
- `session$userData` is best used with values or objects that persist across actions or navigation inside the app (i.e., maintaining values or data across pages of a multi-page app). `session$userData` can react to changes, but we'd need to explicitly create these reactive expressions or observers.
[`reactiveValues()`]{style='font-size: 1.30em; font-weight: bold;'}
- An object created with `reactiveValues()` is designed to be reactive, so changing values will trigger reactivity in any observers and/or reactives that depend on those values. Remember that '*values taken from the `reactiveValues()` object are reactive, but the `reactiveValues()` object itself is not.*'
",
fold = FALSE
)
```
```{r}
#| label: git_contrib_box
#| echo: false
#| results: asis
#| eval: true
git_contrib_box()
```