-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10-dynamic-ui.Rmd
352 lines (273 loc) · 7.75 KB
/
10-dynamic-ui.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
# Dynamic UI
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
## 10.1.6 Exercises {-}
1.
```{r}
library(shiny)
ui <- fluidPage(
numericInput("year", "year", value = 2020),
dateInput("date", "date")
)
server <- function(input, output, session) {
# From Mastering Shiny Solutions 2021
observeEvent(input$year, {
req(input$year)
date_range <- range(as.Date(paste0(input$year, "-01-01")),
as.Date(paste0(input$year, "-12-31")))
updateDateInput(session, "date",
min = date_range[1],
max = date_range[2]
)
})
}
shinyApp(ui, server)
```
2.
```{r}
library(shiny)
library(tidyverse)
library(openintro, warn.conflicts = FALSE)
states <- unique(county$state)
ui <- fluidPage(
selectInput("state", "State", choices = states),
selectInput("county", "County", choices = NULL)
)
server <- function(input, output, session) {
observeEvent(input$state, {
req(input$state)
# pull out county names
choices <- county %>%
filter(state == input$state) %>%
pull(name) %>%
unique()
updateSelectInput(inputId = "county", choices = choices)
})
}
shinyApp(ui, server)
```
3.
```{r}
library(shiny)
library(gapminder)
continents <- unique(gapminder$continent)
ui <- fluidPage(
# add "(All)" to the list of choices
selectInput("continent", "Continent", choices = continents),
selectInput("country", "Country", choices = NULL),
tableOutput("data")
)
server <- function(input, output, session) {
observeEvent(input$continent, {
req(input$continent)
# pull out country names
choices <- gapminder %>%
filter(continent == input$continent) %>%
pull(country) %>%
unique()
updateSelectInput(inputId = "country", choices = choices)
})
output$data <- renderTable({
gapminder %>%
filter(continent == input$continent,
country == input$country)
})
}
shinyApp(ui, server)
```
4.
```{r}
library(shiny)
library(gapminder)
continents <- unique(gapminder$continent)
ui <- fluidPage(
# add "(All)" to the list of choices
selectInput("continent", "Continent", choices = c(as.character(continents), "(All)")),
selectInput("country", "Country", choices = NULL),
tableOutput("data")
)
server <- function(input, output, session) {
observeEvent(input$continent, {
req(input$continent)
if (input$continent == "(All)") {
# pull out country names
choices <- gapminder %>%
pull(country) %>%
unique()
updateSelectInput(inputId = "country", choices = choices)
} else {
# pull out country names
choices <- gapminder %>%
filter(continent == input$continent) %>%
pull(country) %>%
unique()
updateSelectInput(inputId = "country", choices = choices)
}
})
output$data <- renderTable({
if (input$continent == "(All)") {
gapminder %>%
filter(country == input$country)
} else {
gapminder %>%
filter(continent == input$continent,
country == input$country)
}
})
}
shinyApp(ui, server)
```
5.
```{r}
library(shiny)
u <- shinyUI(fluidPage(
titlePanel("Mutually Dependent Input Values"),
sidebarLayout(
sidebarPanel(
numericInput("A", "A",.333),
numericInput("B", "B",.333),
numericInput("C", "C",.333)
),
mainPanel(
verbatimTextOutput("result")
)
)
))
s <- shinyServer(function(input, output,session) {
observeEvent(input$A,{
newB <- 1 - input$A - input$C
updateNumericInput(session, "B", value = newB)
newC <- 1 - input$A - input$B
updateNumericInput(session, "C", value = newC)
})
observeEvent(input$B,{
newC <- 1 - input$B - input$A
updateNumericInput(session, "C", value = newC)
newA <- 1 - input$B - input$C
updateNumericInput(session, "A", value = newA)
})
observeEvent(input$C,{
newA <- 1 - input$C - input$B
updateNumericInput(session, "A", value = newA)
newB <- 1 - input$C - input$C
updateNumericInput(session, "B", value = newB)
})
})
shinyApp(u,s)
```
- Circular reference is the issue. Once you run this app, the numeric inputs continue to update autonomously.
## 10.2.3 Exercises {-}
2.
```{r}
library(shiny)
library(tidyverse)
# Put the unique user interface for each geom in its own tabPanel(),
# and then arrange the three tabs into a tabsetPanel()
parameter_tabs <- tabsetPanel(
id = "params",
type = "hidden",
tabPanel("geom_histogram",
numericInput("binwidth_hist", "binwidth", value = 0.2)
),
tabPanel("geom_freqpoly",
numericInput("binwidth_freq", "binwidth", value = 0.2)
),
tabPanel("geom_density",
numericInput("bw_density", "bandwidth", value = 1),
)
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("geom", "Select geom",
choices = c("geom_histogram",
"geom_freqpoly",
"geom_density")
),
parameter_tabs
),
mainPanel(
plotOutput("final_plot")
)
)
)
server <- function(input, output, session) {
# Change tabs depending on geom
observeEvent(input$geom, {
updateTabsetPanel(inputId = "params", selected = input$geom)
})
# Reactive plot
final_plot <- reactive({
switch(input$geom,
geom_histogram = ggplot(diamonds, aes(carat)) + geom_histogram(binwidth = input$binwidth_hist),
geom_freqpoly = ggplot(diamonds, aes(carat)) + geom_freqpoly(binwidth = input$binwidth_freq),
geom_density = ggplot(diamonds, aes(carat)) + geom_density(bw = input$bw_density),
)
})
# Plot
output$final_plot <- renderPlot(final_plot(), res = 96)
}
shinyApp(ui, server)
```
::: {.rmdwarning}
3. Not sure about this question, but I thought of using `checkboxInput()`
:::
## 10.3.5 Exercises {-}
1.
```{r}
library(shiny)
parameter_tabs <- tabsetPanel(
id = "params",
type = "hidden",
tabPanel("slider",
sliderInput("n", "n", value = 0, min = 0, max = 100)
),
tabPanel("numeric",
numericInput("n", "n", value = 0, min = 0, max = 100)
)
)
ui <- fluidPage(
selectInput("type", "type", c("slider", "numeric")),
parameter_tabs
)
server <- function(input, output, session) {
# Change tabs depending on type
observeEvent(input$type, {
updateTabsetPanel(inputId = "params", selected = input$type)
})
}
shinyApp(ui, server)
```
2.
```{r}
library(shiny)
ui <- fluidPage(
actionButton("go", "Enter password"),
textOutput("text")
)
server <- function(input, output, session) {
observeEvent(input$go, {
showModal(modalDialog(
passwordInput("password", NULL),
title = "Please enter your password"
))
})
output$text <- renderText({
if (!isTruthy(input$password)) {
"No password"
} else {
"Password entered"
}
})
}
shinyApp(ui, server)
```
This app has an action button titled "Enter password". Once we click on the button, we are shown a dialog box where we can enter our password. After we enter our password, we see a new message: "Password entered". When you click the enter password button a second time, we make the `input$password` NULL again, making the password disappear.
3. You lose the currently selected value. It ensures that we don’t create a reactive dependency that would cause this code to re-run every time `input$dynamic` changes (which will happen whenever the user modifies the value). We only want it to change when `input$type` or `input$label` changes.
::: {.rmdimportant}
4. Solution at [Mastering Shiny Solutions 2021](https://mastering-shiny-solutions.org/dynamic-ui.html#exercise-10.3.4.3)
:::
::: {.rmdwarning}
5. Not sure about this question because I don't know the S3 OOP system.
:::