generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsection-structure.html
495 lines (458 loc) · 36.8 KB
/
section-structure.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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 1 Structure of a Shiny App | 03-structure.knit</title>
<meta name="description" content="" />
<meta name="generator" content="bookdown 0.25 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 1 Structure of a Shiny App | 03-structure.knit" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 1 Structure of a Shiny App | 03-structure.knit" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="/Users/nkan0009/BioinformaticsPlatform-MBP/R-ShinyIntro-MBP/libs/style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">Intro to R Shiny</a></li>
<li class="divider"></li>
<li class="chapter" data-level="1" data-path=""><a href="#section-structure"><i class="fa fa-check"></i><b>1</b> Structure of a Shiny App<span></span></a>
<ul>
<li class="chapter" data-level="1.1" data-path=""><a href="#section-ui"><i class="fa fa-check"></i><b>1.1</b> UI<span></span></a></li>
<li class="chapter" data-level="1.2" data-path=""><a href="#section-layout"><i class="fa fa-check"></i><b>1.2</b> Layout<span></span></a>
<ul>
<li class="chapter" data-level="1.2.1" data-path=""><a href="#section-challenge-0---.activity."><i class="fa fa-check"></i><b>1.2.1</b> Challenge 0 {- .activity}.<span></span></a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path=""><a href="#section-widgets"><i class="fa fa-check"></i><b>1.3</b> Widgets<span></span></a>
<ul>
<li><a href="#section-activity-3-now-open-the-widgets.r-inside-shinyapps-folder-and-try-running-it.">Activity 3: Now, open the <code>widgets.R</code> inside <code>ShinyApps</code> folder, and try running it.<span></span></a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path=""><a href="#section-server"><i class="fa fa-check"></i><b>1.4</b> Server<span></span></a>
<ul>
<li class="chapter" data-level="1.4.1" data-path=""><a href="#section-input"><i class="fa fa-check"></i><b>1.4.1</b> Input<span></span></a></li>
<li class="chapter" data-level="1.4.2" data-path=""><a href="#section-output"><i class="fa fa-check"></i><b>1.4.2</b> Output<span></span></a></li>
<li><a href="#section-challenge-1-rewrite-your-ui-to-create-a-shiny-app-that-contains-following">Challenge 1: Rewrite your <code>ui</code> to create a Shiny app that contains following:<span></span></a></li>
</ul></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./"></a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<!--bookdown:title:end-->
<!--bookdown:title:start-->
<div id="section-structure" class="section level1 hasAnchor" number="1">
<h1><span class="header-section-number">Chapter 1</span> Structure of a Shiny App<a href="#section-structure" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>A basic Shiny app consists of two objects:</p>
<ul>
<li><p>UI object - contains the code for the UI (user interface) elements i.e the visible part of the app; takes input and displays output.</p></li>
<li><p>server object - contains the code for all the calculations the app will need. It contains the logical part of the code and access input from UI, performs the computation and generates an output which is returned to the UI.</p></li>
</ul>
<center>
<img src="https://github.com/MonashBioinformaticsPlatform/r-more/blob/gh-pages/figures/shiny_explanation.png?raw=true" width="409" height="240" />
</center>
<p>
</p>
<p>The skeleton of every Shiny app looks like:</p>
<div class="sourceCode" id="section-cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb1-1"><a href="#section-cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="section-cb1-2"><a href="#section-cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb1-3"><a href="#section-cb1-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="section-cb1-4"><a href="#section-cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># front end interface</span></span>
<span id="section-cb1-5"><a href="#section-cb1-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="section-cb1-6"><a href="#section-cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb1-7"><a href="#section-cb1-7" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="section-cb1-8"><a href="#section-cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># back end logic</span></span>
<span id="section-cb1-9"><a href="#section-cb1-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="section-cb1-10"><a href="#section-cb1-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb1-11"><a href="#section-cb1-11" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server) <span class="co"># runs the app</span></span></code></pre></div>
<div id="section-ui" class="section level2 hasAnchor" number="1.1">
<h2><span class="header-section-number">1.1</span> UI<a href="#section-ui" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>This is the front-end of the shiny app that allows you to capture (input) and display (output) a variety of data types. Fundamentally the UI is an HTML document with shiny functions returning chunks of HTML code. Fortunately, we don’t have to write the HTML code directly ourselves, Shiny has R functions that do that instead.</p>
<p>The UI object controls the layout and appearance of the app. The UI makes use of widgets to interact with data. Here, we’ll look at examples of shinyWidgets that insert input controls.</p>
<div class="sourceCode" id="section-cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb2-1"><a href="#section-cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="section-cb2-2"><a href="#section-cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sliderInput</span>(<span class="st">"slider1"</span>,</span>
<span id="section-cb2-3"><a href="#section-cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="at">label =</span> <span class="fu">h4</span>(<span class="st">"This code will make a slider </span><span class="sc">\n</span><span class="st">"</span>),</span>
<span id="section-cb2-4"><a href="#section-cb2-4" 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 class="at">value =</span> <span class="dv">50</span>)</span></code></pre></div>
<div class="form-group shiny-input-container">
<label class="control-label" id="slider1-label" for="slider1">
<h4>This code will make a slider
</h4>
</label>
<input class="js-range-slider" id="slider1" data-skin="shiny" data-min="0" data-max="100" data-from="50" data-step="1" data-grid="true" data-grid-num="10" data-grid-snap="false" data-prettify-separator="," data-prettify-enabled="true" data-keyboard="true" data-data-type="number"/>
</div>
<div class="sourceCode" id="section-cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb3-1"><a href="#section-cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">renderPrint</span>({<span class="fu">paste</span>(<span class="st">"The number selected by the slider is:"</span>, input<span class="sc">$</span>slider1)})</span></code></pre></div>
<p><pre class="shiny-text-output noplaceholder" id="outcf2cd5352460e64a"></pre></p>
<div class="sourceCode" id="section-cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb4-1"><a href="#section-cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">fluidPage</span>(</span>
<span id="section-cb4-2"><a href="#section-cb4-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb4-3"><a href="#section-cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># Copy the chunk below to make a group of checkboxes</span></span>
<span id="section-cb4-4"><a href="#section-cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxGroupInput</span>(<span class="st">"checkGroup"</span>, <span class="at">label =</span> <span class="fu">h3</span>(<span class="st">"Checkbox group"</span>), </span>
<span id="section-cb4-5"><a href="#section-cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="at">choices =</span> <span class="fu">list</span>(<span class="st">"Choice 1"</span> <span class="ot">=</span> <span class="dv">1</span>, <span class="st">"Choice 2"</span> <span class="ot">=</span> <span class="dv">2</span>, <span class="st">"Choice 3"</span> <span class="ot">=</span> <span class="dv">3</span>),</span>
<span id="section-cb4-6"><a href="#section-cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="at">selected =</span> <span class="dv">1</span>),</span>
<span id="section-cb4-7"><a href="#section-cb4-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb4-8"><a href="#section-cb4-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb4-9"><a href="#section-cb4-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">hr</span>(),</span>
<span id="section-cb4-10"><a href="#section-cb4-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">fluidRow</span>(<span class="fu">column</span>(<span class="dv">3</span>, <span class="fu">verbatimTextOutput</span>(<span class="st">"value"</span>)))</span>
<span id="section-cb4-11"><a href="#section-cb4-11" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb4-12"><a href="#section-cb4-12" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<div class="container-fluid">
<div id="checkGroup" class="form-group shiny-input-checkboxgroup shiny-input-container" role="group" aria-labelledby="checkGroup-label">
<label class="control-label" id="checkGroup-label" for="checkGroup">
<h3>Checkbox group</h3>
</label>
<div class="shiny-options-group">
<div class="checkbox">
<label>
<input type="checkbox" name="checkGroup" value="1" checked="checked"/>
<span>Choice 1</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="checkGroup" value="2"/>
<span>Choice 2</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="checkGroup" value="3"/>
<span>Choice 3</span>
</label>
</div>
</div>
</div>
<hr/>
<div class="row">
<div class="col-sm-3">
<pre class="shiny-text-output noplaceholder" id="value"></pre>
</div>
</div>
</div>
<p>The <a href="https://shiny.rstudio.com/gallery/#demos">shiny gallery</a> has a section for application layouts. This is a useful of resource for ideas on how to structure the appearance of your app.</p>
<p>To see what widgets are available, look at the <a href="http://shiny.rstudio.com/gallery/widget-gallery.html">Shiny widget gallery.</a> or consult the <a href="http://shiny.rstudio.com/images/shiny-cheatsheet.pdf">Shiny cheatsheet</a>.</p>
</div>
<div id="section-layout" class="section level2 hasAnchor" number="1.2">
<h2><span class="header-section-number">1.2</span> Layout<a href="#section-layout" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The complexity of your application may have an impact on the complexity of your layout. For instance, an application that contains only one or two widgets and one or two outputs may only need one page to fit all these elements. If however there are lot of controls and results to display it might be easier to organize these under tabs or a navbar.</p>
<p>A layout with three panels: a title panel, a sidebar panel and a main panel.</p>
<div class="sourceCode" id="section-cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb5-1"><a href="#section-cb5-1" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>( <span class="co"># fluidPage - this function creates a display that auto adjusts to the browser window</span></span>
<span id="section-cb5-2"><a href="#section-cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># further elements set to define the appearance will be placed within fluidPage</span></span>
<span id="section-cb5-3"><a href="#section-cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">titlePanel</span>(<span class="st">"this is a title panel"</span>),</span>
<span id="section-cb5-4"><a href="#section-cb5-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb5-5"><a href="#section-cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarLayout</span>( <span class="co"># this function creates a layout with two pane21ls, one panel takes about a third of the screen width, the other panel takes about two thirds of the screen width.</span></span>
<span id="section-cb5-6"><a href="#section-cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># the position of the sidebar can be moved by providing the `position = "right" argument to the sidebarLayout function</span></span>
<span id="section-cb5-7"><a href="#section-cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarPanel</span>(<span class="st">"this is the sidebar panel"</span>),</span>
<span id="section-cb5-8"><a href="#section-cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mainPanel</span>(<span class="st">"this is the main panel"</span>)</span>
<span id="section-cb5-9"><a href="#section-cb5-9" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="section-cb5-10"><a href="#section-cb5-10" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>Building a UI involves nesting a lot of components inside each other. From this example here we can see that <code>sidebarPanel</code> and <code>mainPanel</code> are nested within <code>sidebarLayout</code>. <code>sidebarLayout</code> itself is also nested within <code>fluidPage</code>. As more components are added to the app, e.g widgets and outputs, they too will be nested within the panel functions to control their location on the layout.</p>
<p>The above layout is probably the most common layout used with shiny apps. It’s good for an app with only a few number of components. You may want to add navbar and tab sets if you have a large number of components.</p>
<div class="sourceCode" id="section-cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb6-1"><a href="#section-cb6-1" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>( </span>
<span id="section-cb6-2"><a href="#section-cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">titlePanel</span>(<span class="st">"this is a title panel"</span>),</span>
<span id="section-cb6-3"><a href="#section-cb6-3" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb6-4"><a href="#section-cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarLayout</span>( </span>
<span id="section-cb6-5"><a href="#section-cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sidebarPanel</span>(<span class="st">"this is the sidebar panel"</span>),</span>
<span id="section-cb6-6"><a href="#section-cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mainPanel</span>( </span>
<span id="section-cb6-7"><a href="#section-cb6-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">tabsetPanel</span>( <span class="co"># this function takes an argument of `position = "left"` (arguments can be "left", "right", "below". default is above)</span></span>
<span id="section-cb6-8"><a href="#section-cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># this argument directs where the tabs are positioned</span></span>
<span id="section-cb6-9"><a href="#section-cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">tabPanel</span>(<span class="st">"plot results"</span>),</span>
<span id="section-cb6-10"><a href="#section-cb6-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">tabPanel</span>(<span class="st">"table results"</span>),</span>
<span id="section-cb6-11"><a href="#section-cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">tabPanel</span>(<span class="st">"fun gifs"</span>)</span>
<span id="section-cb6-12"><a href="#section-cb6-12" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="section-cb6-13"><a href="#section-cb6-13" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="section-cb6-14"><a href="#section-cb6-14" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="section-cb6-15"><a href="#section-cb6-15" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>For more information about the application layout (including nav bars and lists), we recommend this <a href="https://shiny.rstudio.com/articles/layout-guide.html">resource</a>. For more information about tab sets specifically, we recommend this <a href="https://shiny.rstudio.com/articles/tabsets.html">article</a>.</p>
<p>Another thing to note that a lot of the functions that build the UI are essentially wrapping HTML code within R functions. This means that the UI can also be customized by directly writing HTML code or by using <a href="https://shiny.rstudio.com/articles/tag-glossary.html">wrapper tag functions</a>. This is beyond the scope of this course but we thought it was worth mentioning.</p>
<div id="section-challenge-0---.activity." class="section level3 hasAnchor" number="1.2.1">
<h3><span class="header-section-number">1.2.1</span> Challenge 0 {- .activity}.<a href="#section-challenge-0---.activity." class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Does the <code>tabsetPanel</code> need to be nested in the <code>sidebarLayout</code>?</p>
</div>
</div>
<div id="section-widgets" class="section level2 hasAnchor" number="1.3">
<h2><span class="header-section-number">1.3</span> Widgets<a href="#section-widgets" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>There is a wide variety of available pre-built widgets but all of them share a common basic framework. All widget input functions have the same first argument i.e. <code>inputID</code>. This is an identifier that connects the UI with the server. If for example <code>inputID = select</code>, you can access the input in the server as <code>input$select</code>. We’ll discuss more about the server in the next section.</p>
<ul>
<li><p>The <code>inputID</code> is like a variable in R and must follow the naming convention. It must be a string that may contain only letters, numbers, and underscores.</p></li>
<li><p>Like a variable, the <code>inputID</code> has to be unique so it can be accessed inside the server.</p></li>
</ul>
<p>Here are some more examples of Shiny widgets:</p>
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<span class="navbar-brand">Navbar</span>
</div>
<ul class="nav navbar-nav" data-tabsetid="4214">
<li class="active">
<a href="#tab-4214-1" data-toggle="tab" data-bs-toggle="tab" data-value="Widgets!">Widgets!</a>
</li>
<li>
<a href="#tab-4214-2" data-toggle="tab" data-bs-toggle="tab" data-value="More widgets!">More widgets!</a>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
<div class="tab-content" data-tabsetid="4214">
<div class="tab-pane active" data-value="Widgets!" id="tab-4214-1">
<div class="form-group shiny-input-container">
<label class="control-label" id="select-label" for="select">
<h3>Select box</h3>
</label>
<div>
<select id="select"><option value="1" selected>Choice 1</option>
<option value="2">Choice 2</option>
<option value="3">Choice 3</option></select>
<script type="application/json" data-for="select" data-nonempty="">{"plugins":["selectize-plugin-a11y"]}</script>
</div>
</div>
<pre class="shiny-text-output noplaceholder" id="out9a21e0aadd42d52a"></pre>
<div class="form-group shiny-input-container">
<label class="control-label" id="text-label" for="text">
<h3>Text input</h3>
</label>
<input id="text" type="text" class="form-control" value="Enter text..."/>
</div>
<pre class="shiny-text-output noplaceholder" id="out06053fc7ab0a0169"></pre>
</div>
<div class="tab-pane" data-value="More widgets!" id="tab-4214-2">
<div id="checkGroup" class="form-group shiny-input-checkboxgroup shiny-input-container" role="group" aria-labelledby="checkGroup-label">
<label class="control-label" id="checkGroup-label" for="checkGroup">
<h3>Checkbox group</h3>
</label>
<div class="shiny-options-group">
<div class="checkbox">
<label>
<input type="checkbox" name="checkGroup" value="circle" checked="checked"/>
<span>Circle</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="checkGroup" value="square"/>
<span>Square</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="checkGroup" value="triangle"/>
<span>Triangle</span>
</label>
</div>
</div>
</div>
<pre class="shiny-text-output noplaceholder" id="out0142731d5753eabb"></pre>
</div>
</div>
</div>
<p>The <code>choices</code> augment takes a list. This list can either be unnamed or named. In the case of a named list, such as the one above for the <code>checkboxGroupInput</code> widget, the names will be displayed on the UI (e.g “Choice 1”) while the value (e.g 1) will be passed to the server side to be use1d for further computations. For an unnamed list, the value will both be displayed on the UI and passed to the server side.</p>
<div id="section-activity-3-now-open-the-widgets.r-inside-shinyapps-folder-and-try-running-it." class="section level3 unnumbered activity hasAnchor">
<h3>Activity 3: Now, open the <code>widgets.R</code> inside <code>ShinyApps</code> folder, and try running it.<a href="#section-activity-3-now-open-the-widgets.r-inside-shinyapps-folder-and-try-running-it." class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>There are a variety of widgets available in Shiny Apps. Have a go at them!!</p>
<p>Please Note: For now, keep the <code>Server Logic</code> and <code>Run the App</code> as it is. We’ll explain these in next sections.</p>
</div>
</div>
<div id="section-server" class="section level2 hasAnchor" number="1.4">
<h2><span class="header-section-number">1.4</span> Server<a href="#section-server" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The server object contains the code for building the app. It contains the code for data processing, analysis and plotting. It takes input from the UI, runs (and re-runs) code chunks defined within, generates output and sends it to the UI.</p>
<p>The server logic follows reactive programming, which defines a graph of dependencies. The idea is that when the input changes, all related outputs are updated. This makes the Shiny App interactive.</p>
<p>In the Shiny skeleton code, server is defined as a function with three arguments- input, output and session. These arguments are created by Shiny when the session starts and cannot be changed.</p>
<div class="sourceCode" id="section-cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb7-1"><a href="#section-cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="section-cb7-2"><a href="#section-cb7-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb7-3"><a href="#section-cb7-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="section-cb7-4"><a href="#section-cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># front end interface</span></span>
<span id="section-cb7-5"><a href="#section-cb7-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="section-cb7-6"><a href="#section-cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb7-7"><a href="#section-cb7-7" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="section-cb7-8"><a href="#section-cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># back end logic</span></span>
<span id="section-cb7-9"><a href="#section-cb7-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="section-cb7-10"><a href="#section-cb7-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb7-11"><a href="#section-cb7-11" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
<div id="section-input" class="section level3 hasAnchor" number="1.4.1">
<h3><span class="header-section-number">1.4.1</span> Input<a href="#section-input" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>This is a list-like object that contains all input data from the UI. The input data can be accessed as <code>input$inputId</code>, where <code>inputId</code> is defined in the UI for any given widget. For example, if we refer back to the check-box example in shiny widgets, we specified the <code>inputId</code> as <code>checkGroup</code>. We can access the value of that specific input using <code>input$checkGroup</code>. It will contain the value <code>1</code> as we specified this in the <code>selected</code> argument. This value will be updated everytime a user selects a different check-box.</p>
<div class="sourceCode" id="section-cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb8-1"><a href="#section-cb8-1" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="section-cb8-2"><a href="#section-cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Copy the chunk below to make a group of checkboxes</span></span>
<span id="section-cb8-3"><a href="#section-cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">checkboxGroupInput</span>(<span class="at">inputId =</span> <span class="st">"checkGroup"</span>,</span>
<span id="section-cb8-4"><a href="#section-cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="at">label =</span> <span class="fu">h3</span>(<span class="st">"Checkbox group"</span>),</span>
<span id="section-cb8-5"><a href="#section-cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="at">choices =</span> <span class="fu">list</span>(<span class="st">"Choice 1"</span> <span class="ot">=</span> <span class="dv">1</span>,</span>
<span id="section-cb8-6"><a href="#section-cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Choice 2"</span> <span class="ot">=</span> <span class="dv">2</span>,</span>
<span id="section-cb8-7"><a href="#section-cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"Choice 3"</span> <span class="ot">=</span> <span class="dv">3</span>),</span>
<span id="section-cb8-8"><a href="#section-cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="at">selected =</span> <span class="dv">1</span>)</span>
<span id="section-cb8-9"><a href="#section-cb8-9" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
</div>
<div id="section-output" class="section level3 hasAnchor" number="1.4.2">
<h3><span class="header-section-number">1.4.2</span> Output<a href="#section-output" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>This is also a list-like object named according to the <code>outputId</code> arguments created in the UI. In the following example, the <code>outputId</code> for the <code>textOutput</code> is defined as <code>greeting</code> inside the UI. Inside the server function, the output is stored in the <code>outputId</code> using <code>output$greeting</code> (think of it as a storing a value inside a variable). Alternatively, think of it as a way to direct the results of a computation on the server side to a specific <code>outputID</code>.</p>
<p>The value of the <code>greeting</code> is <code>Hello, World!</code></p>
<div class="sourceCode" id="section-cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb9-1"><a href="#section-cb9-1" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="section-cb9-2"><a href="#section-cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">textOutput</span>(<span class="at">outputId =</span> <span class="st">"greeting"</span>) <span class="co"># defines outputID</span></span>
<span id="section-cb9-3"><a href="#section-cb9-3" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="section-cb9-4"><a href="#section-cb9-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb9-5"><a href="#section-cb9-5" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="section-cb9-6"><a href="#section-cb9-6" aria-hidden="true" tabindex="-1"></a> output<span class="sc">$</span>greeting <span class="ot"><-</span> <span class="fu">renderText</span>(<span class="st">"Hello, World!"</span>) <span class="co"># stores output in the outputID</span></span>
<span id="section-cb9-7"><a href="#section-cb9-7" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>The <code>render</code> function sets up a special “reactive context” that automatically tracks the input values the output has used. It also converts the output of your R code into HTML to be display on a web page.</p>
</div>
<div id="section-challenge-1-rewrite-your-ui-to-create-a-shiny-app-that-contains-following" class="section level3 unnumbered challenge hasAnchor">
<h3>Challenge 1: Rewrite your <code>ui</code> to create a Shiny app that contains following:<a href="#section-challenge-1-rewrite-your-ui-to-create-a-shiny-app-that-contains-following" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<ol style="list-style-type: decimal">
<li><p>Title that says “Challenge 1”</p></li>
<li><p>Select <code>airquality</code> from the default datasets in R (Hint: Refer to <code>app_ui.R</code>).</p></li>
<li><p>Create three widgets:</p>
<ul>
<li><p><code>helpText()</code> to write “List of variables to choose from”.</p></li>
<li><p><code>selectInput()</code> to list variables in the <code>airquality</code> dataset.</p></li>
<li><p><code>sliderInput()</code> to add a slider where, <code>min = 0</code>; <code>max = 30</code> and selected values are: <code>(5,21)</code></p></li>
</ul></li>
<li><p>Try replacing the list options in selectInput by <code>colnames(airquality)</code>.</p></li>
</ol>
<p>Your Shiny app should look like this:</p>
<img src="/Users/nkan0009/BioinformaticsPlatform-MBP/R-ShinyIntro-MBP/libs/figs/challenge1.png" />
<p>
</p>
<blockquote>
<p>Hint: Use the Shiny skeleton code to create a new R-script and edit the UI part:</p>
</blockquote>
<div class="sourceCode" id="section-cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="section-cb10-1"><a href="#section-cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(shiny)</span>
<span id="section-cb10-2"><a href="#section-cb10-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb10-3"><a href="#section-cb10-3" aria-hidden="true" tabindex="-1"></a>ui <span class="ot"><-</span> <span class="fu">fluidPage</span>(</span>
<span id="section-cb10-4"><a href="#section-cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># edit this part</span></span>
<span id="section-cb10-5"><a href="#section-cb10-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="section-cb10-6"><a href="#section-cb10-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb10-7"><a href="#section-cb10-7" aria-hidden="true" tabindex="-1"></a>server <span class="ot"><-</span> <span class="cf">function</span>(input, output, session) {</span>
<span id="section-cb10-8"><a href="#section-cb10-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="section-cb10-9"><a href="#section-cb10-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="section-cb10-10"><a href="#section-cb10-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="section-cb10-11"><a href="#section-cb10-11" aria-hidden="true" tabindex="-1"></a><span class="fu">shinyApp</span>(ui, server)</span></code></pre></div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</div>
</div>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/nkandhari/R-ShinyIntro-MBP/edit/master/%s",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": "https://github.com/nkandhari/R-ShinyIntro-MBP/blob/master/%s",
"text": null
},
"download": null,
"search": false,
"toc": {
"collapse": "subsection"
}
});
});
</script>
</body>
</html>