-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic-elements.qmd
322 lines (237 loc) · 8.59 KB
/
dynamic-elements.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
---
title: "Introduction to Shiny"
subtitle: "Session - Adding dynamic elements"
---
```{r}
#| label: "libs"
#| include: false
#| eval: true
#| echo: false
library(countdown)
```
## Introducing a simplified reactive app
The real magic of Shiny happens when you have an app with inputs and outputs
. . .
and the two "react" to each other
```{r}
ui <- fluidPage(
textInput(inputId = "name",
label = "What's your name?"),
textOutput("greeting")
)
server <- function(input, output, session) {
output$greeting <- renderText({
paste0("Hello ", input$name, "!")
})
}
```
## Looking for "name" and "greeting"
::: {.incremental}
- The ui `textInput()` is providing the information for the server `input$name`
- And that is updating the object `output$greeting` in the server that updates the `textOutput("greeting")` in the ui
- The updates occur every time something is changed
:::
## The mental model
Unlike with R where updates like this occur when we run the code, here Shiny is being told *how it could* create the string if it needs to. The possibilities are:
::: {.incremental}
- This code is never run as the option is not used
- It might be run as soon as the app is launched
- It might be run once some time after the launch
- ... there are many possibilities
:::
. . .
:::{.callout-tip collapse=false appearance='default' icon=true}
## Tip!
Think of a Shiny app as providing recipes and not giving commands!
:::
## Introducing a new input
Four inputs were mentioned previously:
* `sliderInput()`
* `selectInput()`
* `textInput()`
* `numericInput()`
. . .
And there are more! We are going to use `checkboxGroupInput()` in a new app.
## Building a new app
Starting again with a new script, typing `shinyapp` and then Shift + Tab add the code to `ui`, save and Run App
```{r}
titlePanel("Basic Demo"), # Title
checkboxGroupInput(
inputId = "name1",
label = "What are your favourite languages?",
choices = c("SQL", "Python", "R")
),
```
## Adding an action button
Next we'll add an action button so, after making a selection, the user presses a button for the next thing to happen:
```{r}
actionButton(inputId = "name2",
label = "List the selections",
icon = icon("question"))
```
. . .
:::{.callout-warning collapse=false appearance='default' icon=true}
## Don't forget the comma!
- In the ui the functions need a comma to separate
- The code will break unless it reads `checkboxGroupInput(...)` **,** `actionButton(...)`
:::
## Panels
Before we move to the interactive part of shiny we've currently got the layout where everything is appearing on the page as if it were a document.
## Panel functions
To give the app more structure that are more familiar we need to introduce three functions:
```{r}
#| code-line-numbers: 2|3|15
ui <- fluidPage(titlePanel("Basic Demo"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(
inputId = "name1",
label = "What are your favourite languages?",
choices = c("SQL", "Python", "R")
),
actionButton(
inputId = "name2",
label = "Selected",
icon = icon("question")
)
),
mainPanel(textOutput(outputId = "name3"))
)
)
```
## Tips
::: {.incremental}
- All three functions `sidebarLayout()`, `sidebarPanel()` and `mainPanel()` are needed to run the app
- If the copying and pasting loses the formatting, highlight and use `Ctrl+i` for indentation
- Alternatively install {styler} which can reformat selection or the entire script
:::
## `output$` and `input$` in the server
::: {.incremental}
- We now need to add in the server and have it "reactive" to what is selected in the ui
- The output will be simple in that we'll just show what is selected (a useful thing sometimes for debugging)
- Note in the `mainPanel()` the function `textOutput()` is used and that has a partnering function: `renderText()` in the server
:::
## server code
```{r}
server <- function(input, output, session) {
observeEvent(input$name2, {
output$name3 <- renderText(input$name1)
})
}
```
Copy and paste this, save and run
## Exercise - renaming placeholders
Replace the names `name1`, `name2`, `name3` with more descriptive names, paying attention to where the repetitions occur
```{r}
#| eval: true
#| echo: false
countdown::countdown(minutes = 10,
color_border = "#005EB8",
color_text = "#005EB8",
color_running_text = "white",
color_running_background = "#005EB8",
color_finished_text = "#005EB8",
color_finished_background = "white",
margin = "0.9em",
font_size = "2em")
```
## `observeEvent()`
::: {.incremental}
- The app takes the input from the button in this example and until the button is pressed nothing will be printed.
- Once pressed any selection or de-selection is updated automatically.
- What about making the app require the button to be pressed before printing?
- For that we need a different function...
:::
## `bindEvent()`
`bindEvent()` can be used to execute only when a button is pressed. We'll replace the code
```{r}
observeEvent(input$name2, {
output$name3 <- renderText(input$name1)
})
```
with
```{r}
output$name3 <- renderText({
input$name1
}) |>
bindEvent(input$name2)
```
:::{.callout-warning collapse=false appearance='default' icon=true}
## Remember your code will be different now
- In the last slide you will have changed `name1` and so on to something more readable!
- If names don't match you might not get an error just nothing prints.
:::
## The formal names - imperative and declarative
Mastering Shiny explains this really well:
::: {.incremental}
- Imperative is where you issue a particular command and it's carried out immediately.
- This is what we are familiar with in analysis scripts.
- Declarative describes the goals and the software figures out how to achieved them without intervention.
:::
## Laziness
Although it's a strength of Shiny that declarative programming is lazy it does mean that code can have bugs but no errors.
If you recall when we changed `names1` to other names there was **no error** if these did not appear in both the ui and server.
This applies to functions so if don't pair them correctly:
```{r}
output$name3 <- renderPlot({ # should be renderText()
input$name1
}) |>
bindEvent(input$name2)
```
## Execution order
The familiar analysis scripts are run in the order of lines.
Quarto reports for example, need to particularly have everything in the right order to run.
This isn't the case for Shiny!
## [Exercise (from Mastering Shiny)](https://mastering-shiny.org/basic-reactivity.html#exercises-3)
Given this UI:
```{r}
ui <- fluidPage(
textInput("name", "What's your name?"),
textOutput("greeting")
)
```
Fix the simple errors found in each of the three server functions below. First try spotting the problem just by reading the code; then run the code to make sure you’ve fixed it.
```{r}
server1 <- function(input, output, server) {
input$greeting <- renderText(paste0("Hello ", name))
}
server2 <- function(input, output, server) {
greeting <- paste0("Hello ", input$name)
output$greeting <- renderText(greeting)
}
server3 <- function(input, output, server) {
output$greting <- paste0("Hello", input$name)
}
```
```{r}
#| eval: true
#| echo: false
countdown::countdown(minutes = 8,
color_border = "#005EB8",
color_text = "#005EB8",
color_running_text = "white",
color_running_background = "#005EB8",
color_finished_text = "#005EB8",
color_finished_background = "white",
margin = "0.9em",
font_size = "2em")
```
## Solution
```{r}
server1 <- function(input, output, session) {
output$greeting <- renderText(paste0("Hello ", input$name))
}
server2 <- function(input, output, server) {
greeting <- reactive(paste0("Hello ", input$name))
output$greeting <- renderText(greeting())
}
server3 <- function(input, output, session) {
output$greeting <- renderText({
paste0("Hello ", input$name)
})
}
```
## End session
### Acknowledgements
[Mastering Shiny](https://mastering-shiny.org/basic-app.html) by Hadley Wickham
[Building Web Apps with R Shiny](https://debruine.github.io/shinyintro/first-app.html) from [PsyTeachR](https://psyteachr.github.io/)