-
Notifications
You must be signed in to change notification settings - Fork 16
/
shiny.qmd
374 lines (284 loc) · 9.53 KB
/
shiny.qmd
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
---
title: 'R advanced: webapps with Shiny'
format:
gfm:
toc: true
toc-depth: 3
editor: source
date: today
author: UQ Library
---
```{r setup, include=FALSE}
# default to not evaluating chunks!
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
## Shiny webapps
Shiny is a package that allows to create a web application with R code.
A Shiny app requires two main elements:
* a user interface (UI)
* a server
Let's build an app from scratch, using our ACORN data and functions.
What we want to create is a small webapp that visualises Australian temperature data and gives the user a bit of control over the visualisation.
### Setting up
#### Base project
We will first download our base project that contains custom functions to get our data ready.
* Download the [project archive](https://download-directory.github.io/?url=https%3A%2F%2Fgithub.com%2Fuqlibrary%2Ftechnology-training%2Ftree%2Fmaster%2FR%2Fpackaging), and extract it wherever you'd like to store your project.
* Open the .Rproj file
* Create a new script: "New File > R Script"
#### Get the data
We can source our custom functions that make it easier for us to download the ACORN data and merge all the datasets into one big file:
```{r}
source("get_acorn.R")
source("read_station.R")
source("merge_acorn.R")
get_acorn("acorn_data")
library(tidyverse)
all_stations <- merge_acorn("acorn_data")
```
We now have a single object that contains data from 112 weather stations around Australia.
### Create a new app
In our project, let's create a new app with "File > New File > Shiny Web App...". We will stick to "single file", and the current project directory as the location.
In our files, we can now see a "myApp" directory that contains an "app.R" script.
The app is currently an example app. We can run it with the "Run App" button, and you can see what kind of interaction a basic Shiny app can offer: a slider to change the number of bins in a histogram, for example.
### Creating a minimal skeleton
For our app to work, we need three sections:
* define a UI: what users see
* define a server: what happens in the background
* define how the app is run
Back in the app.R file, we can start with this empty skeleton:
```{r}
# Load necessary packages
library(shiny)
# UI
ui <- fluidPage()
# Server
server <- function(input, output) {}
# Run the application
shinyApp(ui = ui, server = server)
```
Running it will show a blank page. Let's add a title:
```{r}
# UI
ui <- fluidPage(
titlePanel("ACORN data explorer")
)
```
### Prepare the data
Now, let's make sure we have the data ready to be used in our app. We don't want to do the summarising of our data every time we run the app, so let's save the finished product into an RDS file. Back in our first script, let's write:
```{r}
# process for monthly average
monthly <- all_stations %>%
group_by(month = month(date),
year = year(date)) %>%
summarise(mean.max = mean(max.temp, na.rm = TRUE))
```
Let's save that object into our app directory, so the app can find it:
```{r}
saveRDS(monthly, "myApp/monthly.rds")
```
This dataset will be the base of our Shiny app.
### Interactive tables
We can now read that data file into our app, process it, and present it in an interactive table, using the DT package:
```{r}
# Import data
monthly <- readRDS("monthly.rds")
# Load necessary packages
library(shiny)
library(DT)
# Define UI
ui <- fluidPage(
titlePanel("ACORN data explorer"),
DTOutput("dt")
)
# Define server logic
server <- function(input, output) {
output$dt <- renderDT({
monthly
})
}
```
Notice that we had to define an output in the server section (with a "render" function), and use that output in a UI function (with an "output" function).
#### Plots
Now, for a different kind of output, let's add a plot:
```{r}
# Load necessary packages
library(shiny)
library(DT)
# Define UI
ui <- fluidPage(
titlePanel("ACORN data explorer"),
plotOutput("plot"),
DTOutput("dt")
)
# Define server logic
server <- function(input, output) {
output$dt <- renderDT({
monthly
})
output$plot <- renderPlot({
ggplot(monthly,
aes(x = year, y = month, fill = mean.max)) +
geom_tile() +
scale_fill_distiller(palette = "RdYlBu")
})
}
```
Again, we have to:
* Define how the plot is generated on the server
* Save the plot as an output, using the right `render*` function
* Show the plot in the UI with the right `*Output` function
#### User input
How can we add some interaction? We could give the user control over which month they want to visualise by adding a slider:
```{r}
# Load necessary packages
library(shiny)
library(DT)
library(ggplot2)
library(dplyr)
# Define UI
ui <- fluidPage(
titlePanel("ACORN data explorer"),
# input slider for months
sliderInput("month",
"Pick a month:",
min = 1,
max = 12,
value = 1),
plotOutput("plot"),
DTOutput("dt")
)
# Define server logic
server <- function(input, output) {
output$dt <- renderDT({
monthly
})
output$plot <- renderPlot({
monthly %>%
filter(month == input$month) %>%
ggplot(aes(x = year, y = month, fill = mean.max)) +
geom_tile() +
scale_fill_distiller(palette = "RdYlBu")
})
}
# Run the application
shinyApp(ui = ui, server = server)
```
### Challenge 1: restore an "all months" option?
How could we give the option to go back to the full-year view?
Hint: have a look at `?selectInput`, or find other ideas on this list: https://shiny.rstudio.com/tutorial/written-tutorial/lesson3/
One solution could be:
```{r}
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("ACORN data explorer"),
# input slider for months
selectInput("month",
"Pick one or more months:",
1:12,
multiple = TRUE),
plotOutput("plot"),
DTOutput("dt")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$dt <- renderDT({
monthly
})
output$plot <- renderPlot({
monthly %>%
filter(month %in% input$month) %>%
ggplot(aes(x = year, y = month, fill = mean.max)) +
geom_tile() +
scale_fill_distiller(palette = "RdYlBu")
})
}
```
### Theming
To change the theme of the app, we can use the bslib package, and change the `theme` argument in `fluidPage()`:
```{r}
# Load necessary packages
library(shiny)
library(DT)
library(ggplot2)
library(dplyr)
library(bslib)
# Define UI for application that draws a histogram
ui <- fluidPage(
theme = bs_theme(bootswatch = "solar"),
titlePanel("ACORN data explorer"),
# input slider for months
selectInput("month",
"Pick one or more months:",
1:12,
multiple = TRUE),
plotOutput("plot"),
DTOutput("dt")
)
```
You can see the different themes available with the `bootswatch_themes()` function.
This is great to quickly change the general look of our app, but our visualisation looks out of place: how can we also change the theme for ggplot2? Let's use the convenient thematic package:
```{r}
# Load necessary packages
library(shiny)
library(DT)
library(ggplot2)
library(dplyr)
library(bslib)
library(thematic)
thematic_shiny()
```
Now, the theme propagates to ggplot2 visualisations.
### Challenge 2: make the plot interactive
Using the plotly package, how could you make the plot interactive?
Remember to change the code that generates the plot _as well as_ the render and output functions.
```{r}
# import data
monthly <- readRDS("monthly.rds")
# Load necessary packages
library(shiny)
library(DT)
library(ggplot2)
library(plotly)
library(dplyr)
library(bslib)
library(thematic)
thematic_shiny()
# Define UI for application that draws a histogram
ui <- fluidPage(
theme = bs_theme(bootswatch = "solar"),
titlePanel("ACORN data explorer"),
# input slider for months
selectInput("month",
"Pick one or more months:",
1:12,
multiple = TRUE),
plotlyOutput("plot"),
DTOutput("dt")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$dt <- renderDT({
monthly
})
output$plot <- renderPlotly({
p <- monthly %>%
filter(month %in% input$month) %>%
ggplot(aes(x = year, y = month, fill = mean.max)) +
geom_tile() +
scale_fill_distiller(palette = "RdYlBu")
ggplotly(p)
})
}
# Run the application
shinyApp(ui = ui, server = server)
```
The user can now hover over parts of the plot to see the corresponding data.
## Publishing a Shiny app
You can use [ShinyApps.io](https://www.shinyapps.io/), which offers free or paid accounts. This is integrated into RStudio to easily deploy and updae your applications.
We also have access to ARDC's [Nectar](https://dashboard.rc.nectar.org.au) (National eResearch Collaboration Tools and Resources project), in which we can request a virtual machine and deploy a Shiny server.
Other options exist, see for example this [comparison table](https://hosting.analythium.io/assets/files/shiny-hosting-options.html).
## Useful links
* [Official Shiny tutorial](https://shiny.posit.co/r/getstarted/shiny-basics/lesson1/index.html)
* [Gallery of Shiny examples](https://shiny.posit.co/r/gallery/)
* Hadley Wickham's book _[Mastering Shiny](https://mastering-shiny.org/index.html)_
* [Shiny cheatsheet](https://rstudio.github.io/cheatsheets/html/shiny.html)