-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiny_Tutorial.html
More file actions
894 lines (774 loc) · 39.3 KB
/
Shiny_Tutorial.html
File metadata and controls
894 lines (774 loc) · 39.3 KB
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Dani Cosme & Sam Chavez" />
<title>Shiny Tutorial</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/flatly.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #0000ff; } /* Keyword */
code > span.ch { color: #008080; } /* Char */
code > span.st { color: #008080; } /* String */
code > span.co { color: #008000; } /* Comment */
code > span.ot { color: #ff4000; } /* Other */
code > span.al { color: #ff0000; } /* Alert */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #008000; font-weight: bold; } /* Warning */
code > span.cn { } /* Constant */
code > span.sc { color: #008080; } /* SpecialChar */
code > span.vs { color: #008080; } /* VerbatimString */
code > span.ss { color: #008080; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { } /* Variable */
code > span.cf { color: #0000ff; } /* ControlFlow */
code > span.op { } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #ff4000; } /* Preprocessor */
code > span.do { color: #008000; } /* Documentation */
code > span.an { color: #008000; } /* Annotation */
code > span.cv { color: #008000; } /* CommentVar */
code > span.at { } /* Attribute */
code > span.in { color: #008000; } /* Information */
</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 60px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 65px;
margin-top: -65px;
}
.section h2 {
padding-top: 65px;
margin-top: -65px;
}
.section h3 {
padding-top: 65px;
margin-top: -65px;
}
.section h4 {
padding-top: 65px;
margin-top: -65px;
}
.section h5 {
padding-top: 65px;
margin-top: -65px;
}
.section h6 {
padding-top: 65px;
margin-top: -65px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Sam's Silly Website</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About Me</a>
</li>
<li>
<a href="projects.html">Projects</a>
</li>
<li>
<a href="HomerResume.pdf">Street Cred</a>
</li>
<li>
<a href="presentations.html">Resources</a>
</li>
<li>
<a href="shiny.html">Shiny</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Shiny Tutorial</h1>
<h4 class="author"><em>Dani Cosme & Sam Chavez</em></h4>
<h4 class="date"><em>June 7, 2018</em></h4>
</div>
<p></br></p>
<div id="the-structure-of-a-shiny-app" class="section level2">
<h2>The Structure of a Shiny App</h2>
<p>Shiny apps are contained in a single, integrated script called app.R.</p>
<p>This script has at least three components:</p>
<ul>
<li>a user interface object (can be a call to a separate ui.R script)</li>
<li>a server function (can be a call to a separate server.R script)</li>
<li>a call to the shinyApp function</li>
<li>optional: extra stuff in your global environment (can be a call to a separate global.R script)</li>
<li>If you want to host your shiny app, save this script as app.R and put it in it’s own folder. The name for that folder will be the name of your shiny app.</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># If you choose to host this app online, you'll need to copy everything in this code chunk and save it to app.R in it's own folder. Then, set your working directory to the saved folder. Finally, use the command 'rsconnect::deployApp()' to host your app.</span>
<span class="co"># Here's a user interface (ui) component</span>
<span class="kw">library</span>(shiny)
<span class="co"># Define UI for app that draws a histogram ----</span>
ui <-<span class="st"> </span><span class="kw">fluidPage</span>(
<span class="co"># App title ----</span>
<span class="kw">titlePanel</span>(<span class="st">"Hello Shiny!"</span>),
<span class="co"># Sidebar layout with input and output definitions ----</span>
<span class="kw">sidebarLayout</span>(
<span class="co"># Sidebar panel for inputs ----</span>
<span class="kw">sidebarPanel</span>(
<span class="co"># Input: Slider for the number of bins ----</span>
<span class="kw">sliderInput</span>(<span class="dt">inputId =</span> <span class="st">"bins"</span>,
<span class="dt">label =</span> <span class="st">"Number of bins:"</span>,
<span class="dt">min =</span> <span class="dv">1</span>,
<span class="dt">max =</span> <span class="dv">50</span>,
<span class="dt">value =</span> <span class="dv">30</span>)
),
<span class="co"># Main panel for displaying outputs ----</span>
<span class="kw">mainPanel</span>(
<span class="co"># Output: Histogram ----</span>
<span class="kw">plotOutput</span>(<span class="dt">outputId =</span> <span class="st">"distPlot"</span>)
)
)
)
<span class="co"># Here's a server component</span>
<span class="co"># Define server logic required to draw a histogram ----</span>
server <-<span class="st"> </span><span class="cf">function</span>(input, output) {
<span class="co"># Histogram of the Old Faithful Geyser Data ----</span>
<span class="co"># with requested number of bins</span>
<span class="co"># This expression that generates a histogram is wrapped in a call</span>
<span class="co"># to renderPlot to indicate that:</span>
<span class="co">#</span>
<span class="co"># 1. It is "reactive" and therefore should be automatically</span>
<span class="co"># re-executed when inputs (input$bins) change</span>
<span class="co"># 2. Its output type is a plot</span>
output<span class="op">$</span>distPlot <-<span class="st"> </span><span class="kw">renderPlot</span>({
x <-<span class="st"> </span>faithful<span class="op">$</span>waiting
bins <-<span class="st"> </span><span class="kw">seq</span>(<span class="kw">min</span>(x), <span class="kw">max</span>(x), <span class="dt">length.out =</span> input<span class="op">$</span>bins <span class="op">+</span><span class="st"> </span><span class="dv">1</span>)
<span class="kw">hist</span>(x, <span class="dt">breaks =</span> bins, <span class="dt">col =</span> <span class="st">"#75AADB"</span>, <span class="dt">border =</span> <span class="st">"white"</span>,
<span class="dt">xlab =</span> <span class="st">"Waiting time to next eruption (in mins)"</span>,
<span class="dt">main =</span> <span class="st">"Histogram of waiting times"</span>)
})
}
<span class="co"># When you've made both components, you're ready to call shinyApp</span>
<span class="kw">shinyApp</span>(<span class="dt">ui =</span> ui, <span class="dt">server =</span> server)</code></pre></div>
<p><br></p>
<p></br></p>
</div>
<div id="layout" class="section level2">
<h2>Layout</h2>
<p>The layout of a shiny app is specified within the user interface with the function fluidPage() using the panel options.</p>
<ul>
<li>Specify a title with titlePanel(“my title”)</li>
<li>Parition your layout into sidebar & main panels</li>
<li>Change the position of a panel</li>
<li>You can use some HTML5 tags without the <> brackets too</li>
<li>Use ‘align’ to change text justification</li>
<li>Use p() to create paragraphs</li>
</ul>
<p>And so much more…</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">ui <-<span class="st"> </span><span class="kw">fluidPage</span>(
<span class="kw">titlePanel</span>(<span class="kw">em</span>(<span class="st">"my italicized title"</span>, <span class="dt">align =</span> <span class="st">"center"</span>)),
<span class="kw">sidebarLayout</span>(<span class="dt">position =</span> <span class="st">"left"</span>,
<span class="kw">sidebarPanel</span>(<span class="kw">strong</span>(<span class="st">"my bold sidebar</span><span class="ch">\n</span><span class="st">and a picture</span><span class="ch">\n</span><span class="st">of a shiny penny"</span>),
<span class="kw">img</span>(<span class="dt">src =</span> <span class="st">"https://www.thefarmersbank.net/wp-content/uploads/2018/04/2005-Penny-Uncirculated-Obverse.png"</span>, <span class="dt">height =</span> <span class="dv">100</span>, <span class="dt">width =</span> <span class="dv">100</span>)),
<span class="kw">mainPanel</span>(
<span class="kw">h1</span>(<span class="st">"most important output"</span>),
<span class="kw">br</span>(),
<span class="kw">p</span>(<span class="st">"this is a paragraph that says nothing in particular. it's just a bunch of sentences to illustrate a point about separating text. watch, we're going to copy and paste it below as another paragraph."</span>),
<span class="kw">p</span>(<span class="st">"this is a paragraph that says nothing in particular. it's just a bunch of sentences to illustrate a point about separating text."</span>),
<span class="kw">h2</span>(<span class="st">"less important output"</span>),
<span class="kw">h3</span>(<span class="kw">div</span>(<span class="st">"here's smaller text in blue"</span>, <span class="dt">style =</span> <span class="st">"color:blue"</span>)),
<span class="kw">h4</span>(<span class="kw">p</span>(<span class="st">"here's a tiny text paragraph with just"</span>, <span class="kw">span</span>(<span class="st">"a few orange words,"</span>, <span class="dt">style =</span> <span class="st">"color:orange"</span>), <span class="st">"if you're into that."</span>))
)
)
)
<span class="co"># Let's see what we created</span>
<span class="co"># Uncomment the lines below and run this chunk only</span>
server <-<span class="st"> </span><span class="cf">function</span>(input, output) {
}
<span class="kw">shinyApp</span>(ui, server)</code></pre></div>
<iframe src="https://schavez.shinyapps.io/layout/" style="border: none; width: 800px; height: 500px">
</iframe>
<p><br></p>
<p></br></p>
</div>
<div id="control-widgets" class="section level2">
<h2>Control Widgets</h2>
<p>Widgets are the interactive parts of a web application. When we include widgets in our shiny app, the user can manipulate features of the UI. Later, we’ll learn how to make the output of our app reflect these changes dynamically.</p>
<ul>
<li>Shiny has some built-in widgets. Below, we’ve listed their names and corresponding functions.</li>
</ul>
<table>
<thead>
<tr class="header">
<th>Function</th>
<th>Widget</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>actionButton</td>
<td>Action Button</td>
</tr>
<tr class="even">
<td>checkboxGroupInput</td>
<td>A group of check boxes</td>
</tr>
<tr class="odd">
<td>checkboxInput</td>
<td>A single check box</td>
</tr>
<tr class="even">
<td>dateInput</td>
<td>A calendar to aid date selection</td>
</tr>
<tr class="odd">
<td>dateRangeInput</td>
<td>A pair of calendars for selecting a date range</td>
</tr>
<tr class="even">
<td>fileInput</td>
<td>A file upload control wizard</td>
</tr>
<tr class="odd">
<td>helpText</td>
<td>Help text that can be added to an input form</td>
</tr>
<tr class="even">
<td>numericInput</td>
<td>A field to enter numbers</td>
</tr>
<tr class="odd">
<td>radioButtons</td>
<td>A set of radio buttons</td>
</tr>
<tr class="even">
<td>selectInput</td>
<td>A box with choices to select from</td>
</tr>
<tr class="odd">
<td>sliderInput</td>
<td>A slider bar</td>
</tr>
<tr class="even">
<td>submitButton</td>
<td>A submit button</td>
</tr>
<tr class="odd">
<td>textInput</td>
<td>A field to enter text</td>
</tr>
</tbody>
</table>
<p>Note: If that’s not enough for you, install the shinyWidgets package for more!</p>
<ul>
<li><p>To add control widgets to your app, put one of the widget functions in the sidebarPanel or mainPanel of your UI. Every widget needs these ingredients:</p>
<ul>
<li>a name (charcter string that you call later)</li>
<li>label (character string that the app user will see)</li>
</ul></li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co">#install.packages("shinyWidgets")</span>
<span class="kw">library</span>(shinyWidgets)
<span class="kw">library</span>(shiny)
<span class="co"># Define UI ----</span>
ui <-<span class="st"> </span><span class="kw">fluidPage</span>(
<span class="kw">titlePanel</span>(<span class="st">"A Bunch of Widgets"</span>),
<span class="kw">fluidRow</span>(
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">h3</span>(<span class="st">"Button Widgets"</span>),
<span class="kw">actionButton</span>(<span class="st">"action"</span>, <span class="st">"Action"</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">submitButton</span>(<span class="st">"Submit"</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">actionBttn</span>(<span class="st">"fancy action"</span>, <span class="st">"Danger! Don't Click Me"</span>,
<span class="dt">style =</span> <span class="st">"jelly"</span>, <span class="dt">color =</span> <span class="st">"danger"</span>, <span class="dt">size =</span> <span class="st">"lg"</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">actionGroupButtons</span>(
<span class="dt">inputIds =</span> <span class="kw">c</span>(<span class="st">"btn1"</span>, <span class="st">"btn2"</span>, <span class="st">"btn3"</span>),
<span class="dt">labels =</span> <span class="kw">list</span>(<span class="st">"Action 1"</span>, <span class="st">"Action 2"</span>, tags<span class="op">$</span><span class="kw">span</span>(<span class="kw">icon</span>(<span class="st">"gear"</span>), <span class="st">"Action 3"</span>)),
<span class="dt">status =</span> <span class="st">"primary"</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">downloadBttn</span>(<span class="st">"download button"</span>, <span class="st">"Click to Download"</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">prettyRadioButtons</span>(<span class="dt">inputId =</span> <span class="st">"radio4"</span>, <span class="dt">label =</span> <span class="st">"Click me!"</span>,
<span class="dt">choices =</span> <span class="kw">c</span>(<span class="st">"Click me !"</span>, <span class="st">"Me !"</span>, <span class="st">"Or me !"</span>),
<span class="dt">outline =</span> <span class="ot">TRUE</span>,
<span class="dt">plain =</span> <span class="ot">TRUE</span>, <span class="dt">icon =</span> <span class="kw">icon</span>(<span class="st">"thumbs-up"</span>))),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">h3</span>(<span class="st">"Checkbox Widgets"</span>),
<span class="kw">checkboxInput</span>(<span class="st">"checkbox"</span>, <span class="st">"Choice A"</span>, <span class="dt">value =</span> <span class="ot">TRUE</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">checkboxGroupInput</span>(<span class="st">"checkGroup"</span>, <span class="st">"Checkbox group"</span>,
<span class="dt">choices =</span> <span class="kw">list</span>(<span class="st">"Choice 1"</span> =<span class="st"> </span><span class="dv">1</span>,
<span class="st">"Choice 2"</span> =<span class="st"> </span><span class="dv">2</span>,
<span class="st">"Choice 3"</span> =<span class="st"> </span><span class="dv">3</span>), <span class="dt">selected =</span> <span class="dv">1</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">prettyCheckbox</span>(<span class="dt">inputId =</span> <span class="st">"checkbox2"</span>,
<span class="dt">label =</span> <span class="st">"Click me!"</span>, <span class="dt">thick =</span> <span class="ot">TRUE</span>,
<span class="dt">animation =</span> <span class="st">"pulse"</span>, <span class="dt">status =</span> <span class="st">"info"</span>,
<span class="dt">fill =</span> T, <span class="dt">shape =</span> <span class="st">"round"</span>, <span class="dt">bigger =</span> T),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">prettyCheckboxGroup</span>(<span class="dt">inputId =</span> <span class="st">"checkgroup5"</span>,
<span class="dt">label =</span> <span class="st">"Click me!"</span>, <span class="dt">icon =</span> <span class="kw">icon</span>(<span class="st">"check"</span>),
<span class="dt">choices =</span> <span class="kw">c</span>(<span class="st">"Click me !"</span>, <span class="st">"Me !"</span>, <span class="st">"Or me !"</span>),
<span class="dt">animation =</span> <span class="st">"rotate"</span>, <span class="dt">status =</span> <span class="st">"default"</span>),
<span class="kw">br</span>(),
<span class="kw">br</span>(),
<span class="kw">h4</span>(<span class="st">"pretty sweet checkbox"</span>),
<span class="kw">prettyToggle</span>(<span class="dt">inputId =</span> <span class="st">"toggle4"</span>,
<span class="dt">label_on =</span> <span class="st">"Yes!"</span>,
<span class="dt">label_off =</span> <span class="st">"No.."</span>, <span class="dt">outline =</span> <span class="ot">TRUE</span>,
<span class="dt">plain =</span> <span class="ot">TRUE</span>, <span class="dt">icon_on =</span> <span class="kw">icon</span>(<span class="st">"thumbs-up"</span>),
<span class="dt">icon_off =</span> <span class="kw">icon</span>(<span class="st">"thumbs-down"</span>))),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">h3</span>(<span class="st">"Date/Calendar Widgets"</span>),
<span class="kw">airDatepickerInput</span>(<span class="dt">inputId =</span> <span class="st">"default"</span>,
<span class="dt">label =</span> <span class="st">"Choose a date:"</span>),
<span class="kw">br</span>(),
<span class="kw">airDatepickerInput</span>(<span class="dt">inputId =</span> <span class="st">"range"</span>,
<span class="dt">label =</span> <span class="st">"Select range of dates:"</span>,
<span class="dt">range =</span> <span class="ot">TRUE</span>, <span class="dt">value =</span> <span class="kw">c</span>(<span class="kw">Sys.Date</span>()<span class="op">-</span><span class="dv">7</span>, <span class="kw">Sys.Date</span>())),
<span class="kw">br</span>(),
<span class="kw">airDatepickerInput</span>(<span class="dt">inputId =</span> <span class="st">"multiple times"</span>,
<span class="dt">label =</span> <span class="st">"Indicate 3 times you're free to meet:"</span>,
<span class="dt">timepicker =</span> T,
<span class="dt">placeholder =</span> <span class="st">"meeting times"</span>,
<span class="dt">multiple =</span> <span class="dv">3</span>, <span class="dt">separator =</span> <span class="st">" ; "</span>,
<span class="dt">clearButton =</span> <span class="ot">TRUE</span>),
<span class="kw">airDatepickerInput</span>(<span class="dt">inputId =</span> <span class="st">"inline"</span>,
<span class="dt">label =</span> <span class="st">"Inline:"</span>,
<span class="dt">inline =</span> <span class="ot">TRUE</span>))
),
<span class="kw">fluidRow</span>(
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">fileInput</span>(<span class="st">"file"</span>, <span class="kw">h3</span>(<span class="st">"File input"</span>))),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">h3</span>(<span class="st">"Help text"</span>),
<span class="kw">helpText</span>(<span class="st">"Note: help text isn't a true widget,"</span>,
<span class="st">"but it provides an easy way to add text to"</span>,
<span class="st">"accompany other widgets."</span>)),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">numericInput</span>(<span class="st">"num"</span>,
<span class="kw">h3</span>(<span class="st">"Numeric input"</span>),
<span class="dt">value =</span> <span class="dv">1</span>))
),
<span class="kw">fluidRow</span>(
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">radioButtons</span>(<span class="st">"radio"</span>, <span class="kw">h3</span>(<span class="st">"Radio buttons"</span>),
<span class="dt">choices =</span> <span class="kw">list</span>(<span class="st">"Choice 1"</span> =<span class="st"> </span><span class="dv">1</span>, <span class="st">"Choice 2"</span> =<span class="st"> </span><span class="dv">2</span>,
<span class="st">"Choice 3"</span> =<span class="st"> </span><span class="dv">3</span>),<span class="dt">selected =</span> <span class="dv">1</span>)),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">selectInput</span>(<span class="st">"select"</span>, <span class="kw">h3</span>(<span class="st">"Select box"</span>),
<span class="dt">choices =</span> <span class="kw">list</span>(<span class="st">"Choice 1"</span> =<span class="st"> </span><span class="dv">1</span>, <span class="st">"Choice 2"</span> =<span class="st"> </span><span class="dv">2</span>,
<span class="st">"Choice 3"</span> =<span class="st"> </span><span class="dv">3</span>), <span class="dt">selected =</span> <span class="dv">1</span>)),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">sliderInput</span>(<span class="st">"slider1"</span>, <span class="kw">h3</span>(<span class="st">"Sliders"</span>),
<span class="dt">min =</span> <span class="dv">0</span>, <span class="dt">max =</span> <span class="dv">100</span>, <span class="dt">value =</span> <span class="dv">50</span>),
<span class="kw">sliderInput</span>(<span class="st">"slider2"</span>, <span class="st">""</span>,
<span class="dt">min =</span> <span class="dv">0</span>, <span class="dt">max =</span> <span class="dv">100</span>, <span class="dt">value =</span> <span class="kw">c</span>(<span class="dv">25</span>, <span class="dv">75</span>))
),
<span class="kw">column</span>(<span class="dv">3</span>,
<span class="kw">textInput</span>(<span class="st">"text"</span>, <span class="kw">h3</span>(<span class="st">"Text input"</span>),
<span class="dt">value =</span> <span class="st">"Enter text..."</span>))
)
)
<span class="co"># Define server logic ----</span>
server <-<span class="st"> </span><span class="cf">function</span>(input, output) {
}
<span class="co"># Run the app ----</span>
<span class="kw">shinyApp</span>(<span class="dt">ui =</span> ui, <span class="dt">server =</span> server)</code></pre></div>
<iframe src="https://schavez.shinyapps.io/widgets/" style="border: none; width: 800px; height: 500px">
</iframe>
<p><br></p>
<p></br></p>
</div>
<div id="displaying-reactive-output" class="section level2">
<h2>Displaying Reactive Output</h2>
<p>Reactive output displays automatically when a user interacts with your shiny app using one (or more) of the control widgets we just made.</p>
<p>We can make reactive output in two steps:</p>
<ol style="list-style-type: decimal">
<li>Add an R object in your UI function. Shiny has built-in functions to turn your R objects into UI-compatible output. Here’s a table with those built-in output functions and what they do.</li>
</ol>
<table>
<thead>
<tr class="header">
<th>Output Function</th>
<th>Object It Makes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>dataTableOutput</td>
<td>DataTable</td>
</tr>
<tr class="even">
<td>htmlOutput</td>
<td>raw HTML</td>
</tr>
<tr class="odd">
<td>imageOutput</td>
<td>image</td>
</tr>
<tr class="even">
<td>plotOutput</td>
<td>plot</td>
</tr>
<tr class="odd">
<td>tableOutput</td>
<td>table</td>
</tr>
<tr class="even">
<td>textOutput</td>
<td>text</td>
</tr>
<tr class="odd">
<td>uiOutput</td>
<td>raw HTML</td>
</tr>
<tr class="even">
<td>verbatimTextOutput</td>
<td>text</td>
</tr>
</tbody>
</table>
<ul>
<li><p>Put your output function in the your UI, in either the sidebarPanel or mainPanel.</p></li>
<li><p>Your output function will take a character string, in quotes, as an argument. This character string corresponds to the name of a control widget in your UI.</p></li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Here's an example based on one of the widgets we made earlier.</span>
ui <-<span class="st"> </span><span class="kw">fluidPage</span>(
<span class="kw">titlePanel</span>(<span class="st">"The Presidential Launch Codes"</span>),
<span class="kw">sidebarLayout</span>(
<span class="kw">sidebarPanel</span>(
<span class="kw">helpText</span>(<span class="st">"Be careful. Interacting with this shiny app could cause the world to implode."</span>),
<span class="kw">actionBttn</span>(<span class="st">"fancy_action"</span>, <span class="st">"Danger! Don't Click Me"</span>,
<span class="dt">style =</span> <span class="st">"jelly"</span>, <span class="dt">color =</span> <span class="st">"danger"</span>, <span class="dt">size =</span> <span class="st">"lg"</span>)
),
<span class="kw">mainPanel</span>(
<span class="kw">imageOutput</span>(<span class="st">"res_fancy_action"</span>)
)
)
)</code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>Tell R how to build the object in your server function</li>
</ol>
<ul>
<li><p>The server function makes on object called ‘output’ that contains a list of all the things we need to make a shiny app.</p></li>
<li><p>Each output object that you make from step 1 needs to have it’s own entry/element in the server function.</p></li>
<li><p>You need to name this element the same thing as you named your control widget.</p></li>
<li><p>Finally, we need to call a widget value to make the output display reactively when a user manipulates the widget. (Hint: Sometimes (especially when you use button widgets), you need to add an observeEvent() command, like the one below.)</p></li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Here's an example server function to go with the ui function above.</span>
server <-<span class="st"> </span><span class="cf">function</span>(input, output) {
<span class="kw">observeEvent</span>(input<span class="op">$</span>fancy_action, {
output<span class="op">$</span>res_fancy_action <-<span class="st"> </span><span class="kw">renderImage</span>({
endworld <-<span class="st"> </span><span class="kw">normalizePath</span>(<span class="kw">file.path</span>(<span class="st">'./pics'</span>,
<span class="kw">paste</span>(<span class="st">'endworld'</span>, <span class="st">'.gif'</span>, <span class="dt">sep=</span><span class="st">''</span>)))
<span class="co"># Return a list containing the filename</span>
<span class="kw">list</span>(<span class="dt">src =</span> endworld)
}, <span class="dt">deleteFile =</span> F)
})
}</code></pre></div>
<ul>
<li><p>Each output element should contain a render function.</p></li>
<li><p>Each render function will only takes only one argument, which always take the form of an R expression in culy braces like these {}.</p></li>
<li><p>Here’s a table describing the render functions that are built into shiny.</p></li>
</ul>
<table>
<thead>
<tr class="header">
<th>Render Function</th>
<th>Object It Makes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>renderDataTable</td>
<td>DataTable</td>
</tr>
<tr class="even">
<td>renderImage</td>
<td>images (saved as a link to a source file)</td>
</tr>
<tr class="odd">
<td>renderPlot</td>
<td>plots</td>
</tr>
<tr class="even">
<td>renderPrint</td>
<td>any printed output</td>
</tr>
<tr class="odd">
<td>renderTable</td>
<td>data frame, matrix, other table like structures</td>
</tr>
<tr class="even">
<td>renderText</td>
<td>character strings</td>
</tr>
<tr class="odd">
<td>renderUI</td>
<td>a Shiny tag object or HTML</td>
</tr>
</tbody>
</table>
<p>We’ve finished building our shiny app. To launch it in Rstudio, just run the the UI and server code chunks above and then run:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">shinyApp</span>(<span class="dt">ui =</span> ui, <span class="dt">server =</span> server)</code></pre></div>
</div>
<div id="share-your-shiny-app" class="section level2">
<h2>Share Your Shiny App</h2>
<p>Step 1: Create an account on shinyapps.io (<a href="https://www.shinyapps.io/admin/#/signup" class="uri">https://www.shinyapps.io/admin/#/signup</a>)</p>
<ul>
<li>Create an account name and then hit enter/return.</li>
</ul>
<p>Step 2: Configure your account.</p>
<ul>
<li>Install rsconnect package in R</li>
<li>Click the “show secret” button in step 2 of shinyapps.io</li>
<li>Click the “copy to clipboard” button in step 2 of shinyapps.io</li>
<li>Copy the info in the popup and paste it into R & run</li>
<li>Load rsconnect library</li>
</ul>
<p>Step 3: After you’ve created an awesome, original shiny app IN ITS OWN FOLDER</p>
<ul>
<li>Set your working directory to your shiny app folder</li>
<li>Deploy your shiny app with rsconnect::deployApp()</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">install.packages</span>(<span class="st">'rsconnect'</span>)
##you should replace this with your own info from
rsconnect<span class="op">::</span><span class="kw">setAccountInfo</span>(<span class="dt">name=</span><span class="st">'schavez'</span>, <span class="dt">token=</span><span class="st">'a bunch of numbers and letters'</span>, <span class="dt">secret=</span><span class="st">'secret numbers and letters'</span>)
<span class="kw">library</span>(rsconnect)
<span class="co">#Last Step: This step is for after you've made a shiny app</span>
<span class="kw">setwd</span>(<span class="st">"~/UO/Spring 2018/Data Science/personal_site/shinythings/shinytest/"</span>)
rsconnect<span class="op">::</span><span class="kw">deployApp</span>()</code></pre></div>
<p>When deployApp() has finished running, a shinyapps.io website should pop up with your shiny app. Now, we can just use an inline frame to embed our shiny app in our new website, like this.</p>
<div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><iframe</span><span class="ot"> src=</span><span class="st">"https://schavez.shinyapps.io/world/"</span><span class="ot"> style=</span><span class="st">"border: none; width: 800px; height: 500px"</span><span class="kw">></iframe></span></code></pre></div>
<p>And here it is, in all it’s glory.</p>
<iframe src="https://schavez.shinyapps.io/world/" style="border: none; width: 800px; height: 500px">
</iframe>
<p><br></p>
<p></br></p>
</div>
<div id="more-resources" class="section level2">
<h2>More Resources</h2>
<p>Begin with Lesson 5 to pick up where we left off. This site will teach you how to load data into shiny and use r scripts and packages within shiny. <br></br> <a href="https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/" class="uri">https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/</a> </br> <a href="https://github.com/grabear/awesome-rshiny" class="uri">https://github.com/grabear/awesome-rshiny</a> </br> <a href="https://blog.rstudio.com/categories/shiny/" class="uri">https://blog.rstudio.com/categories/shiny/</a></p>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>