forked from batpigandme/tidynomicon
/
shiny.Rmd
752 lines (628 loc) · 21.5 KB
/
shiny.Rmd
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
# Web Applications {#shiny}
```{r setup, include=FALSE}
source("etc/common.R")
```
## Questions
```{r, child="questions/shiny.md"}
```
## Learning Objectives
```{r, child="objectives/shiny.md"}
```
Sooner or later,
almost every application needs a graphical interface
so that users can load data files, control parameters, and view results.
While the desktop still has a role to play,
the best place to build an interface these days is on the web,
and the best toolkit for doing that in R is [Shiny][shiny].
This lesson will walk through the construction of a simple application
and along the way introduce you to [reactive programming](glossary.html#reactive-programming).
To follow along,
please install Shiny using:
```{r eval=FALSE}
install.packages("shiny")
```
```{r eval=FALSE}
library(shiny)
```
## How do I set up a simple application?
Every Shiny app has three things:
1. A user interface object that shows things to user.
2. A server function, which is the back end that provides data.
3. A call to `shinyApp` that binds the two together.
These can all live in the same file,
but most developers prefer to put the UI and server code in separate files.
To start,
we will reproduce the first example from the Shiny tutorials.
The first step is to create a directory called `faithful_app`:
we *must* do this because every Shiny application needs to be in its own directory.
Inside that directory,
create a file called `app.R`.
The function call `runApp(directory_name)` automatically looks in the named directory for a file with that name.
Inside `app.R`, we can create the skeleton of the application:
```{r eval=FALSE}
library(shiny)
ui <- # ...user interface...
server <- # ...server...
shinyApp(ui = ui, server = server)
```
Our next tasks are to fill in the user interface and server.
## How do I create a user interface?
Our interface is a [fluid page](glossary.html#fluid-page),
i.e., it resizes as needed based on the size of the browser.
It uses a single sidebar layout with two elements, `sidebarPanel` and `mainPanel`.
The sidebar contains a `sliderInput` object that (as you'd expect from the name) creates a slider;
we must specify `label`, `min`, `max`, and `value` to set it up.
We must also specify a value called `inputId`;
this gives it a name that we can use to refer to it in the server.
Our interface also contains a `mainPanel` object that in turn contains a single `plotOutput`
whose `outputId` attribute is used to refer to it.
The whole thing looks like this:
```{r eval=FALSE}
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(plotOutput(outputId = "distPlot"))
)
)
```
## How do I create a server?
In any interactive application,
something has to react to changes in controls and update displays.
Shiny watches for the former and takes care of the latter automatically,
but we have to tell it what to watch, what to update, and how to make those updates.
We do this by creating a function called `server` that Shiny calls when it needs to:
```{r eval=FALSE}
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
```
When there is a change in one of the input elements,
Shiny notices and calls our function,
giving it inputs (i.e., our controls) and outputs (our displays).
For example,
`input$bins` matches the `bins` ID for the slider,
so the value of `input$bins` will be the value of the slider.
Similarly,
`output$distPlot` matches the `distPlot` ID of the plot,
so we can use Shiny's `renderPlot` function to tell it what to plot.
(We can't use ggplot2 calls directly, but the terminology is very similar.)
In this case:
the `x` axis is waiting times from the `faithful` data,
`bins` is the bin labels (we use `input$bins` to get the value),
and `hist` is the histogram we want plotted.
## How do I run my application?
We can now run `app.R` from the command line or use:
```{r eval=FALSE}
runApp("faithful_app")
```
from inside RStudio.
Once the application is running,
we can narrow the window to see things automatically resize
(the "fluid" part of the interface).
## How can I improve my user interface?
Let's trying building a tool for exploring the UNICEF data we tidied up in earlier lessons.
To start,
we `mkdir unicef/skeleton` and create `app.R`:
<!-- unicef/skeleton/app.R -->
```{r eval=FALSE}
library(shiny)
ui <- fluidPage(
titlePanel("UNICEF Data"),
sidebarLayout(
position = "right",
sidebarPanel(
img(src = "logo.png", width = 200),
h2("Controls")
),
mainPanel(h1("Display"))
)
)
server <- function(input, output){
# Empty for now.
}
shinyApp(ui = ui, server = server)
```
```{r}
knitr::include_graphics("figures/shiny/unicef-skeleton.png")
```
As the screenshot shows,
this positions the controls on the right.
We use `h1`, `h2`, and similarly-named functions to create HTML elements
and `img` to display a logo.
The server is empty for now;
when we run it,
everything looks good except the image,
because it turns out that static images must be in the application's `www` folder,
i.e.,
in `unicef/skeleton/www`.
It's now time to add interactive control elements, better known as [widgets](glossary.html#widget).
Shiny provides:
- buttons
- checkboxes
- radio buttons
- pulldown selectors (for cases when checkboxes or radio buttons would take up too much space)
- date inputs and date ranges
- filenames
- sliders (like the one seen before)
- free-form text input
That's a lot of options;
since we don't have a user to consult,
we need to decide what we're going to visualize.
One obvious choice is to allow people to choose a data file,
and then select a data range based on the years in that file
and see a line plot of the average estimate by year.
This makes our interface:
<!-- unicef/prototype/app.R -->
```{r eval=FALSE}
ui <- fluidPage(
titlePanel("UNICEF Data"),
sidebarLayout(
position = "right",
sidebarPanel(
img(src = "logo.png", width = 200),
fileInput("datafile", p("data file")),
dateRangeInput("years", p("years"), format = "yyyy")
),
mainPanel(h1("Display"))
)
)
```
```{r}
knitr::include_graphics("figures/shiny/unicef-prototype.png")
```
Let's show the chosen filename in the output display:
<!-- unicef/filename_wrong/app.R -->
```{r eval=FALSE}
ui <- fluidPage(
titlePanel("UNICEF Data"),
sidebarLayout(
# ...as before...
mainPanel(
textOutput("filename")
)
)
)
server <- function(input, output){
output$filename <- renderText({
paste("input file:", input$datafile)
})
}
```
The initial display looks good:
```{r}
knitr::include_graphics("figures/shiny/unicef-filename-wrong-before.png")
```
but when we fill in the filename, something is clearly wrong:
```{r}
knitr::include_graphics("figures/shiny/unicef-filename-wrong-after.png")
```
A quick browse of the documentation reveals that
`input` is a named list-like object of everything set up in the interface.
`input$datafile` picks out one element,
but it turns out that's a data frame:
what we actually want is `input$datafile$datapath`:
<!-- unicef/filename_right/app.R -->
```{r eval=FALSE}
server <- function(input, output){
output$filename <- renderText({
paste("input file:", input$datafile$datapath)
})
}
```
Some more experimentation reveals that we should display `name`,
but use `datapath` when reading data.
Let's fill in the server a bit:
<!-- unicef/server/app.R -->
```{r eval=FALSE}
server <- function(input, output){
currentData <- NULL
output$filename <- renderText({
currentName <- input$datafile$name
currentPath <- input$datafile$datapath
if (is.null(currentName)) {
currentData <- NULL
text <- "no filename set"
} else {
currentData <- read_csv(currentPath)
text <- paste("showing", currentName)
}
text
})
}
```
This short server shows that there are three places we can put variables:
1. At the top of the script outside functions, they are run once when the app launches.
2. Inside `server`, they are run once for each user.
3. Inside a handler like `renderText`, they are run once on each change.
```{r}
knitr::include_graphics("figures/shiny/unicef-filename-right.png")
```
## How can I display the data in a file?
Now comes the hard part: updating the chart when the file changes.
The trick is to use a [reactive variable](glossary.html#reactive-variable),
which is actually a function that changes value whenever something it depends on changes.
That "something" is usually another reactive,
like the ones provided by Shiny.
In the code below,
`currentData` is created by calling `reactive` with a block of code that produces the variable's value.
It uses `input$datafile`,
so it will automatically be triggered whenever `input$datafile` changes.
Other things can depend on it in the same way,
which allows us to get rid of `currentData`:
`output$filename` uses `currentData()`,
so it is automatically called when the reactive variable's value changes.
<!-- unicef/reactive_update/app.R -->
```{r eval=FALSE}
server <- function(input, output){
currentData <- reactive({
currentPath <- input$datafile$datapath
if (is.null(currentPath)) {
result <- NULL
} else {
result <- read_csv(currentPath)
}
result
})
output$filename <- renderText({
currentName <- input$datafile$name
if (is.null(currentName)) {
text <- "no filename set"
} else {
text <- paste("showing", currentName)
}
text
})
output$chart <- renderPlot({
data <- currentData()
if (is.null(data)) {
message("no data")
chart <- NULL
} else {
message("we have data, creating chart")
chart <- data %>%
group_by(year) %>%
summarize(average = mean(estimate, na.rm = TRUE)) %>%
ggplot() +
geom_line(mapping = aes(x = year, y = average))
}
chart
})
}
```
```{r echo=FALSE}
if (knitr::is_latex_output()){
knitr::include_graphics("figures/shiny/unicef-reactive-update.png")
} else {
knitr::include_graphics("figures/shiny/unicef-reactive-update.gif")
}
```
## How can I break circular dependencies?
Now comes the *other* hard part: handling changes to the date range.
We want the chart to display data for the selected range of years
and have the minimum and maximum possible year set by the data.
That means we have to change something in the user interface from the server;
to do that,
we add a third parameter `session` to the `server` function.
This variable holds the backward connection from the server to the UI.
Inside our server,
we get the current years from `input$years`
and use `updateDateRangeInput` to push a change from the output function to the input controls.
(This is the part that needs `session`.)
<!-- unicef/infinite_update/app.R -->
```{r eval=FALSE}
server <- function(input, output, session){
# ...other code as before...
output$chart <- renderPlot({
years <- input$years
message('years', years)
data <- currentData()
if (is.null(data)) {
chart <- NULL
} else {
minYear <- as.character(min(data$year))
maxYear <- as.character(max(data$year))
updateDateRangeInput(session, "years", min = minYear, max = maxYear,
start = minYear, end = maxYear)
chart <- data %>%
group_by(year) %>%
summarize(average = mean(estimate, na.rm = TRUE)) %>%
ggplot() +
geom_line(mapping = aes(x = year, y = average)) +
ggtitle(paste("Years", minYear, "-", maxYear))
}
chart
})
}
```
When we run this,
it displays the current date twice on startup before a file is selected because that's the default for the date input.
Once dates are entered,
though,
it goes into an infinite loop because
ghe chart depends on the dates,
but we're changing the dates inside the plot update.
Let's try again.
We will just read `years` inside the chart update and display it:
<!-- unicef/every_character/app.R -->
```{r eval=FALSE}
output$chart <- renderPlot({
years <- input$years
message('years', years)
data <- currentData()
if (is.null(data)) {
chart <- NULL
} else {
minYear <- as.character(min(data$year))
maxYear <- as.character(max(data$year))
chart <- data %>%
group_by(year) %>%
summarize(average = mean(estimate, na.rm = TRUE)) %>%
ggplot() +
geom_line(mapping = aes(x = year, y = average)) +
ggtitle(paste("Years", minYear, "-", maxYear))
}
chart
})
```
Whoops:
the message appears every time a character is typed in one of the date controls,
i.e.,
deleting the start year and typing `2`, `0`, `1`, `8`
produces `0002`, `0020`, and `0201` before producing a usable year.
That's clearly not what we want,
so we'll try a third approach
and only show the year selector when there's data.
While we're doing this,
we'll change the year selector to a double-ended slider, because seeing the day and month is misleading.
The revised UI code looks like this:
<!-- unicef/slider/app.R -->
```{r eval=FALSE}
ui <- fluidPage(
titlePanel("UNICEF Data"),
sidebarLayout(
position = "right",
sidebarPanel(
img(src = "logo.png", width = 200),
div(
id = "datafileInput",
fileInput("datafile", p("data file"))
)
),
mainPanel(
p(textOutput("filename")),
plotOutput("chart")
)
)
)
```
Here, we have wrapped the file selector in a `div`
so that we have a named element after which to insert our date range selector,
but *haven't* included the date range selector (yet).
The outline of the corresponding server is:
```{r eval=FALSE}
server <- function(input, output){
currentData <- reactive({
# ...provide currentData...
})
selectedData <- reactive({
# ...provide selectedData...
})
observeEvent(input$datafile, {
# ...insert year selector when datafile changes...
})
output$chart <- renderPlot({
# ...update chart when selectedData changes...
})
output$filename <- renderText({
# ...update displayed filename when selected file changes...
})
}
```
The zero'th change is getting rid of the `session` variable:
we don't need it any longer because we're not modifying the interface from the server.
The first change is to create a reactive variable for the selected data.
We need this because the chart depends on the selected data,
while the range of years we can select depends on the current (actual) data.
As a rule,
everywhere we might need to "see" data,
we should create a reactive variable.
The function `observeEvent` allows us to create event handlers that aren't directly attached to display objects;
we need one so that we can create the year display.
Once that's set up,
`currentData` is straightforward:
if the filename changes, load that CSV file:
```{r eval=FALSE}
currentData <- reactive({
read_csv(input$datafile$datapath)
})
```
`selectedData` is also straightforward:
if `currentData` changes, filter by year range:
```{r eval=FALSE}
selectedData <- reactive({
req(input$years)
currentData() %>%
filter(between(year, input$years[1], input$years[2]))
})
```
This function uses `currentData()` so that Shiny knows it depends on changes to the current data.
But how do we know we *have* a year range?
The answer is that `req(input$years)` means "make sure this thing exists before going any further".
Once we have the years, we can filter as required.
Now for the clever bit:
we will create a slider *after* loading a data file.
More specifically,
we will use `observeEvent(input$datafile, {...})` to indicate that this action depends on changes to the filename,
then get the current data,
grab the year range,
create a `sliderInput`,
and use `insertUI` to add it after the `div` we created:
```{r eval=FALSE}
observeEvent(input$datafile, {
current <- currentData()
lowYear <- min(current$year)
highYear <- max(current$year)
insertUI(
selector = "#datafileInput",
where = "afterEnd",
ui = sliderInput("years", "years",
min = lowYear,
max = highYear,
value = c(lowYear, highYear),
sep = "")
)
})
```
Creating the chart and displaying the filename is done as before,
though we have switched to `ifelse` for the filename's value to be idiomatic.
Note that the chart depends on `selectedData()` and *not* the raw data:
```{r eval=FALSE}
output$chart <- renderPlot({
selectedData() %>%
group_by(year) %>%
summarize(average = mean(estimate, na.rm = TRUE)) %>%
ggplot() +
geom_line(mapping = aes(x = year, y = average)) +
labs(title = paste("Years", input$years[1], "to", input$years[2]))
})
output$filename <- renderText({
currentName <- input$datafile$name
ifelse(is.null(currentName), "no filename set", paste("showing", currentName))
})
```
Here are the dependencies we have created:
```{r}
knitr::include_graphics("figures/shiny/unicef-slider-dependencies.png")
```
It works!
Except that we're adding a slider every time we open a file,
so if we open the same file twice, we get two sliders with identical ranges:
```{r}
knitr::include_graphics("figures/shiny/unicef-slider.png")
```
## How can I control how the user interface is rendered?
Our last refinement uses `uiOutput` and `renderUI` to (re-)create the slider at just the right moment.
The UI looks familiar,
except there's a `uiOutput` placeholder where the slider is to go---its name "slider"
will be used in the server:
```{r eval=FALSE}
ui <- fluidPage(
titlePanel("UNICEF Data"),
sidebarLayout(
position = "right",
sidebarPanel(
img(src = "logo.png", width = 200),
fileInput("datafile", p("data file")),
uiOutput("slider")
),
mainPanel(
p(textOutput("filename")),
plotOutput("chart")
)
)
)
```
`uiOutput` is always used in conjunction with `renderUI` in the server,
so let's look at the server:
```{r eval=FALSE}
server <- function(input, output){
currentData <- reactive({
# ...get the data...
})
output$slider <- renderUI({
# ...create a widget to allow year selection...
})
selectedData <- reactive({
# ...select data using values from the year selector...
})
output$chart <- renderPlot({
# ...draw the chart...
})
output$filename <- renderText({
# ...display the filename...
})
}
```
What does `currentData` look like?
```{r eval=FALSE}
currentData <- reactive({
req(input$datafile)
read_csv(input$datafile$datapath)
})
```
We use `req(...)` to tell Shiny that there's no point proceeding unless `input$datafile` actually has a value,
because we can't load data if we have a `NULL` filename.
Once we have data,
we create a slider or overwrite the existing slider if there already is one;
this prevents the problem of multiple sliders.
Note that the slider's ID is `"years"`
and that its range is set based on data,
so we avoid the problem of having to create a slider when we don't know what its range should be:
```{r eval=FALSE}
output$slider <- renderUI({
current <- currentData()
lowYear <- min(current$year)
highYear <- max(current$year)
sliderInput("years", "years",
min = lowYear,
max = highYear,
value = c(lowYear, highYear),
sep = "")
})
```
Once we have a slider,
we select the data;
this depends on the years from the slider,
so we make that explicit using `req`:
```{r eval=FALSE}
selectedData <- reactive({
req(input$years)
currentData() %>%
filter(between(year, input$years[1], input$years[2]))
})
```
Displaying the chart and the filename are exactly as we've seen before:
the chart depends on `selectedData`
and the filename display depends on `input$datafile$name`.
So how and why does this all work?
1. When the UI is initially created:
- There is no data file, so `req(input$datafile)` in the definition of `currentData` halts.
- Without `currentData`, the `renderUI` call used to create the slider doesn't proceed.
- So the UI doesn't get a slider and doesn't try to display data it doesn't have.
2. When a filename is selected for the first time:
- `input$datafile` gets a value.
- So we load data *and* we can display the filename.
- `currentData` now has a meaningful value.
- So we can create a slider *and* initialize its limits to the min and max years from the actual data.
- So `selectedData` can now be constructed (all of the things it depends on exist).
- So we can draw the chart.
3. When a new file is selected:
- `input$datafile` gets a value.
- So we load data and display the filenmae.
- `currentData` is then re-created.
- So we replace the slider with a new one whose bounds are set by the new data.
- And then construct `selectedData` and draw the chart.
This isn't the only way to build our interface,
but the alternatives use more advanced functions like `freeze` and are harder to debug.
The way to get where we want to is to break everything down into single actions,
each of which is probably smaller than we might first expect.
It takes a bit of practice,
but once you're used to it,
you'll be able to build some powerful tools with just a page or two of code.
## Key Points
```{r, child="keypoints/shiny.md"}
```
```{r, child="etc/links.md"}
```