-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tex
10120 lines (7610 loc) · 472 KB
/
index.tex
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
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
letterpaper,
DIV=11,
numbers=noendperiod]{scrreprt}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
pdftitle={Shiny Getting Started},
pdfauthor={Mine Çetinkaya-Rundel},
colorlinks=true,
linkcolor={blue},
filecolor={Maroon},
citecolor={Blue},
urlcolor={Blue},
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{241,243,245}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.40,0.46,0.14}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.28,0.35,0.67}{#1}}
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.07,0.07,0.07}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Make \paragraph and \subparagraph free-standing
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newlength{\cslentryspacingunit} % times entry-spacing
\setlength{\cslentryspacingunit}{\parskip}
\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing
{% don't indent paragraphs
\setlength{\parindent}{0pt}
% turn on hanging indent if param 1 is 1
\ifodd #1
\let\oldpar\par
\def\par{\hangindent=\cslhangindent\oldpar}
\fi
% set entry spacing
\setlength{\parskip}{#2\cslentryspacingunit}
}%
{}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
<script src="site_libs/jquery-3.6.0/jquery.min.js"></script>
<script src="site_libs/htmlwidgets-1.5.4/htmlwidgets.js"></script>
<link href="site_libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="site_libs/datatables-binding-0.19/datatables.js"></script>
<link href="site_libs/crosstalk-1.1.1/css/crosstalk.css" rel="stylesheet" />
<script src="site_libs/crosstalk-1.1.1/js/crosstalk.min.js"></script>
\KOMAoption{captions}{tableheading}
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\AtBeginDocument{%
\renewcommand*\contentsname{Table of contents}
\renewcommand*\listfigurename{List of Figures}
\renewcommand*\listtablename{List of Tables}
\renewcommand*\figurename{Figure}
\renewcommand*\tablename{Table}
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\makeatletter
\@ifpackageloaded{tcolorbox}{}{\usepackage[many]{tcolorbox}}
\makeatother
\makeatletter
\@ifundefined{shadecolor}{\definecolor{shadecolor}{rgb}{.97, .97, .97}}
\makeatother
\makeatletter
\makeatother
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\title{Shiny Getting Started}
\author{Mine Çetinkaya-Rundel}
\date{2/25/2022}
\begin{document}
\maketitle
\ifdefined\Shaded\renewenvironment{Shaded}{\begin{tcolorbox}[boxrule=0pt, enhanced, borderline west={3pt}{0pt}{shadecolor}, interior hidden, sharp corners, frame hidden]}{\end{tcolorbox}}\fi
\renewcommand*\contentsname{Table of contents}
{
\hypersetup{linkcolor=}
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{preface}{%
\chapter*{Preface}\label{preface}}
\addcontentsline{toc}{chapter}{Preface}
This is Mine Çetinkaya-Rundel's Getting Started with Shiny course as a
Quarto book.
You will see some text.
You will see code blocks like this.
\begin{Shaded}
\begin{Highlighting}[]
\DecValTok{1} \SpecialCharTok{+} \DecValTok{1}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] 2
\end{verbatim}
\hypertarget{introduction}{%
\chapter*{Introduction}\label{introduction}}
\addcontentsline{toc}{chapter}{Introduction}
This tutorial comprised of four modules will introduce you to Shiny, the
R package for building interactive web apps straight from R.
\hypertarget{what-youll-learn}{%
\section*{What you'll learn}\label{what-youll-learn}}
\addcontentsline{toc}{section}{What you'll learn}
In this module, we'll begin by introducing the basics of Shiny.
So, let's get to it\ldots{}
\part{Hello Shiny}
\hypertarget{getting-started}{%
\chapter{Getting Started}\label{getting-started}}
\hypertarget{here-is-a-shiny-app}{%
\section{Here is a Shiny app}\label{here-is-a-shiny-app}}
And not just any app, an app that you will be able to build by the end
of this tutorial!
https://minecr.shinyapps.io/movies/?showcase=0
1200px
https://minecr.shinyapps.io/movies/
We'll go through the technical details of each component of such an app
throughout the tutorial, but for now let's take a high level view.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/shiny-app-annotated.png}
}
\end{figure}
We have:
\begin{itemize}
\tightlist
\item
a title for the app,
\item
a series of inputs:
\begin{itemize}
\tightlist
\item
some of these inputs use drop down menus for selection,
\item
some are sliders,
\item
some allow for text input, and
\item
some are action buttons
\end{itemize}
\item
and a few outputs:
\begin{itemize}
\tightlist
\item
a plot output that the user can interactively update,
\item
a text output that updates alongside it, and
\item
a data table output that also updates alongside these.
\end{itemize}
\end{itemize}
As much as it looks like there is a lot going on in this sample app, the
app doesn't even scratch the surface of what you can build with Shiny.
I hope you're excited to take it all in!
\hypertarget{background}{%
\section{Background}\label{background}}
Before we get started with Shiny, let's talk background\ldots{}
This tutorial assumes that you are familiar with R as a programming
language.
Additionally, this tutorial uses packages from the
\href{https://tidyverse.org/}{tidyverse}
(e.g.~\href{https://dplyr.tidyverse.org/}{\textbf{dplyr}} for data
wrangling and \href{https://ggplot2.tidyverse.org/}{\textbf{ggplot2}}
for data visualisation). Your Shiny apps can use any package, but if
you'd like to learn more about doing data science with the tidyverse,
see \href{https://www.tidyverse.org/learn/}{here}.
\hypertarget{help}{%
\section{Help}\label{help}}
The tutorial is designed for beginners and many of the exercises have
plenty of scaffolding to help you along the way.
That being said, there are a few other resources that might help your
learning.
\begin{figure}
{\centering \includegraphics[width=0.8\textwidth,height=\textheight]{./images/help.png}
}
\end{figure}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
The first is the
\href{https://github.com/rstudio/cheatsheets/raw/master/shiny.pdf}{\textbf{Shiny
cheatsheet}}. This is a handy-dandy
\href{https://rstudio.com/resources/cheatsheets/}{cheatsheet} that I
recommend you keep close by when building Shiny apps.
\item
The second is the \href{https://shiny.rstudio.com}{\textbf{Shiny
homepage}}. It is \emph{the} place to learn about all things Shiny and
to keep up to date with it as it evolves.
\end{enumerate}
\hypertarget{tips}{%
\section{Tips}\label{tips}}
Also, let's go over three very important tips for learning to develop
Shiny apps:
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
Always \textbf{run the entire script} containing the R code, not just
up to the point where you're developing code. For most exercises in
this tutorial you will be asked to modify or update existing Shiny
code, and even though you might be altering a small portion of the
code, you still need to run the entire app code to create the app.
\item
Sometimes the best way to troubleshoot is to \textbf{run the app and
review the error}. Not only can the error message be informative, but
googling the error message might quickly land you on a solution.
\item
\textbf{Watch out for commas!} This will mean more as you start to
learn Shiny, but just keep in mind, a Shiny error can often be caused
by a missing comma. Thankfully, the RStudio IDE will alert you to most
of these missing comma or similar syntax errors, like the one shown
below.
\end{enumerate}
\begin{figure}
{\centering \includegraphics[width=0.3\textwidth,height=\textheight]{./images/missing-comma-ide.png}
}
\end{figure}
\hypertarget{anatomy-of-a-shiny-app}{%
\section{Anatomy of a Shiny app}\label{anatomy-of-a-shiny-app}}
Alrighty, let's take a look at the anatomy of a Shiny app:
\begin{figure}
{\centering \includegraphics[width=0.8\textwidth,height=\textheight]{./images/anatomy.png}
}
\end{figure}
\begin{itemize}
\item
We start by loading any necessary packages, one of which is
necessarily shiny.
\item
Then we lay out the user interface with a ui object that controls the
appearance of our app.
\item
And we define the server function that contains instructions needed to
build the app.
\item
We end each Shiny app script with a call to the shinyApp() function
that puts these two components together to create the Shiny app
object.
\end{itemize}
\hypertarget{data}{%
\section{Data}\label{data}}
In this tutorial we will build a simple movie browser app.
We will use data from the movies dataset, which combines data from two
websites: the Internet Movie Database, commonly known as IMDB, and
Rotten Tomatoes. The observations are a random sample of 651 movies
released in the US between 1970 and 2014.
So where does the loading of the data happen in an app?
\hypertarget{revisit}{%
\section{Revisit}\label{revisit}}
Let's revisit the app layout from a couple sections back.
\begin{figure}
{\centering \includegraphics[width=0.8\textwidth,height=\textheight]{./images/revisit.png}
}
\end{figure}
We load the data before \texttt{ui} and \texttt{server} definitions so
that it can be used in both.
Alright, time for some practice!
\hypertarget{practice-whats-in-an-app}{%
\section{Practice: What's in an app?}\label{practice-whats-in-an-app}}
Which of these is not generally a part of the Shiny app architecture?''
- A function that installs an R package - User interface - Server
function - A function that creates Shiny app objects
Answer: A function that installs an R package. You don't want to
reinstall the package every time you run your app, so you should do this
once in your console instead of within your Shiny app
\hypertarget{practice-first-peek-under-the-hood}{%
\section{Practice: First peek under the
hood}\label{practice-first-peek-under-the-hood}}
Below you can see the complete code to reproduce the app we introduced
in the previous section. Now you get to interact with the app yourself,
and make small adjustments to it.
I've created an RStudio Cloud Project for you to test drive this code.
Click the button below to be taken to your RStudio Workspace, select
\textbf{1.1 First peek under the hood} from the Project list, and follow
the exercise instructions below to get started!
\emph{Navigate to the project called \textbf{1-1 First peek under the
hood} after clicking the button below}
\href{https://rstudio.cloud/spaces/81721/join?access_code=I4VJaNsKfTqR3Td9hLP7E1nz8\%2FtMg6Xbw9Bgqumv}{
Go to RStudio Cloud Workspace}
\hypertarget{your-turn}{%
\section{Your turn}\label{your-turn}}
\begin{itemize}
\tightlist
\item
Once your RStudio Cloud Project is open to the script \texttt{app.R},
click to run the code and generate the app.
\item
Play with the input selectors for the Y-axis and the X-axis and
observe how the output changes.
\item
Close the app by closing the pop-up window or clicking on the red Stop
button in the viewer.
\item
Locate the relevant lines of code in \texttt{app.R} that build the
selector widget for the Y-axis. This is in a \texttt{selectInput()}
function starting on Line 20, underneath the comment
\texttt{\#\ Select\ variable\ for\ y-axis}. Note that this function
takes four arguments: \texttt{inputId}, \texttt{label},
\texttt{choices}, and \texttt{selected}. We'll discuss what each of
these mean in detail shortly. For now, change the \texttt{selected}
argument to \texttt{imdb\_rating}, save your changes, and run the app
again by clicking on . What changed?
\begin{itemize}
\tightlist
\item
If you get an error when you try to rerun the app, you can either
try to debug the issue by tracing back your steps or delete
everything in \texttt{app.R} and copy and paste the code below into
\texttt{app.R}. This will get you back to your starting point. You
can use this ``start over'' approach for any of the exercises in
this tutorial.
\end{itemize}
\item
Now locate the relevant lines of code in \texttt{app.R} that build the
selector widget for the X-axis. This is also in a
\texttt{selectInput()} function, starting on Line 27, underneath the
comment \texttt{\#\ Select\ variable\ for\ x-axis}. Change the
\texttt{selected} argument to \texttt{imdb\_rating} as well, save your
changes, and run the app again. What changed?
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Load packages {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{library}\NormalTok{(shiny)}
\FunctionTok{library}\NormalTok{(ggplot2)}
\CommentTok{\# Load data {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{load}\NormalTok{(}\StringTok{"movies.RData"}\NormalTok{)}
\CommentTok{\# Define UI {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\NormalTok{ui }\OtherTok{\textless{}{-}} \FunctionTok{fluidPage}\NormalTok{(}
\FunctionTok{sidebarLayout}\NormalTok{(}
\CommentTok{\# Inputs: Select variables to plot}
\FunctionTok{sidebarPanel}\NormalTok{(}
\CommentTok{\# Select variable for y{-}axis}
\FunctionTok{selectInput}\NormalTok{(}
\AttributeTok{inputId =} \StringTok{"y"}\NormalTok{,}
\AttributeTok{label =} \StringTok{"Y{-}axis:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"imdb\_rating"}\NormalTok{, }\StringTok{"imdb\_num\_votes"}\NormalTok{, }\StringTok{"critics\_score"}\NormalTok{, }\StringTok{"audience\_score"}\NormalTok{, }\StringTok{"runtime"}\NormalTok{),}
\AttributeTok{selected =} \StringTok{"audience\_score"}
\NormalTok{ ),}
\CommentTok{\# Select variable for x{-}axis}
\FunctionTok{selectInput}\NormalTok{(}
\AttributeTok{inputId =} \StringTok{"x"}\NormalTok{,}
\AttributeTok{label =} \StringTok{"X{-}axis:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"imdb\_rating"}\NormalTok{, }\StringTok{"imdb\_num\_votes"}\NormalTok{, }\StringTok{"critics\_score"}\NormalTok{, }\StringTok{"audience\_score"}\NormalTok{, }\StringTok{"runtime"}\NormalTok{),}
\AttributeTok{selected =} \StringTok{"critics\_score"}
\NormalTok{ )}
\NormalTok{ ),}
\CommentTok{\# Output: Show scatterplot}
\FunctionTok{mainPanel}\NormalTok{(}
\FunctionTok{plotOutput}\NormalTok{(}\AttributeTok{outputId =} \StringTok{"scatterplot"}\NormalTok{)}
\NormalTok{ )}
\NormalTok{ )}
\NormalTok{)}
\CommentTok{\# Define server {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\NormalTok{server }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(input, output, session) \{}
\NormalTok{ output}\SpecialCharTok{$}\NormalTok{scatterplot }\OtherTok{\textless{}{-}} \FunctionTok{renderPlot}\NormalTok{(\{}
\FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data =}\NormalTok{ movies, }\FunctionTok{aes\_string}\NormalTok{(}\AttributeTok{x =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{x, }\AttributeTok{y =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{y)) }\SpecialCharTok{+}
\FunctionTok{geom\_point}\NormalTok{()}
\NormalTok{ \})}
\NormalTok{\}}
\CommentTok{\# Create a Shiny app object {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{shinyApp}\NormalTok{(}\AttributeTok{ui =}\NormalTok{ ui, }\AttributeTok{server =}\NormalTok{ server)}
\end{Highlighting}
\end{Shaded}
\hypertarget{user-interface-ui}{%
\section{User interface (UI)}\label{user-interface-ui}}
\hypertarget{section}{%
\subsection{}\label{section}}
In this section we'll build the user interface of a simple app.
However, before we get into the weeds of building a user interface,
let's revisit the anatomy of a Shiny app.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/app-selectinput-scatterplot.png}
}
\end{figure}
\begin{itemize}
\item
The user interface, that we'll refer to as the ``UI'' going forward,
defines and lays out the inputs of your app where users can make their
selections. It also lays out the outputs.
\item
The server function, on the other hand, calculates outputs and
performs any other calculations needed for the outputs.
\end{itemize}
\hypertarget{example}{%
\subsection{Example}\label{example}}
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/ui-to-scatterplot.png}
}
\end{figure}
For example, if your app features a plot the code for building that plot
lives in the server function. But the setup for the user defined inputs
for the plot, as well as information on where physically on the app the
plot should appear, are defined in the UI.
\hypertarget{section-1}{%
\subsection{}\label{section-1}}
Here is the app we'll work with in this section and the code that builds
the UI of that app.
Since this is too much code to parse, we'll explore individual
components of the UI one by one.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/app-selectinput-scatterplot.png}
}
\end{figure}
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/ui-selectinput-scatterplot.png}
}
\end{figure}
\hypertarget{fluidpage}{%
\subsection{\texorpdfstring{\texttt{fluidPage()}}{fluidPage()}}\label{fluidpage}}
At the outermost layer of our UI definition we begin with the
\texttt{fluidPage()} function.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/fluidPage.png}
}
\end{figure}
The \texttt{fluidPage()} function creates a fluid page layout consisting
of rows and columns. Rows make sure that elements in them appear on the
same line. Columns within these rows define how much horizontal space
each element should occupy.
Fluid pages scale their components in realtime to fill all available
browser width, which means you, the app developer, don't need to worry
about defining relative widths for individual app components.
As always, for more information on arguments to this function, you can
view the R function help by typing \texttt{?fluidPage} in your R console
or visiting the function reference page on the package website
\href{https://shiny.rstudio.com/reference/shiny/latest/}{here}.
\hypertarget{layout}{%
\subsection{Layout}\label{layout}}
Next, we define the layout of our app with \texttt{sidebarLayout()}.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/layout.png}
}
\end{figure}
Shiny includes a number of options for laying out the components of an
application. The default layout, the one we're using in our example app,
is a layout with a sidebar, that you can define with the
\texttt{sidebarLayout()} function.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/layout-app.png}
}
\end{figure}
This is a simple layout with a narrow sidebar for inputs and a wider
main area for output.
Under the hood, Shiny implements layout features available in Bootstrap
2, which is a popular HTML/CSS framework. However the nice thing about
working in Shiny is that no prior experience with Bootstrap is
necessary.
To learn more about various layouts, I recommend reviewing the
\href{https://shiny.rstudio.com/articles/layout-guide.html}{Application
Layout Guide article} at \url{shiny.rstudio.com}.
\hypertarget{input-controls}{%
\subsection{Input controls}\label{input-controls}}
Next we define our sidebar panel containing input controls.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/input-controls.png}
}
\end{figure}
\hypertarget{section-2}{%
\subsection{}\label{section-2}}
This panel contains two dropdown menus created with the
\texttt{selectInput()} function.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/input-dropdowns.png}
}
\end{figure}
\hypertarget{section-3}{%
\subsection{}\label{section-3}}
Let's take a look at one of the \texttt{selectInput} widgets a little
more closely.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/input-closeup.png}
}
\end{figure}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
The first argument is the \texttt{inputId}, which is the input value
that the app will internally use to access the value selected by the
user.
\item
The second argument is the \texttt{label}, which is the display label
that the user sees.
\item
The third argument is the list of \texttt{choices} the user will
choose from. In this app, these are variable names from the movies
dataset.
\item
And lastly we specify a default selection from that list with
\texttt{selected}.
\end{enumerate}
\hypertarget{main-panel}{%
\subsection{Main Panel}\label{main-panel}}
The final component of our UI is \texttt{mainPanel()}.
\begin{figure}
{\centering \includegraphics[width=1\textwidth,height=\textheight]{./images/main-panel.png}
}
\end{figure}
Currently the main panel contains only one component, a plot output.
We'll talk about how this plot is built later in the tutorial.
Next, let's practice building an app UI!
\hypertarget{practice-extend-the-ui}{%
\subsection{Practice: Extend the UI}\label{practice-extend-the-ui}}
We'll start with a simplified version of the app you saw in the previous
exercise. In this app a \texttt{selectInput()} widget is used to allow
the user to select which variables should be plotted on the \texttt{x}
and \texttt{y} axes of the scatterplot.
The \texttt{selectInput()} function has the following arguments:
\begin{itemize}
\tightlist
\item
an \texttt{inputId} that is used to refer to the input parameter when
building the scatterplot,
\item
a list of \texttt{choices} to pick from (which must match variable
names in the data frame),
\item
and a \texttt{selected} choice for when the app first launches.
\end{itemize}
\hypertarget{your-turn-1}{%
\subsubsection{Your turn}\label{your-turn-1}}
Modify the Shiny app code in \texttt{app.R} / shown below:
\begin{itemize}
\tightlist
\item
In the \texttt{ui}:
\begin{itemize}
\tightlist
\item
Add a new \texttt{selectInput} widget to color the points by a
choice of the following variables: \texttt{"title\_type"},
\texttt{"genre"}, \texttt{"mpaa\_rating"},
\texttt{"critics\_rating"}, \texttt{"audience\_rating"}.
\item
Make the default selection \texttt{"mpaa\_rating"}.
\item
Use \texttt{"z"} as the \texttt{inputId}.
\item
\texttt{label} can be whatever you like.
\end{itemize}
\item
In the \texttt{server}:
\begin{itemize}
\tightlist
\item
Set the color argument in \texttt{ggplot()} aesthetic mappings to
\texttt{input\$z}.
\end{itemize}
\end{itemize}
\emph{Complete this exercise by opening up the RStudio Project titled
\textbf{1-2a Extend the UI} within your RStudio Cloud Workspace}
\href{https://rstudio.cloud/spaces/81721/join?access_code=I4VJaNsKfTqR3Td9hLP7E1nz8\%2FtMg6Xbw9Bgqumv}{
Go to RStudio Cloud Workspace}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Load packages {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{library}\NormalTok{(shiny)}
\FunctionTok{library}\NormalTok{(ggplot2)}
\CommentTok{\# Load data {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{load}\NormalTok{(}\StringTok{"movies.RData"}\NormalTok{)}
\CommentTok{\# Define UI {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\NormalTok{ui }\OtherTok{\textless{}{-}} \FunctionTok{fluidPage}\NormalTok{(}
\FunctionTok{sidebarLayout}\NormalTok{(}
\CommentTok{\# Inputs: Select variables to plot}
\FunctionTok{sidebarPanel}\NormalTok{(}
\CommentTok{\# Select variable for y{-}axis}
\FunctionTok{selectInput}\NormalTok{(}\AttributeTok{inputId =} \StringTok{"y"}\NormalTok{, }
\AttributeTok{label =} \StringTok{"Y{-}axis:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"imdb\_rating"}\NormalTok{, }\StringTok{"imdb\_num\_votes"}\NormalTok{, }\StringTok{"critics\_score"}\NormalTok{, }\StringTok{"audience\_score"}\NormalTok{, }\StringTok{"runtime"}\NormalTok{), }
\AttributeTok{selected =} \StringTok{"audience\_score"}\NormalTok{),}
\CommentTok{\# Select variable for x{-}axis}
\FunctionTok{selectInput}\NormalTok{(}\AttributeTok{inputId =} \StringTok{"x"}\NormalTok{, }
\AttributeTok{label =} \StringTok{"X{-}axis:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"imdb\_rating"}\NormalTok{, }\StringTok{"imdb\_num\_votes"}\NormalTok{, }\StringTok{"critics\_score"}\NormalTok{, }\StringTok{"audience\_score"}\NormalTok{, }\StringTok{"runtime"}\NormalTok{), }
\AttributeTok{selected =} \StringTok{"critics\_score"}\NormalTok{),}
\CommentTok{\# Select variable for color}
\FunctionTok{selectInput}\NormalTok{(}\AttributeTok{inputId =} \StringTok{"\_\_\_"}\NormalTok{, }
\AttributeTok{label =} \StringTok{"\_\_\_\_"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(\_\_\_),}
\AttributeTok{selected =} \StringTok{"\_\_\_"}\NormalTok{)}
\NormalTok{ ),}
\CommentTok{\# Output: Show scatterplot}
\FunctionTok{mainPanel}\NormalTok{(}
\FunctionTok{plotOutput}\NormalTok{(}\AttributeTok{outputId =} \StringTok{"scatterplot"}\NormalTok{)}
\NormalTok{ )}
\NormalTok{ )}
\NormalTok{)}
\CommentTok{\# Define server {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\NormalTok{server }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(input, output, session) \{}
\NormalTok{ output}\SpecialCharTok{$}\NormalTok{scatterplot }\OtherTok{\textless{}{-}} \FunctionTok{renderPlot}\NormalTok{(\{}
\FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data =}\NormalTok{ movies, }\FunctionTok{aes\_string}\NormalTok{(}\AttributeTok{x =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{x, }\AttributeTok{y =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{y,}
\AttributeTok{color =}\NormalTok{ \_\_\_)) }\SpecialCharTok{+}
\FunctionTok{geom\_point}\NormalTok{()}
\NormalTok{ \})}
\NormalTok{ \}}
\CommentTok{\# Create a Shiny app object {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{shinyApp}\NormalTok{(}\AttributeTok{ui =}\NormalTok{ ui, }\AttributeTok{server =}\NormalTok{ server)}
\end{Highlighting}
\end{Shaded}
Show solution
See the following code chunk for the solution to the exercise above.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Load packages {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{library}\NormalTok{(shiny)}
\FunctionTok{library}\NormalTok{(ggplot2)}
\CommentTok{\# Load data {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{load}\NormalTok{(}\StringTok{"movies.RData"}\NormalTok{)}
\CommentTok{\# Define UI {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\NormalTok{ui }\OtherTok{\textless{}{-}} \FunctionTok{fluidPage}\NormalTok{(}
\FunctionTok{sidebarLayout}\NormalTok{(}
\CommentTok{\# Inputs: Select variables to plot}
\FunctionTok{sidebarPanel}\NormalTok{(}
\CommentTok{\# Select variable for y{-}axis}
\FunctionTok{selectInput}\NormalTok{(}\AttributeTok{inputId =} \StringTok{"y"}\NormalTok{, }
\AttributeTok{label =} \StringTok{"Y{-}axis:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"imdb\_rating"}\NormalTok{, }\StringTok{"imdb\_num\_votes"}\NormalTok{, }\StringTok{"critics\_score"}\NormalTok{, }\StringTok{"audience\_score"}\NormalTok{, }\StringTok{"runtime"}\NormalTok{), }
\AttributeTok{selected =} \StringTok{"audience\_score"}\NormalTok{),}
\CommentTok{\# Select variable for x{-}axis}
\FunctionTok{selectInput}\NormalTok{(}\AttributeTok{inputId =} \StringTok{"x"}\NormalTok{, }
\AttributeTok{label =} \StringTok{"X{-}axis:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"imdb\_rating"}\NormalTok{, }\StringTok{"imdb\_num\_votes"}\NormalTok{, }\StringTok{"critics\_score"}\NormalTok{, }\StringTok{"audience\_score"}\NormalTok{, }\StringTok{"runtime"}\NormalTok{), }
\AttributeTok{selected =} \StringTok{"critics\_score"}\NormalTok{),}
\CommentTok{\# Select variable for color}
\FunctionTok{selectInput}\NormalTok{(}\AttributeTok{inputId =} \StringTok{"z"}\NormalTok{, }
\AttributeTok{label =} \StringTok{"Color by:"}\NormalTok{,}
\AttributeTok{choices =} \FunctionTok{c}\NormalTok{(}\StringTok{"title\_type"}\NormalTok{, }\StringTok{"genre"}\NormalTok{, }\StringTok{"mpaa\_rating"}\NormalTok{, }\StringTok{"critics\_rating"}\NormalTok{, }\StringTok{"audience\_rating"}\NormalTok{),}
\AttributeTok{selected =} \StringTok{"mpaa\_rating"}\NormalTok{)}
\NormalTok{ ),}
\CommentTok{\# Output: Show scatterplot}
\FunctionTok{mainPanel}\NormalTok{(}
\FunctionTok{plotOutput}\NormalTok{(}\AttributeTok{outputId =} \StringTok{"scatterplot"}\NormalTok{)}
\NormalTok{ )}
\NormalTok{ )}
\NormalTok{)}
\CommentTok{\# Define server {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\NormalTok{server }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(input, output, session) \{}
\NormalTok{ output}\SpecialCharTok{$}\NormalTok{scatterplot }\OtherTok{\textless{}{-}} \FunctionTok{renderPlot}\NormalTok{(\{}
\FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data =}\NormalTok{ movies, }\FunctionTok{aes\_string}\NormalTok{(}\AttributeTok{x =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{x, }\AttributeTok{y =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{y,}
\AttributeTok{color =}\NormalTok{ input}\SpecialCharTok{$}\NormalTok{z)) }\SpecialCharTok{+}
\FunctionTok{geom\_point}\NormalTok{()}
\NormalTok{ \})}
\NormalTok{ \}}
\CommentTok{\# Create a Shiny app object {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\FunctionTok{shinyApp}\NormalTok{(}\AttributeTok{ui =}\NormalTok{ ui, }\AttributeTok{server =}\NormalTok{ server)}
\end{Highlighting}
\end{Shaded}
\hypertarget{practice-extend-the-ui-further}{%
\subsection{Practice: Extend the UI
further}\label{practice-extend-the-ui-further}}
The potential variables the user can select for the \texttt{x} and
\texttt{y} axes and \texttt{color} currently appear in the UI of the app
the same way that they are spelled in the data frame header. However we
might want to label them in a way that is more human readable. We can
achieve this using named vectors for the \texttt{choices} argument, in
the format of \texttt{"Human\ readable\ label"\ =\ "variable\_name"}.