-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmastering-shiny-book-exercises-solutions-book.html
1846 lines (1821 loc) · 216 KB
/
mastering-shiny-book-exercises-solutions-book.html
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Mastering Shiny Solutions</title>
<meta name="author" content="Howard Baek" />
<meta name="description" content="<p>Solutions manual to the exercises in Hadley Wickham’s Mastering Shiny.</p>" />
<meta name="generator" content="placeholder" />
<meta property="og:title" content="Mastering Shiny Solutions" />
<meta property="og:type" content="book" />
<meta property="og:url" content="<a href="https://mastering-shiny-solutions.netlify.app/" class="uri">https://mastering-shiny-solutions.netlify.app/</a>" />
<meta property="og:image" content="<a href="https://mastering-shiny-solutions.netlify.app/" class="uri">https://mastering-shiny-solutions.netlify.app/</a>/img/mastering-shiny-cover.png" />
<meta property="og:description" content="<p>Solutions manual to the exercises in Hadley Wickham’s Mastering Shiny.</p>" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Mastering Shiny Solutions" />
<meta name="twitter:description" content="<p>Solutions manual to the exercises in Hadley Wickham’s Mastering Shiny.</p>" />
<meta name="twitter:image" content="<a href="https://mastering-shiny-solutions.netlify.app/" class="uri">https://mastering-shiny-solutions.netlify.app/</a>/img/mastering-shiny-cover.png" />
<!-- JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script>
<script src="libs/header-attrs-2.11/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet" />
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script>
<script src="libs/bs3compat-0.3.1/transition.js"></script>
<script src="libs/bs3compat-0.3.1/tabs.js"></script>
<script src="libs/bs3compat-0.3.1/bs3compat.js"></script>
<link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet" />
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script>
<!-- CSS -->
<link rel="stylesheet" href="bs4_style.css" />
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container-fluid">
<!--bookdown:title:start-->
<!--bookdown:title:end-->
<!--bookdown:toc:start-->
<div class="row">
<header class="col-sm-12 col-lg-3 sidebar sidebar-book">
<a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
<div class="d-flex align-items-start justify-content-between">
<h1>
<a href="index.html" title="">Mastering Shiny Solutions</a>
</h1>
<button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
</div>
<div id="main-nav" class="collapse-lg">
<form role="search">
<input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>
<nav aria-label="Table of contents">
<h2>Table of contents</h2>
<div id="book-toc"></div>
<div class="book-extra">
<p><a id="book-repo" href="#">View book source <i class="fab fa-github"></i></a></li></p>
</div>
</nav>
</div>
</header>
<main class="col-sm-12 col-md-9 col-lg-7" id="content">
<!--bookdown:toc:end-->
<!--bookdown:body:start-->
<div id="welcome" class="section level1 unnumbered">
<h1 class="unnumbered">Welcome</h1>
<p><img src="img/mastering-shiny-cover.png" class="cover" width="250" height="328"/>This is the website for Mastering Shiny Solutions, a solutions manual for the exercises in <a href="https://mastering-shiny.org/">Mastering Shiny</a>, written by Hadley Wickham.</p>
<p><a href="https://mastering-shiny-solutions.org/">Mastering Shiny Solutions 2021</a>, by Maya Gans and Marly Gotti, was released in early 2021. Since then, there have been various changes to the exercises in Mastering Shiny, and this book serves as an <em>updated version</em>. A few solutions in this book defer to those provided in Mastering Shiny Solutions 2021. Also, some exercises don’t contain solutions, and for these exercises, the author writes, “Not sure.”</p>
<p>If my work has helped you, <a href="https://ko-fi.com/howardbaek">you can buy me a coffee on Ko-fi!</a></p>
<div id="about-the-author" class="section level2 unnumbered">
<h2 class="unnumbered">About the Author</h2>
<p><a href="http://insidethetv.rbind.io/"><em>Howard Baek</em></a> is a Master’s student in Biostatistics at the University of Washington. His past experiences include a <a href="https://github.com/howardbaek/addiction-dashboard-simple">NIH-funded Research Assistantship</a> at the Behavioral Research In Technology and Engineering (BRiTE) Center, where he developed a <strong>Shiny Dashboard</strong> that allows patients and clinicians in addiction treatment to monitor patients’ progress and goals over time and an <a href="https://github.com/howardbaek/mooc-project-github">Educational Data Mining Research Internship</a> at George Mason University, where he analyzed real world datasets from a Stanford online course and created a <strong>Shiny Dashboard</strong> for instructors to interact with the dataset.</p>
</div>
<div id="acknowledgments" class="section level2 unnumbered">
<h2 class="unnumbered">Acknowledgments</h2>
<p>The author is grateful to Hadley Wickham for writing Mastering Shiny and making it available online. Alison Hill and Desirée De Leon’s talk, <a href="https://youtu.be/QcE4RBH2auQ?t=1881">Sharing on Short Notice</a>, helped in deploying this book.</p>
<p>Thank you to everyone who contributed solutions by creating a Pull Request on GitHub: <code>@kcha193</code></p>
<!--chapter:end:index.Rmd-->
</div>
</div>
<div id="part-getting-started" class="section level1 unnumbered">
<h1 class="unnumbered">(PART) Getting started</h1>
</div>
<div id="your-first-shiny-app" class="section level1" number="1">
<h1><span class="header-section-number">1</span> Your first Shiny app</h1>
<div id="exercises" class="section level2 unnumbered">
<h2 class="unnumbered">1.8 Exercises</h2>
<ol style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">textInput</span>(<span class="st">"name"</span>, <span class="st">"What's your name?"</span>),</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">textOutput</span>(<span class="st">"greeting"</span>)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>greeting <span class="ot"><-</span> <span class="fu">renderText</span>({</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"Hello "</span>, input<span class="sc">$</span>name)</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">"x"</span>, <span class="at">label =</span> <span class="st">"If x is"</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">50</span>, <span class="at">value =</span> <span class="dv">30</span>),</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"then x times 5 is"</span>,</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">textOutput</span>(<span class="st">"product"</span>)</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>product <span class="ot"><-</span> <span class="fu">renderText</span>({ </span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># Fixed error</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> input<span class="sc">$</span>x <span class="sc">*</span> <span class="dv">5</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># by adding input$ </span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">"x"</span>, <span class="at">label =</span> <span class="st">"If x is"</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">50</span>, <span class="at">value =</span> <span class="dv">30</span>),</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">"y"</span>, <span class="at">label =</span> <span class="st">"and y is"</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">50</span>, <span class="at">value =</span> <span class="dv">30</span>),</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"then x times y is"</span>,</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">textOutput</span>(<span class="st">"product"</span>)</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>product <span class="ot"><-</span> <span class="fu">renderText</span>({ </span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> input<span class="sc">$</span>x <span class="sc">*</span> input<span class="sc">$</span>y</span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">"x"</span>, <span class="st">"If x is"</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">50</span>, <span class="at">value =</span> <span class="dv">30</span>),</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">"y"</span>, <span class="st">"and y is"</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">50</span>, <span class="at">value =</span> <span class="dv">5</span>),</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"then, (x * y) is"</span>, <span class="fu">textOutput</span>(<span class="st">"product"</span>),</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"and, (x * y) + 5 is"</span>, <span class="fu">textOutput</span>(<span class="st">"product_plus5"</span>),</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"and (x * y) + 10 is"</span>, <span class="fu">textOutput</span>(<span class="st">"product_plus10"</span>)</span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># Add this reactive expression to reduce </span></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># amount of duplicated code</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a> product <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a> input<span class="sc">$</span>x <span class="sc">*</span> input<span class="sc">$</span>y</span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>product <span class="ot"><-</span> <span class="fu">renderText</span>({ </span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">product</span>()</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>product_plus5 <span class="ot"><-</span> <span class="fu">renderText</span>({ </span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">product</span>() <span class="sc">+</span> <span class="dv">5</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>product_plus10 <span class="ot"><-</span> <span class="fu">renderText</span>({ </span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">product</span>() <span class="sc">+</span> <span class="dv">10</span></span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ul>
<li>What’s new is the additional calculation where 5 and 10 were added to the product and the outputs rendered as text.</li>
</ul>
<ol start="5" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>datasets <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"economics"</span>, <span class="st">"faithfuld"</span>, <span class="st">"seals"</span>)</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"dataset"</span>, <span class="st">"Dataset"</span>, <span class="at">choices =</span> datasets),</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">verbatimTextOutput</span>(<span class="st">"summary"</span>),</span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># 1st Bug: tableOutput -> plotOutput</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"plot"</span>)</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> dataset <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">get</span>(input<span class="sc">$</span>dataset, <span class="st">"package:ggplot2"</span>)</span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> <span class="co"># 2nd Bug: Spelling</span></span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>summary <span class="ot"><-</span> <span class="fu">renderPrint</span>({</span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">summary</span>(<span class="fu">dataset</span>())</span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>plot <span class="ot"><-</span> <span class="fu">renderPlot</span>({</span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a> <span class="co"># 3rd Bug: dataset -> dataset() </span></span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">plot</span>(<span class="fu">dataset</span>())</span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a> }, <span class="at">res =</span> <span class="dv">96</span>)</span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<!--chapter:end:01-first-app.Rmd-->
</div>
</div>
<div id="basic-ui" class="section level1" number="2">
<h1><span class="header-section-number">2</span> Basic UI</h1>
<div id="exercises-1" class="section level2 unnumbered">
<h2 class="unnumbered">2.2.8 Exercises</h2>
<ol style="list-style-type: decimal">
<li><p>Provide <code>value</code> parameter: <code>textInput("name", value = "Your name")</code></p></li>
<li></li>
</ol>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>?shiny<span class="sc">::</span><span class="fu">sliderInput</span>()</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="at">inputId =</span> <span class="st">"user_input"</span>,</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="at">label =</span> <span class="st">"User Input"</span>, </span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="at">value =</span> <span class="dv">10</span>,</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="at">min =</span> <span class="dv">0</span>, <span class="at">max =</span> <span class="dv">100</span>,</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="at">step =</span> <span class="dv">5</span>,</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Added animation</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="at">animate =</span> <span class="fu">animationOptions</span>(</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="at">interval =</span> <span class="dv">1000</span>,</span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="at">loop =</span> <span class="cn">TRUE</span>,</span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="at">playButton =</span> <span class="cn">NULL</span>,</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="at">pauseButton =</span> <span class="cn">NULL</span></span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {}</span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li><code>selectInput()</code> documentation:</li>
</ol>
<div class="rmdnote">
<p>It’s also possible to group related inputs by providing a named list whose elements are (either named or unnamed) lists, vectors, or factors. In this case, the outermost names will be used as the group labels (leveraging the <code><optgroup></code> HTML tag) for the elements in the respective sublist. See the example section for a small demo of this feature.</p>
</div>
</div>
<div id="exercises-2" class="section level2 unnumbered">
<h2 class="unnumbered">2.3.5 Exercises</h2>
<ol style="list-style-type: decimal">
<li></li>
</ol>
<ol style="list-style-type: lower-alpha">
<li><code>renderPrint(summary(mtcars))</code> should be paired with <code>verbatimTextOutput</code> since it is console output.</li>
<li><code>renderText("Good morning!")</code> should be paired with <code>textOutput</code> since it is regular text.</li>
<li><code>renderPrint(t.test(1:5, 2:6))</code> should be paired with <code>verbatimTextOutput</code> since it is console output.</li>
<li><code>renderText(str(lm(mpg ~ wt, data = mtcars)))</code> should be paired with <code>verbatimTextOutput</code> since it is console output.</li>
</ol>
<ol start="2" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"plot"</span>, <span class="at">width =</span> <span class="st">"700px"</span>, <span class="at">height =</span> <span class="st">"300px"</span>)</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>plot <span class="ot"><-</span> <span class="fu">renderPlot</span>(<span class="fu">plot</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>), <span class="at">res =</span> <span class="dv">96</span>, </span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="at">alt =</span> <span class="st">"Scatterplot of 5 random numbers"</span>)</span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">dataTableOutput</span>(<span class="st">"table"</span>)</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>table <span class="ot"><-</span> <span class="fu">renderDataTable</span>(mtcars, </span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> <span class="at">options =</span> <span class="fu">list</span>(<span class="at">pageLength =</span> <span class="dv">5</span>,</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a> <span class="at">ordering =</span> <span class="cn">FALSE</span>, </span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a> <span class="at">searching =</span> <span class="cn">FALSE</span>))</span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(reactable)</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">reactableOutput</span>(<span class="st">"table"</span>)</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output) {</span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>table <span class="ot"><-</span> <span class="fu">renderReactable</span>({</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">reactable</span>(mtcars)</span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<!--chapter:end:02-basic-ui.Rmd-->
</div>
</div>
<div id="basic-reactivity" class="section level1" number="3">
<h1><span class="header-section-number">3</span> Basic reactivity</h1>
<div id="exercises-3" class="section level2 unnumbered">
<h2 class="unnumbered">3.3.6 Exercises</h2>
<ol style="list-style-type: decimal">
<li></li>
</ol>
<div class="rmdnote">
<p><strong>Server 1</strong></p>
<ul>
<li><code>input$greeting</code> –> <code>output$greeting</code></li>
<li>Inside <code>renderText</code>, <code>name</code> –> <code>input$name</code></li>
<li>Fixed code:</li>
</ul>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>server1 <span class="ot"><-</span> <span class="cf">function</span>(input, output, server) {</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>greeting <span class="ot"><-</span> <span class="fu">renderText</span>(<span class="fu">paste0</span>(<span class="st">"Hello "</span>, input<span class="sc">$</span>name))</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div class="rmdnote">
<p><strong>Server 2</strong></p>
<ul>
<li>Make <code>greeting</code> a reactive: <code>greeting <- reactive(paste0("Hello ", input$name))</code></li>
<li>Since <code>greeting</code> is now a reactive, add parenthesis around it: <code>output$greeting <- renderText(greeting())</code></li>
<li>Fixed code:</li>
</ul>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>server2 <span class="ot"><-</span> <span class="cf">function</span>(input, output, server) {</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> greeting <span class="ot"><-</span> <span class="fu">reactive</span>(<span class="fu">paste0</span>(<span class="st">"Hello "</span>, input<span class="sc">$</span>name))</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>greeting <span class="ot"><-</span> <span class="fu">renderText</span>(<span class="fu">greeting</span>())</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div class="rmdnote">
<p><strong>Server 3</strong></p>
<ul>
<li>Spelling error: <code>output$greting</code> –> <code>output$greeting</code></li>
<li>Missing <code>renderText()</code></li>
<li>Fixed code:</li>
</ul>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>server3 <span class="ot"><-</span> <span class="cf">function</span>(input, output, server) {</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>greeting <span class="ot"><-</span> <span class="fu">renderText</span>(<span class="fu">paste0</span>(<span class="st">"Hello "</span>, input<span class="sc">$</span>name))</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div class="rmdimportant">
<ol start="2" style="list-style-type: decimal">
<li>Solution at <a href="https://mastering-shiny-solutions.org/basic-reactivity.html#solution-15">Mastering Shiny Solutions 2021</a></li>
</ol>
</div>
<ol start="3" style="list-style-type: decimal">
<li>When you use <code>range()</code> or <code>var()</code>, other readers won’t know if you are using a reactive or the built-in R function.</li>
</ol>
<div class="rmdwarning">
<p>Not sure why code fails, but maybe reading the chapter on <a href="https://mastering-shiny.org/action-tidy.html#action-tidy">Tidy evaluation</a> will help.</p>
</div>
<!--chapter:end:03-basic-reactivity.Rmd-->
</div>
</div>
<div id="case-study-er-injuries" class="section level1" number="4">
<h1><span class="header-section-number">4</span> Case study: ER injuries</h1>
<div id="exercises-4" class="section level2 unnumbered">
<h2 class="unnumbered">4.8 Exercises</h2>
<div class="rmdimportant">
<ol style="list-style-type: decimal">
<li>Solution at <a href="https://mastering-shiny-solutions.org/case-study-er-injuries.html#exercise-5.8.1">Mastering Shiny Solutions 2021</a></li>
</ol>
</div>
<ol start="2" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a>injuries <span class="ot"><-</span> vroom<span class="sc">::</span><span class="fu">vroom</span>(<span class="st">"neiss/injuries.tsv.gz"</span>)</span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a>injuries</span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Original code</span></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a>injuries <span class="sc">%>%</span></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">diag =</span> <span class="fu">fct_lump</span>(<span class="fu">fct_infreq</span>(diag), <span class="at">n =</span> <span class="dv">5</span>)) <span class="sc">%>%</span></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(diag) <span class="sc">%>%</span></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">n =</span> <span class="fu">as.integer</span>(<span class="fu">sum</span>(weight)))</span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Flipped code</span></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a>injuries <span class="sc">%>%</span></span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">diag =</span> <span class="fu">fct_infreq</span>(<span class="fu">fct_lump</span>(diag, <span class="at">n =</span> <span class="dv">5</span>))) <span class="sc">%>%</span></span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(diag) <span class="sc">%>%</span></span>
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">n =</span> <span class="fu">as.integer</span>(<span class="fu">sum</span>(weight)))</span></code></pre></div>
<div class="rmdcaution">
<p>If you want to get the data on your own computer, run this code:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dir.create</span>(<span class="st">"neiss"</span>)</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> Warning in dir.create("neiss"): 'neiss' already exists</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>download <span class="ot"><-</span> <span class="cf">function</span>(name) {</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a> url <span class="ot"><-</span> <span class="st">"https://github.com/hadley/mastering-shiny/raw/master/neiss/"</span></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">download.file</span>(<span class="fu">paste0</span>(url, name), <span class="fu">paste0</span>(<span class="st">"neiss/"</span>, name), <span class="at">quiet =</span> <span class="cn">TRUE</span>)</span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a><span class="fu">download</span>(<span class="st">"injuries.tsv.gz"</span>)</span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a><span class="fu">download</span>(<span class="st">"population.tsv"</span>)</span>
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a><span class="fu">download</span>(<span class="st">"products.tsv"</span>)</span></code></pre></div>
</div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(forcats)</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(vroom)</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a>injuries <span class="ot"><-</span> vroom<span class="sc">::</span><span class="fu">vroom</span>(<span class="st">"neiss/injuries.tsv.gz"</span>)</span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a>products <span class="ot"><-</span> vroom<span class="sc">::</span><span class="fu">vroom</span>(<span class="st">"neiss/products.tsv"</span>)</span>
<span id="cb16-9"><a href="#cb16-9" aria-hidden="true" tabindex="-1"></a>population <span class="ot"><-</span> vroom<span class="sc">::</span><span class="fu">vroom</span>(<span class="st">"neiss/population.tsv"</span>)</span>
<span id="cb16-10"><a href="#cb16-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-11"><a href="#cb16-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-12"><a href="#cb16-12" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb16-13"><a href="#cb16-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb16-14"><a href="#cb16-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">8</span>,</span>
<span id="cb16-15"><a href="#cb16-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"code"</span>, <span class="st">"Product"</span>,</span>
<span id="cb16-16"><a href="#cb16-16" aria-hidden="true" tabindex="-1"></a> <span class="at">choices =</span> <span class="fu">setNames</span>(products<span class="sc">$</span>prod_code, products<span class="sc">$</span>title),</span>
<span id="cb16-17"><a href="#cb16-17" aria-hidden="true" tabindex="-1"></a> <span class="at">width =</span> <span class="st">"100%"</span></span>
<span id="cb16-18"><a href="#cb16-18" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb16-19"><a href="#cb16-19" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb16-20"><a href="#cb16-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">2</span>, <span class="fu">selectInput</span>(<span class="st">"y"</span>, <span class="st">"Y axis"</span>, <span class="fu">c</span>(<span class="st">"rate"</span>, <span class="st">"count"</span>))),</span>
<span id="cb16-21"><a href="#cb16-21" aria-hidden="true" tabindex="-1"></a> <span class="co"># lets the user decide how many rows to show in the summary tables</span></span>
<span id="cb16-22"><a href="#cb16-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">2</span>, <span class="fu">numericInput</span>(<span class="st">"num_rows"</span>, <span class="st">"Number of Rows"</span>, <span class="at">value =</span> <span class="dv">5</span>, <span class="at">min =</span> <span class="dv">0</span>, <span class="at">max =</span> <span class="dv">6</span>))</span>
<span id="cb16-23"><a href="#cb16-23" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb16-24"><a href="#cb16-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb16-25"><a href="#cb16-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">4</span>, <span class="fu">tableOutput</span>(<span class="st">"diag"</span>)),</span>
<span id="cb16-26"><a href="#cb16-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">4</span>, <span class="fu">tableOutput</span>(<span class="st">"body_part"</span>)),</span>
<span id="cb16-27"><a href="#cb16-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">4</span>, <span class="fu">tableOutput</span>(<span class="st">"location"</span>))</span>
<span id="cb16-28"><a href="#cb16-28" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb16-29"><a href="#cb16-29" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb16-30"><a href="#cb16-30" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">12</span>, <span class="fu">plotOutput</span>(<span class="st">"age_sex"</span>))</span>
<span id="cb16-31"><a href="#cb16-31" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb16-32"><a href="#cb16-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb16-33"><a href="#cb16-33" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">2</span>, <span class="fu">actionButton</span>(<span class="st">"story"</span>, <span class="st">"Tell me a story"</span>)),</span>
<span id="cb16-34"><a href="#cb16-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">10</span>, <span class="fu">textOutput</span>(<span class="st">"narrative"</span>))</span>
<span id="cb16-35"><a href="#cb16-35" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb16-36"><a href="#cb16-36" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb16-37"><a href="#cb16-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-38"><a href="#cb16-38" aria-hidden="true" tabindex="-1"></a>count_top <span class="ot"><-</span> <span class="cf">function</span>(df, var, <span class="at">n =</span> <span class="dv">5</span>) {</span>
<span id="cb16-39"><a href="#cb16-39" aria-hidden="true" tabindex="-1"></a> df <span class="sc">%>%</span></span>
<span id="cb16-40"><a href="#cb16-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>({{ var }} <span class="sc">:</span><span class="er">=</span> <span class="fu">fct_lump</span>(<span class="fu">fct_infreq</span>({{ var }}), <span class="at">n =</span> n)) <span class="sc">%>%</span></span>
<span id="cb16-41"><a href="#cb16-41" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>({{ var }}) <span class="sc">%>%</span></span>
<span id="cb16-42"><a href="#cb16-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">n =</span> <span class="fu">as.integer</span>(<span class="fu">sum</span>(weight)))</span>
<span id="cb16-43"><a href="#cb16-43" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb16-44"><a href="#cb16-44" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-45"><a href="#cb16-45" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb16-46"><a href="#cb16-46" aria-hidden="true" tabindex="-1"></a> selected <span class="ot"><-</span> <span class="fu">reactive</span>(injuries <span class="sc">%>%</span> <span class="fu">filter</span>(prod_code <span class="sc">==</span> input<span class="sc">$</span>code))</span>
<span id="cb16-47"><a href="#cb16-47" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb16-48"><a href="#cb16-48" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>diag <span class="ot"><-</span> <span class="fu">renderTable</span>(<span class="fu">count_top</span>(<span class="fu">selected</span>(), diag) <span class="sc">%>%</span> <span class="fu">slice</span>(<span class="dv">1</span><span class="sc">:</span>input<span class="sc">$</span>num_rows), <span class="at">width =</span> <span class="st">"100%"</span>)</span>
<span id="cb16-49"><a href="#cb16-49" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>body_part <span class="ot"><-</span> <span class="fu">renderTable</span>(<span class="fu">count_top</span>(<span class="fu">selected</span>(), body_part) <span class="sc">%>%</span> <span class="fu">slice</span>(<span class="dv">1</span><span class="sc">:</span>input<span class="sc">$</span>num_rows), <span class="at">width =</span> <span class="st">"100%"</span>)</span>
<span id="cb16-50"><a href="#cb16-50" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>location <span class="ot"><-</span> <span class="fu">renderTable</span>(<span class="fu">count_top</span>(<span class="fu">selected</span>(), location) <span class="sc">%>%</span> <span class="fu">slice</span>(<span class="dv">1</span><span class="sc">:</span>input<span class="sc">$</span>num_rows), <span class="at">width =</span> <span class="st">"100%"</span>)</span>
<span id="cb16-51"><a href="#cb16-51" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb16-52"><a href="#cb16-52" aria-hidden="true" tabindex="-1"></a> summary <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb16-53"><a href="#cb16-53" aria-hidden="true" tabindex="-1"></a> <span class="fu">selected</span>() <span class="sc">%>%</span></span>
<span id="cb16-54"><a href="#cb16-54" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(age, sex, <span class="at">wt =</span> weight) <span class="sc">%>%</span></span>
<span id="cb16-55"><a href="#cb16-55" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(population, <span class="at">by =</span> <span class="fu">c</span>(<span class="st">"age"</span>, <span class="st">"sex"</span>)) <span class="sc">%>%</span></span>
<span id="cb16-56"><a href="#cb16-56" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rate =</span> n <span class="sc">/</span> population <span class="sc">*</span> <span class="fl">1e4</span>)</span>
<span id="cb16-57"><a href="#cb16-57" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb16-58"><a href="#cb16-58" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb16-59"><a href="#cb16-59" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>age_sex <span class="ot"><-</span> <span class="fu">renderPlot</span>({</span>
<span id="cb16-60"><a href="#cb16-60" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (input<span class="sc">$</span>y <span class="sc">==</span> <span class="st">"count"</span>) {</span>
<span id="cb16-61"><a href="#cb16-61" aria-hidden="true" tabindex="-1"></a> <span class="fu">summary</span>() <span class="sc">%>%</span></span>
<span id="cb16-62"><a href="#cb16-62" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(age, n, <span class="at">colour =</span> sex)) <span class="sc">+</span></span>
<span id="cb16-63"><a href="#cb16-63" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span></span>
<span id="cb16-64"><a href="#cb16-64" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">y =</span> <span class="st">"Estimated number of injuries"</span>)</span>
<span id="cb16-65"><a href="#cb16-65" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb16-66"><a href="#cb16-66" aria-hidden="true" tabindex="-1"></a> <span class="fu">summary</span>() <span class="sc">%>%</span></span>
<span id="cb16-67"><a href="#cb16-67" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(age, rate, <span class="at">colour =</span> sex)) <span class="sc">+</span></span>
<span id="cb16-68"><a href="#cb16-68" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">na.rm =</span> <span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb16-69"><a href="#cb16-69" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">y =</span> <span class="st">"Injuries per 10,000 people"</span>)</span>
<span id="cb16-70"><a href="#cb16-70" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb16-71"><a href="#cb16-71" aria-hidden="true" tabindex="-1"></a> }, <span class="at">res =</span> <span class="dv">96</span>)</span>
<span id="cb16-72"><a href="#cb16-72" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb16-73"><a href="#cb16-73" aria-hidden="true" tabindex="-1"></a> narrative_sample <span class="ot"><-</span> <span class="fu">eventReactive</span>(</span>
<span id="cb16-74"><a href="#cb16-74" aria-hidden="true" tabindex="-1"></a> <span class="fu">list</span>(input<span class="sc">$</span>story, <span class="fu">selected</span>()),</span>
<span id="cb16-75"><a href="#cb16-75" aria-hidden="true" tabindex="-1"></a> <span class="fu">selected</span>() <span class="sc">%>%</span> <span class="fu">pull</span>(narrative) <span class="sc">%>%</span> <span class="fu">sample</span>(<span class="dv">1</span>)</span>
<span id="cb16-76"><a href="#cb16-76" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb16-77"><a href="#cb16-77" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>narrative <span class="ot"><-</span> <span class="fu">renderText</span>(<span class="fu">narrative_sample</span>())</span>
<span id="cb16-78"><a href="#cb16-78" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb16-79"><a href="#cb16-79" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-80"><a href="#cb16-80" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<div class="rmdimportant">
<ol start="4" style="list-style-type: decimal">
<li>Solution at <a href="https://mastering-shiny-solutions.org/case-study-er-injuries.html#exercise-5.8.4">Mastering Shiny Solutions 2021</a></li>
</ol>
</div>
<!--chapter:end:04-case-study.Rmd-->
</div>
</div>
<div id="part-shiny-in-action" class="section level1 unnumbered">
<h1 class="unnumbered">(PART) Shiny in action</h1>
</div>
<div id="workflow" class="section level1" number="5">
<h1><span class="header-section-number">5</span> Workflow</h1>
<p>There are no exercises in this chapter.</p>
<!--chapter:end:05-workflow.Rmd-->
</div>
<div id="layout-themes-html" class="section level1" number="6">
<h1><span class="header-section-number">6</span> Layout, themes, HTML</h1>
<div id="exercises-5" class="section level2 unnumbered">
<h2 class="unnumbered">6.2.4 Exercises</h2>
<ol style="list-style-type: decimal">
<li><code>sidebarLayout()</code> documentation:</li>
</ol>
<div class="rmdnote">
<p>By default, the sidebar takes up 1/3 of the width, and the main panel 2/3.</p>
</div>
<p>In other words, given the width is 12 columns, the sidebar is made up of 4 columns and the main panel 8 columns.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Recreate sidebarLayout()</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fluidRow</span>(</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># sidebar (4 columns)</span></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">4</span>, </span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> ...</span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># # main panel (8 columns)</span></span>
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">8</span>, </span>
<span id="cb17-9"><a href="#cb17-9" aria-hidden="true" tabindex="-1"></a> ...</span>
<span id="cb17-10"><a href="#cb17-10" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb17-11"><a href="#cb17-11" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">titlePanel</span>(<span class="st">"Central limit theorem"</span>),</span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarLayout</span>(</span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarPanel</span>(</span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">numericInput</span>(<span class="st">"m"</span>, <span class="st">"Number of samples:"</span>, <span class="dv">2</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">100</span>)</span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mainPanel</span>(</span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"hist"</span>)</span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># Modified to put position of sidebar on the right</span></span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a> <span class="at">position =</span> <span class="st">"right"</span></span>
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>hist <span class="ot"><-</span> <span class="fu">renderPlot</span>({</span>
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a> means <span class="ot"><-</span> <span class="fu">replicate</span>(<span class="fl">1e4</span>, <span class="fu">mean</span>(<span class="fu">runif</span>(input<span class="sc">$</span>m)))</span>
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">hist</span>(means, <span class="at">breaks =</span> <span class="dv">20</span>)</span>
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a> }, <span class="at">res =</span> <span class="dv">96</span>)</span>
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="rmdtip">
<p>Reference:
<a href="https://shiny.rstudio.com/articles/layout-guide.html" class="uri">https://shiny.rstudio.com/articles/layout-guide.html</a></p>
</div>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="co"># UI ONLY</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a>dataset <span class="ot"><-</span> diamonds</span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Diamonds Explorer"</span>,</span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">6</span>,</span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># First plot taking up half the width</span></span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"plot1"</span>)</span>
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb19-16"><a href="#cb19-16" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-17"><a href="#cb19-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">6</span>,</span>
<span id="cb19-18"><a href="#cb19-18" aria-hidden="true" tabindex="-1"></a> <span class="co"># Second plot taking up half the width</span></span>
<span id="cb19-19"><a href="#cb19-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"plot2"</span>)</span>
<span id="cb19-20"><a href="#cb19-20" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb19-21"><a href="#cb19-21" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb19-22"><a href="#cb19-22" aria-hidden="true" tabindex="-1"></a> <span class="co"># Horizontal Line</span></span>
<span id="cb19-23"><a href="#cb19-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">hr</span>(),</span>
<span id="cb19-24"><a href="#cb19-24" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-25"><a href="#cb19-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb19-26"><a href="#cb19-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">3</span>,</span>
<span id="cb19-27"><a href="#cb19-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">h4</span>(<span class="st">"Diamonds Explorer"</span>),</span>
<span id="cb19-28"><a href="#cb19-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">'sampleSize'</span>, <span class="st">'Sample Size'</span>, </span>
<span id="cb19-29"><a href="#cb19-29" aria-hidden="true" tabindex="-1"></a> <span class="at">min=</span><span class="dv">1</span>, <span class="at">max=</span><span class="fu">nrow</span>(dataset), <span class="at">value=</span><span class="fu">min</span>(<span class="dv">1000</span>, <span class="fu">nrow</span>(dataset)), </span>
<span id="cb19-30"><a href="#cb19-30" aria-hidden="true" tabindex="-1"></a> <span class="at">step=</span><span class="dv">500</span>, <span class="at">round=</span><span class="dv">0</span>),</span>
<span id="cb19-31"><a href="#cb19-31" aria-hidden="true" tabindex="-1"></a> <span class="fu">br</span>(),</span>
<span id="cb19-32"><a href="#cb19-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxInput</span>(<span class="st">'jitter'</span>, <span class="st">'Jitter'</span>),</span>
<span id="cb19-33"><a href="#cb19-33" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxInput</span>(<span class="st">'smooth'</span>, <span class="st">'Smooth'</span>)</span>
<span id="cb19-34"><a href="#cb19-34" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb19-35"><a href="#cb19-35" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">4</span>, <span class="at">offset =</span> <span class="dv">1</span>,</span>
<span id="cb19-36"><a href="#cb19-36" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">'x'</span>, <span class="st">'X'</span>, <span class="fu">names</span>(dataset)),</span>
<span id="cb19-37"><a href="#cb19-37" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">'y'</span>, <span class="st">'Y'</span>, <span class="fu">names</span>(dataset), <span class="fu">names</span>(dataset)[[<span class="dv">2</span>]]),</span>
<span id="cb19-38"><a href="#cb19-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">'color'</span>, <span class="st">'Color'</span>, <span class="fu">c</span>(<span class="st">'None'</span>, <span class="fu">names</span>(dataset)))</span>
<span id="cb19-39"><a href="#cb19-39" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb19-40"><a href="#cb19-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="dv">4</span>,</span>
<span id="cb19-41"><a href="#cb19-41" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">'facet_row'</span>, <span class="st">'Facet Row'</span>, <span class="fu">c</span>(<span class="at">None=</span><span class="st">'.'</span>, <span class="fu">names</span>(dataset))),</span>
<span id="cb19-42"><a href="#cb19-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">'facet_col'</span>, <span class="st">'Facet Column'</span>, <span class="fu">c</span>(<span class="at">None=</span><span class="st">'.'</span>, <span class="fu">names</span>(dataset)))</span>
<span id="cb19-43"><a href="#cb19-43" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb19-44"><a href="#cb19-44" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb19-45"><a href="#cb19-45" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb19-46"><a href="#cb19-46" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-47"><a href="#cb19-47" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<!--chapter:end:06-layout-theme-html.Rmd-->
</div>
</div>
<div id="graphics" class="section level1" number="7">
<h1><span class="header-section-number">7</span> Graphics</h1>
<p>There are no exercises in this chapter</p>
<!--chapter:end:07-graphics.Rmd-->
</div>
<div id="user-feedback" class="section level1" number="8">
<h1><span class="header-section-number">8</span> User feedback</h1>
<p>There are no exercises in this chapter.</p>
<!--chapter:end:08-user-feedback.Rmd-->
</div>
<div id="uploads-and-downloads" class="section level1" number="9">
<h1><span class="header-section-number">9</span> Uploads and downloads</h1>
<div id="exercises-6" class="section level2 unnumbered">
<h2 class="unnumbered">9.4 Exercises</h2>
<ol start="2" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Increase max limit of size of uploaded file</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="fu">options</span>(<span class="at">shiny.maxRequestSize =</span> <span class="dv">10</span> <span class="sc">*</span> <span class="dv">1024</span><span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># upload a csv file</span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">fileInput</span>(<span class="st">"upload"</span>, <span class="cn">NULL</span>, </span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> <span class="at">buttonLabel =</span> <span class="st">"Upload CSV"</span>, <span class="at">accept =</span> <span class="st">".csv"</span>),</span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># select a variable</span></span>
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"var"</span>, <span class="st">"Select a variable"</span>, <span class="at">choices =</span> <span class="cn">NULL</span>),</span>
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># show output of t.test()</span></span>
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">verbatimTextOutput</span>(<span class="st">"t_test"</span>)</span>
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-15"><a href="#cb20-15" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb20-16"><a href="#cb20-16" aria-hidden="true" tabindex="-1"></a> <span class="co"># uploaded dataset</span></span>
<span id="cb20-17"><a href="#cb20-17" aria-hidden="true" tabindex="-1"></a> data <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb20-18"><a href="#cb20-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>upload)</span>
<span id="cb20-19"><a href="#cb20-19" aria-hidden="true" tabindex="-1"></a> readr<span class="sc">::</span><span class="fu">read_csv</span>(input<span class="sc">$</span>upload<span class="sc">$</span>datapath)</span>
<span id="cb20-20"><a href="#cb20-20" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb20-21"><a href="#cb20-21" aria-hidden="true" tabindex="-1"></a> <span class="co"># once user uploads data, fill in the available variables</span></span>
<span id="cb20-22"><a href="#cb20-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">observeEvent</span>(<span class="fu">data</span>(), {</span>
<span id="cb20-23"><a href="#cb20-23" aria-hidden="true" tabindex="-1"></a> choices <span class="ot"><-</span> <span class="fu">unique</span>(<span class="fu">colnames</span>(<span class="fu">data</span>()))</span>
<span id="cb20-24"><a href="#cb20-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">updateSelectInput</span>(<span class="at">inputId =</span> <span class="st">"var"</span>, <span class="at">choices =</span> choices) </span>
<span id="cb20-25"><a href="#cb20-25" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb20-26"><a href="#cb20-26" aria-hidden="true" tabindex="-1"></a> <span class="co"># show output of t-test</span></span>
<span id="cb20-27"><a href="#cb20-27" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>t_test <span class="ot"><-</span> <span class="fu">renderPrint</span>({ </span>
<span id="cb20-28"><a href="#cb20-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>var)</span>
<span id="cb20-29"><a href="#cb20-29" aria-hidden="true" tabindex="-1"></a> <span class="fu">t.test</span>(<span class="fu">data</span>()[[input<span class="sc">$</span>var]], <span class="at">mu =</span> <span class="dv">0</span>) </span>
<span id="cb20-30"><a href="#cb20-30" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb20-31"><a href="#cb20-31" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb20-32"><a href="#cb20-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-33"><a href="#cb20-33" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># upload a csv file</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">fileInput</span>(<span class="st">"upload"</span>, <span class="cn">NULL</span>, </span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="at">buttonLabel =</span> <span class="st">"Upload CSV"</span>, <span class="at">accept =</span> <span class="st">".csv"</span>),</span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># select a variable</span></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"var"</span>, <span class="st">"Select a variable"</span>, <span class="at">choices =</span> <span class="cn">NULL</span>),</span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># show histogram</span></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"plot"</span>),</span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">radioButtons</span>(<span class="st">"ext"</span>, <span class="st">"Save As:"</span>,</span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a> <span class="at">choices =</span> <span class="fu">c</span>(<span class="st">"png"</span>, <span class="st">"pdf"</span>, <span class="st">"svg"</span>), <span class="at">inline =</span> <span class="cn">TRUE</span>),</span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a> <span class="co"># download histogram</span></span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">downloadButton</span>(<span class="st">"download"</span>)</span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># uploaded dataset</span></span>
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true" tabindex="-1"></a> data <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb21-21"><a href="#cb21-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>upload)</span>
<span id="cb21-22"><a href="#cb21-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">read_csv</span>(input<span class="sc">$</span>upload<span class="sc">$</span>datapath)</span>
<span id="cb21-23"><a href="#cb21-23" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb21-24"><a href="#cb21-24" aria-hidden="true" tabindex="-1"></a> <span class="co"># once user uploads data, fill in the available variables</span></span>
<span id="cb21-25"><a href="#cb21-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">observeEvent</span>(<span class="fu">data</span>(), {</span>
<span id="cb21-26"><a href="#cb21-26" aria-hidden="true" tabindex="-1"></a> choices <span class="ot"><-</span> <span class="fu">unique</span>(<span class="fu">colnames</span>(<span class="fu">data</span>()))</span>
<span id="cb21-27"><a href="#cb21-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">updateSelectInput</span>(<span class="at">inputId =</span> <span class="st">"var"</span>, <span class="at">choices =</span> choices) </span>
<span id="cb21-28"><a href="#cb21-28" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb21-29"><a href="#cb21-29" aria-hidden="true" tabindex="-1"></a> <span class="co"># create reactive plot </span></span>
<span id="cb21-30"><a href="#cb21-30" aria-hidden="true" tabindex="-1"></a> plot_output <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb21-31"><a href="#cb21-31" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>var)</span>
<span id="cb21-32"><a href="#cb21-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">data</span>()) <span class="sc">+</span></span>
<span id="cb21-33"><a href="#cb21-33" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>(<span class="fu">aes</span>(.data[[input<span class="sc">$</span>var]]))</span>
<span id="cb21-34"><a href="#cb21-34" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb21-35"><a href="#cb21-35" aria-hidden="true" tabindex="-1"></a> <span class="co"># show histogram</span></span>
<span id="cb21-36"><a href="#cb21-36" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>plot <span class="ot"><-</span> <span class="fu">renderPlot</span>({</span>
<span id="cb21-37"><a href="#cb21-37" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>var)</span>
<span id="cb21-38"><a href="#cb21-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">plot_output</span>()</span>
<span id="cb21-39"><a href="#cb21-39" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb21-40"><a href="#cb21-40" aria-hidden="true" tabindex="-1"></a> <span class="co"># download </span></span>
<span id="cb21-41"><a href="#cb21-41" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>download <span class="ot"><-</span> <span class="fu">downloadHandler</span>(</span>
<span id="cb21-42"><a href="#cb21-42" aria-hidden="true" tabindex="-1"></a> <span class="at">filename =</span> <span class="cf">function</span>() {</span>
<span id="cb21-43"><a href="#cb21-43" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste</span>(<span class="st">"histogram"</span>, input<span class="sc">$</span>ext, <span class="at">sep =</span> <span class="st">"."</span>)</span>
<span id="cb21-44"><a href="#cb21-44" aria-hidden="true" tabindex="-1"></a> }, </span>
<span id="cb21-45"><a href="#cb21-45" aria-hidden="true" tabindex="-1"></a> <span class="at">content =</span> <span class="cf">function</span>(file) {</span>
<span id="cb21-46"><a href="#cb21-46" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggsave</span>(file, <span class="fu">plot_output</span>(), <span class="at">device =</span> input<span class="sc">$</span>ext)</span>
<span id="cb21-47"><a href="#cb21-47" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb21-48"><a href="#cb21-48" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb21-49"><a href="#cb21-49" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb21-50"><a href="#cb21-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-51"><a href="#cb21-51" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li>From <a href="https://mastering-shiny-solutions.org/uploads-and-downloads.html#exercise-9.4.4">Mastering Shiny Solutions 2021</a>:</li>
</ol>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(brickr)</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(png)</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Function to provide user feedback (checkout Chapter 8 for more info).</span></span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a>notify <span class="ot"><-</span> <span class="cf">function</span>(msg, <span class="at">id =</span> <span class="cn">NULL</span>) {</span>
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">showNotification</span>(msg, <span class="at">id =</span> id, <span class="at">duration =</span> <span class="cn">NULL</span>, <span class="at">closeButton =</span> <span class="cn">FALSE</span>)</span>
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb22-11"><a href="#cb22-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarLayout</span>(</span>
<span id="cb22-12"><a href="#cb22-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarPanel</span>(</span>
<span id="cb22-13"><a href="#cb22-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(</span>
<span id="cb22-14"><a href="#cb22-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">fileInput</span>(<span class="st">"myFile"</span>, <span class="st">"Upload a PNG file"</span>, <span class="at">accept =</span> <span class="fu">c</span>(<span class="st">'image/png'</span>)),</span>
<span id="cb22-15"><a href="#cb22-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">sliderInput</span>(<span class="st">"size"</span>, <span class="st">"Select size:"</span>, <span class="at">min =</span> <span class="dv">1</span>, <span class="at">max =</span> <span class="dv">100</span>, <span class="at">value =</span> <span class="dv">35</span>),</span>
<span id="cb22-16"><a href="#cb22-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">radioButtons</span>(<span class="st">"color"</span>, <span class="st">"Select color palette:"</span>, <span class="at">choices =</span> <span class="fu">c</span>(<span class="st">"universal"</span>, <span class="st">"generic"</span>))</span>
<span id="cb22-17"><a href="#cb22-17" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb22-18"><a href="#cb22-18" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb22-19"><a href="#cb22-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">mainPanel</span>(</span>
<span id="cb22-20"><a href="#cb22-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">plotOutput</span>(<span class="st">"result"</span>))</span>
<span id="cb22-21"><a href="#cb22-21" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb22-22"><a href="#cb22-22" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb22-23"><a href="#cb22-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-24"><a href="#cb22-24" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output) {</span>
<span id="cb22-25"><a href="#cb22-25" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb22-26"><a href="#cb22-26" aria-hidden="true" tabindex="-1"></a> imageFile <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb22-27"><a href="#cb22-27" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">is.null</span>(input<span class="sc">$</span>myFile))</span>
<span id="cb22-28"><a href="#cb22-28" aria-hidden="true" tabindex="-1"></a> png<span class="sc">::</span><span class="fu">readPNG</span>(input<span class="sc">$</span>myFile<span class="sc">$</span>datapath)</span>
<span id="cb22-29"><a href="#cb22-29" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb22-30"><a href="#cb22-30" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb22-31"><a href="#cb22-31" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>result <span class="ot"><-</span> <span class="fu">renderPlot</span>({</span>
<span id="cb22-32"><a href="#cb22-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(<span class="fu">imageFile</span>())</span>
<span id="cb22-33"><a href="#cb22-33" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb22-34"><a href="#cb22-34" aria-hidden="true" tabindex="-1"></a> id <span class="ot"><-</span> <span class="fu">notify</span>(<span class="st">"Transforming image..."</span>)</span>
<span id="cb22-35"><a href="#cb22-35" aria-hidden="true" tabindex="-1"></a> <span class="fu">on.exit</span>(<span class="fu">removeNotification</span>(id), <span class="at">add =</span> <span class="cn">TRUE</span>)</span>
<span id="cb22-36"><a href="#cb22-36" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb22-37"><a href="#cb22-37" aria-hidden="true" tabindex="-1"></a> <span class="fu">imageFile</span>() <span class="sc">%>%</span></span>
<span id="cb22-38"><a href="#cb22-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">image_to_mosaic</span>(<span class="at">img_size =</span> input<span class="sc">$</span>size, <span class="at">color_palette =</span> input<span class="sc">$</span>color) <span class="sc">%>%</span></span>
<span id="cb22-39"><a href="#cb22-39" aria-hidden="true" tabindex="-1"></a> <span class="fu">build_mosaic</span>()</span>
<span id="cb22-40"><a href="#cb22-40" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb22-41"><a href="#cb22-41" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb22-42"><a href="#cb22-42" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-43"><a href="#cb22-43" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="5" style="list-style-type: decimal">
<li>From the 9.3 Case study, the main change happens in the cleaning step inside the server function, where one large reactive is broken down into three smaller ones.</li>
</ol>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Uploading and parsing the file</span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a>ui_upload <span class="ot"><-</span> <span class="fu">sidebarLayout</span>(</span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarPanel</span>(</span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">fileInput</span>(<span class="st">"file"</span>, <span class="st">"Data"</span>, <span class="at">buttonLabel =</span> <span class="st">"Upload..."</span>),</span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">textInput</span>(<span class="st">"delim"</span>, <span class="st">"Delimiter (leave blank to guess)"</span>, <span class="st">""</span>),</span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">numericInput</span>(<span class="st">"skip"</span>, <span class="st">"Rows to skip"</span>, <span class="dv">0</span>, <span class="at">min =</span> <span class="dv">0</span>),</span>
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">numericInput</span>(<span class="st">"rows"</span>, <span class="st">"Rows to preview"</span>, <span class="dv">10</span>, <span class="at">min =</span> <span class="dv">1</span>)</span>
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb23-12"><a href="#cb23-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">mainPanel</span>(</span>
<span id="cb23-13"><a href="#cb23-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">h3</span>(<span class="st">"Raw data"</span>),</span>
<span id="cb23-14"><a href="#cb23-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">tableOutput</span>(<span class="st">"preview1"</span>)</span>
<span id="cb23-15"><a href="#cb23-15" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb23-16"><a href="#cb23-16" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb23-17"><a href="#cb23-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-18"><a href="#cb23-18" aria-hidden="true" tabindex="-1"></a><span class="co"># Cleaning the file</span></span>
<span id="cb23-19"><a href="#cb23-19" aria-hidden="true" tabindex="-1"></a>ui_clean <span class="ot"><-</span> <span class="fu">sidebarLayout</span>(</span>
<span id="cb23-20"><a href="#cb23-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarPanel</span>(</span>
<span id="cb23-21"><a href="#cb23-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxInput</span>(<span class="st">"snake"</span>, <span class="st">"Rename columns to snake case?"</span>),</span>
<span id="cb23-22"><a href="#cb23-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxInput</span>(<span class="st">"constant"</span>, <span class="st">"Remove constant columns?"</span>),</span>
<span id="cb23-23"><a href="#cb23-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxInput</span>(<span class="st">"empty"</span>, <span class="st">"Remove empty cols?"</span>)</span>
<span id="cb23-24"><a href="#cb23-24" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb23-25"><a href="#cb23-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">mainPanel</span>(</span>
<span id="cb23-26"><a href="#cb23-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">h3</span>(<span class="st">"Cleaner data"</span>),</span>
<span id="cb23-27"><a href="#cb23-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">tableOutput</span>(<span class="st">"preview2"</span>)</span>
<span id="cb23-28"><a href="#cb23-28" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb23-29"><a href="#cb23-29" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb23-30"><a href="#cb23-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-31"><a href="#cb23-31" aria-hidden="true" tabindex="-1"></a><span class="co"># Downloading the file.</span></span>
<span id="cb23-32"><a href="#cb23-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-33"><a href="#cb23-33" aria-hidden="true" tabindex="-1"></a>ui_download <span class="ot"><-</span> <span class="fu">fluidRow</span>(</span>
<span id="cb23-34"><a href="#cb23-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">column</span>(<span class="at">width =</span> <span class="dv">12</span>, <span class="fu">downloadButton</span>(<span class="st">"download"</span>, <span class="at">class =</span> <span class="st">"btn-block"</span>))</span>
<span id="cb23-35"><a href="#cb23-35" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb23-36"><a href="#cb23-36" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-37"><a href="#cb23-37" aria-hidden="true" tabindex="-1"></a><span class="co"># which get assembled into a single fluidPage():</span></span>
<span id="cb23-38"><a href="#cb23-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-39"><a href="#cb23-39" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb23-40"><a href="#cb23-40" aria-hidden="true" tabindex="-1"></a> ui_upload,</span>
<span id="cb23-41"><a href="#cb23-41" aria-hidden="true" tabindex="-1"></a> ui_clean,</span>
<span id="cb23-42"><a href="#cb23-42" aria-hidden="true" tabindex="-1"></a> ui_download</span>
<span id="cb23-43"><a href="#cb23-43" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb23-44"><a href="#cb23-44" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-45"><a href="#cb23-45" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb23-46"><a href="#cb23-46" aria-hidden="true" tabindex="-1"></a> <span class="co"># Upload ---------------------------------------------------------</span></span>
<span id="cb23-47"><a href="#cb23-47" aria-hidden="true" tabindex="-1"></a> raw <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb23-48"><a href="#cb23-48" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>file)</span>
<span id="cb23-49"><a href="#cb23-49" aria-hidden="true" tabindex="-1"></a> delim <span class="ot"><-</span> <span class="cf">if</span> (input<span class="sc">$</span>delim <span class="sc">==</span> <span class="st">""</span>) <span class="cn">NULL</span> <span class="cf">else</span> input<span class="sc">$</span>delim</span>
<span id="cb23-50"><a href="#cb23-50" aria-hidden="true" tabindex="-1"></a> vroom<span class="sc">::</span><span class="fu">vroom</span>(input<span class="sc">$</span>file<span class="sc">$</span>datapath, <span class="at">delim =</span> delim, <span class="at">skip =</span> input<span class="sc">$</span>skip)</span>
<span id="cb23-51"><a href="#cb23-51" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb23-52"><a href="#cb23-52" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>preview1 <span class="ot"><-</span> <span class="fu">renderTable</span>(<span class="fu">head</span>(<span class="fu">raw</span>(), input<span class="sc">$</span>rows))</span>
<span id="cb23-53"><a href="#cb23-53" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-54"><a href="#cb23-54" aria-hidden="true" tabindex="-1"></a> <span class="co"># Clean step ---------------------------------------------------------</span></span>
<span id="cb23-55"><a href="#cb23-55" aria-hidden="true" tabindex="-1"></a> <span class="co"># Breaking one large reactive up into multiple pieces</span></span>
<span id="cb23-56"><a href="#cb23-56" aria-hidden="true" tabindex="-1"></a> cleaned_names <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb23-57"><a href="#cb23-57" aria-hidden="true" tabindex="-1"></a> out <span class="ot"><-</span> <span class="fu">raw</span>()</span>
<span id="cb23-58"><a href="#cb23-58" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-59"><a href="#cb23-59" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (input<span class="sc">$</span>snake) {</span>
<span id="cb23-60"><a href="#cb23-60" aria-hidden="true" tabindex="-1"></a> <span class="fu">names</span>(out) <span class="ot"><-</span> janitor<span class="sc">::</span><span class="fu">make_clean_names</span>(<span class="fu">names</span>(out))</span>
<span id="cb23-61"><a href="#cb23-61" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-62"><a href="#cb23-62" aria-hidden="true" tabindex="-1"></a> out</span>
<span id="cb23-63"><a href="#cb23-63" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb23-64"><a href="#cb23-64" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-65"><a href="#cb23-65" aria-hidden="true" tabindex="-1"></a> removed_empty <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb23-66"><a href="#cb23-66" aria-hidden="true" tabindex="-1"></a> out <span class="ot"><-</span> <span class="fu">cleaned_names</span>()</span>
<span id="cb23-67"><a href="#cb23-67" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-68"><a href="#cb23-68" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (input<span class="sc">$</span>empty) {</span>
<span id="cb23-69"><a href="#cb23-69" aria-hidden="true" tabindex="-1"></a> out <span class="ot"><-</span> janitor<span class="sc">::</span><span class="fu">remove_empty</span>(out, <span class="st">"cols"</span>)</span>
<span id="cb23-70"><a href="#cb23-70" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-71"><a href="#cb23-71" aria-hidden="true" tabindex="-1"></a> out</span>
<span id="cb23-72"><a href="#cb23-72" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb23-73"><a href="#cb23-73" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-74"><a href="#cb23-74" aria-hidden="true" tabindex="-1"></a> removed_constant <span class="ot"><-</span> <span class="fu">reactive</span>({</span>
<span id="cb23-75"><a href="#cb23-75" aria-hidden="true" tabindex="-1"></a> out <span class="ot"><-</span> <span class="fu">removed_empty</span>()</span>
<span id="cb23-76"><a href="#cb23-76" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-77"><a href="#cb23-77" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (input<span class="sc">$</span>constant) {</span>
<span id="cb23-78"><a href="#cb23-78" aria-hidden="true" tabindex="-1"></a> out <span class="ot"><-</span> janitor<span class="sc">::</span><span class="fu">remove_constant</span>(out)</span>
<span id="cb23-79"><a href="#cb23-79" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-80"><a href="#cb23-80" aria-hidden="true" tabindex="-1"></a> out</span>
<span id="cb23-81"><a href="#cb23-81" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb23-82"><a href="#cb23-82" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-83"><a href="#cb23-83" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>preview2 <span class="ot"><-</span> <span class="fu">renderTable</span>(<span class="fu">head</span>(<span class="fu">removed_constant</span>(), input<span class="sc">$</span>rows))</span>
<span id="cb23-84"><a href="#cb23-84" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-85"><a href="#cb23-85" aria-hidden="true" tabindex="-1"></a> <span class="co"># Download -------------------------------------------------------</span></span>
<span id="cb23-86"><a href="#cb23-86" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>download <span class="ot"><-</span> <span class="fu">downloadHandler</span>(</span>
<span id="cb23-87"><a href="#cb23-87" aria-hidden="true" tabindex="-1"></a> <span class="at">filename =</span> <span class="cf">function</span>() {</span>
<span id="cb23-88"><a href="#cb23-88" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(tools<span class="sc">::</span><span class="fu">file_path_sans_ext</span>(input<span class="sc">$</span>file<span class="sc">$</span>name), <span class="st">".tsv"</span>)</span>
<span id="cb23-89"><a href="#cb23-89" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb23-90"><a href="#cb23-90" aria-hidden="true" tabindex="-1"></a> <span class="at">content =</span> <span class="cf">function</span>(file) {</span>
<span id="cb23-91"><a href="#cb23-91" aria-hidden="true" tabindex="-1"></a> vroom<span class="sc">::</span><span class="fu">vroom_write</span>(<span class="fu">removed_constant</span>(), file)</span>
<span id="cb23-92"><a href="#cb23-92" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-93"><a href="#cb23-93" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb23-94"><a href="#cb23-94" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb23-95"><a href="#cb23-95" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-96"><a href="#cb23-96" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<!--chapter:end:09-uploads-downloads.Rmd-->
</div>
</div>
<div id="dynamic-ui" class="section level1" number="10">
<h1><span class="header-section-number">10</span> Dynamic UI</h1>
<div id="exercises-7" class="section level2 unnumbered">
<h2 class="unnumbered">10.1.6 Exercises</h2>
<ol style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">numericInput</span>(<span class="st">"year"</span>, <span class="st">"year"</span>, <span class="at">value =</span> <span class="dv">2020</span>),</span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">dateInput</span>(<span class="st">"date"</span>, <span class="st">"date"</span>)</span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># From Mastering Shiny Solutions 2021</span></span>
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">observeEvent</span>(input<span class="sc">$</span>year, {</span>
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>year)</span>
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a> date_range <span class="ot"><-</span> <span class="fu">range</span>(<span class="fu">as.Date</span>(<span class="fu">paste0</span>(input<span class="sc">$</span>year, <span class="st">"-01-01"</span>)),</span>
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">as.Date</span>(<span class="fu">paste0</span>(input<span class="sc">$</span>year, <span class="st">"-12-31"</span>)))</span>
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">updateDateInput</span>(session, <span class="st">"date"</span>,</span>
<span id="cb24-15"><a href="#cb24-15" aria-hidden="true" tabindex="-1"></a> <span class="at">min =</span> date_range[<span class="dv">1</span>], </span>
<span id="cb24-16"><a href="#cb24-16" aria-hidden="true" tabindex="-1"></a> <span class="at">max =</span> date_range[<span class="dv">2</span>]</span>
<span id="cb24-17"><a href="#cb24-17" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb24-18"><a href="#cb24-18" aria-hidden="true" tabindex="-1"></a> }) </span>
<span id="cb24-19"><a href="#cb24-19" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb24-20"><a href="#cb24-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-21"><a href="#cb24-21" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(openintro, <span class="at">warn.conflicts =</span> <span class="cn">FALSE</span>)</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a>states <span class="ot"><-</span> <span class="fu">unique</span>(county<span class="sc">$</span>state)</span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"state"</span>, <span class="st">"State"</span>, <span class="at">choices =</span> states),</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"county"</span>, <span class="st">"County"</span>, <span class="at">choices =</span> <span class="cn">NULL</span>)</span>
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-12"><a href="#cb25-12" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb25-13"><a href="#cb25-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">observeEvent</span>(input<span class="sc">$</span>state, {</span>
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>state)</span>
<span id="cb25-15"><a href="#cb25-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># pull out county names</span></span>
<span id="cb25-16"><a href="#cb25-16" aria-hidden="true" tabindex="-1"></a> choices <span class="ot"><-</span> county <span class="sc">%>%</span> </span>
<span id="cb25-17"><a href="#cb25-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(state <span class="sc">==</span> input<span class="sc">$</span>state) <span class="sc">%>%</span></span>
<span id="cb25-18"><a href="#cb25-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(name) <span class="sc">%>%</span> </span>
<span id="cb25-19"><a href="#cb25-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">unique</span>()</span>
<span id="cb25-20"><a href="#cb25-20" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb25-21"><a href="#cb25-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">updateSelectInput</span>(<span class="at">inputId =</span> <span class="st">"county"</span>, <span class="at">choices =</span> choices)</span>
<span id="cb25-22"><a href="#cb25-22" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb25-23"><a href="#cb25-23" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb25-24"><a href="#cb25-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-25"><a href="#cb25-25" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gapminder)</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>continents <span class="ot"><-</span> <span class="fu">unique</span>(gapminder<span class="sc">$</span>continent)</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># add "(All)" to the list of choices</span></span>
<span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"continent"</span>, <span class="st">"Continent"</span>, <span class="at">choices =</span> continents), </span>
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"country"</span>, <span class="st">"Country"</span>, <span class="at">choices =</span> <span class="cn">NULL</span>),</span>
<span id="cb26-10"><a href="#cb26-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">tableOutput</span>(<span class="st">"data"</span>)</span>
<span id="cb26-11"><a href="#cb26-11" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb26-12"><a href="#cb26-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-13"><a href="#cb26-13" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb26-14"><a href="#cb26-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">observeEvent</span>(input<span class="sc">$</span>continent, {</span>
<span id="cb26-15"><a href="#cb26-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>continent)</span>
<span id="cb26-16"><a href="#cb26-16" aria-hidden="true" tabindex="-1"></a> <span class="co"># pull out country names</span></span>
<span id="cb26-17"><a href="#cb26-17" aria-hidden="true" tabindex="-1"></a> choices <span class="ot"><-</span> gapminder <span class="sc">%>%</span> </span>
<span id="cb26-18"><a href="#cb26-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(continent <span class="sc">==</span> input<span class="sc">$</span>continent) <span class="sc">%>%</span></span>
<span id="cb26-19"><a href="#cb26-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(country) <span class="sc">%>%</span> </span>
<span id="cb26-20"><a href="#cb26-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">unique</span>()</span>
<span id="cb26-21"><a href="#cb26-21" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb26-22"><a href="#cb26-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">updateSelectInput</span>(<span class="at">inputId =</span> <span class="st">"country"</span>, <span class="at">choices =</span> choices)</span>
<span id="cb26-23"><a href="#cb26-23" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb26-24"><a href="#cb26-24" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb26-25"><a href="#cb26-25" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>data <span class="ot"><-</span> <span class="fu">renderTable</span>({</span>
<span id="cb26-26"><a href="#cb26-26" aria-hidden="true" tabindex="-1"></a> gapminder <span class="sc">%>%</span> </span>
<span id="cb26-27"><a href="#cb26-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(continent <span class="sc">==</span> input<span class="sc">$</span>continent,</span>
<span id="cb26-28"><a href="#cb26-28" aria-hidden="true" tabindex="-1"></a> country <span class="sc">==</span> input<span class="sc">$</span>country)</span>
<span id="cb26-29"><a href="#cb26-29" aria-hidden="true" tabindex="-1"></a> })</span>
<span id="cb26-30"><a href="#cb26-30" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb26-31"><a href="#cb26-31" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb26-32"><a href="#cb26-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-33"><a href="#cb26-33" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li></li>
</ol>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gapminder)</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>continents <span class="ot"><-</span> <span class="fu">unique</span>(gapminder<span class="sc">$</span>continent)</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># add "(All)" to the list of choices</span></span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"continent"</span>, <span class="st">"Continent"</span>, <span class="at">choices =</span> <span class="fu">c</span>(<span class="fu">as.character</span>(continents), <span class="st">"(All)"</span>)), </span>
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">selectInput</span>(<span class="st">"country"</span>, <span class="st">"Country"</span>, <span class="at">choices =</span> <span class="cn">NULL</span>),</span>
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">tableOutput</span>(<span class="st">"data"</span>)</span>
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="cb27-14"><a href="#cb27-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">observeEvent</span>(input<span class="sc">$</span>continent, {</span>
<span id="cb27-15"><a href="#cb27-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">req</span>(input<span class="sc">$</span>continent)</span>
<span id="cb27-16"><a href="#cb27-16" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb27-17"><a href="#cb27-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (input<span class="sc">$</span>continent <span class="sc">==</span> <span class="st">"(All)"</span>) {</span>
<span id="cb27-18"><a href="#cb27-18" aria-hidden="true" tabindex="-1"></a> <span class="co"># pull out country names</span></span>
<span id="cb27-19"><a href="#cb27-19" aria-hidden="true" tabindex="-1"></a> choices <span class="ot"><-</span> gapminder <span class="sc">%>%</span> </span>
<span id="cb27-20"><a href="#cb27-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(country) <span class="sc">%>%</span> </span>
<span id="cb27-21"><a href="#cb27-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">unique</span>()</span>