-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbasic-app.Rmd
executable file
·293 lines (233 loc) · 7.7 KB
/
basic-app.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
# Your First Shiny App
### Exercise 2.8.1 {-}
Create an app that greets the user by name. You don't know all the functions
you need to do this yet, so I've included some lines of code below. Figure out
which lines you'll use and then copy and paste them into the right place in a
Shiny app.
```{r, eval=FALSE}
textInput("name", "What's your name?")
renderText({
paste0("Hello ", input$name)
})
numericInput("age", "How old are you?")
textOutput("greeting")
tableOutput("mortgage")
renderPlot("histogram", {
hist(rnorm(1000))
}, res = 96)
```
:::solution
#### Solution {-}
In the UI, we will need a `textInput` for the user to input text, and
a `textOutput` to output any custom text to the app. The corresponding server
function to `textOutput` is `renderText`, which we can use to compose the
output element we've named "greeting".
```{r, eval=FALSE}
library(shiny)
ui <- fluidPage(
textInput("name", "What's your name?"),
textOutput("greeting")
)
server <- function(input, output, session) {
output$greeting <- renderText({
paste0("Hello ", input$name)
})
}
shinyApp(ui, server)
```
:::
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
## Exercise 2.8.2 {-}
Suppose your friend wants to design an app that allows the user to set a number
(`x`) between 1 and 50, and displays the result of multiplying this number by
5. This is their first attempt:
```{r eval=FALSE}
ui <- fluidPage(
sliderInput("x", label = "If x is", min = 1, max = 50, value = 30),
"then x times 5 is",
textOutput("product")
)
server <- function(input, output, session) {
output$product <- renderText({
x * 5
})
}
```
But unfortunately it has an error:
<br>
<center>
![placeholder](images/ex-x-times-5.png)
</center>
Can you help them find and correct the error?
:::solution
#### Solution {-}
The error here arises because on the server side we need to write `input$x`
rather than `x`. By writing `x`, we are looking for element `x` which doesn't
exist in the Shiny environment; `x` only exists within the read-only object
`input`.
```{r eval=FALSE}
library(shiny)
ui <- fluidPage(
sliderInput("x", label = "If x is", min = 1, max = 50, value = 30),
"then x times 5 is",
textOutput("product")
)
server <- function(input, output, session) {
output$product <- renderText({
input$x * 5
})
}
shinyApp(ui, server)
```
:::
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
## Exercise 2.8.3 {-}
Extend the app from the previous exercise to allow the user to set the value of
the multiplier, `y`, so that the app yields the value of `x * y`. The final
result should look like this:
`r knitr::include_graphics("./images/2.8.3.png")`
:::solution
#### Solution {-}
Let us add another `sliderInput` with ID `y`, and use both `input$x`
and `input$y` to calculate `output$product`.
```{r eval=FALSE}
library(shiny)
ui <- fluidPage(
sliderInput("x", label = "If x is", min = 1, max = 50, value = 30),
sliderInput("y", label = "and y is", min = 1, max = 50, value = 30),
"then x multiplied by y is",
textOutput("product")
)
server <- function(input, output, session) {
output$product <- renderText({
input$x * input$y
})
}
shinyApp(ui, server)
```
:::
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
## Exercise 2.8.4 {-}
Replace the UI and server components of your app from the previous exercise
with the UI and server components below, run the app, and describe the app's
functionality. Then reduce the duplication in the app by using a reactive
expression.
```{r eval=FALSE}
ui <- fluidPage(
sliderInput("x", "If x is", min = 1, max = 50, value = 30),
sliderInput("y", "and y is", min = 1, max = 50, value = 5),
"then, (x * y) is", textOutput("product"),
"and, (x * y) + 5 is", textOutput("product_plus5"),
"and (x * y) + 10 is", textOutput("product_plus10")
)
server <- function(input, output, session) {
output$product <- renderText({
product <- input$x * input$y
product
})
output$product_plus5 <- renderText({
product <- input$x * input$y
product + 5
})
output$product_plus10 <- renderText({
product <- input$x * input$y
product + 10
})
}
```
:::solution
#### Solution {-}
The application above has two numeric inputs `input$x` and `input$y`. It
computes three values: `x*y`, `x*y + 5`, and `x*y + 10`. We can reduce
duplication by making the `product` variable a reactive value and using it
within all three outputs.
```{r eval=FALSE}
library(shiny)
ui <- fluidPage(
sliderInput("x", "If x is", min = 1, max = 50, value = 30),
sliderInput("y", "and y is", min = 1, max = 50, value = 5),
"then, (x * y) is", textOutput("product"),
"and, (x * y) + 5 is", textOutput("product_plus5"),
"and (x * y) + 10 is", textOutput("product_plus10")
)
server <- function(input, output, session) {
product <- reactive(input$x * input$y)
output$product <- renderText( product() )
output$product_plus5 <- renderText( product() + 5 )
output$product_plus10 <- renderText( product() + 10 )
}
shinyApp(ui, server)
```
:::
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
## Exercise 2.8.5 {-}
The following app is very similar to one you've seen earlier in the chapter:
you select a dataset from a package (this time we're using the ggplot2 package)
and the app prints out a summary and plot of the data. It also follows good
practice and makes use of reactive expressions to avoid redundancy of code.
However there are three bugs in the code provided below. Can you find and fix
them?
```{r eval=FALSE}
library(shiny)
library(ggplot2)
datasets <- c("economics", "faithfuld", "seals")
ui <- fluidPage(
selectInput("dataset", "Dataset", choices = datasets),
verbatimTextOutput("summary"),
tableOutput("plot")
)
server <- function(input, output, session) {
dataset <- reactive({
get(input$dataset, "package:ggplot2")
})
output$summmry <- renderPrint({
summary(dataset())
})
output$plot <- renderPlot({
plot(dataset)
}, res = 96)
}
```
:::solution
#### Solution {-}
The app contains the following three bugs:
1. In the UI, the `tableOutput` object should really be a `plotOutput`.
2. In the server, the word "summry" in `output$summry` is misspelled.
* __NB: In the printed 1st edition (Exercise 1.5), the deliberate typo `summry` in the `server` was corrected and there are only 2 two bugs.__
3. In the server, the `plot` function in the `output$plot` should call
`dataset()` rather than the reactive object.
The fixed app looks as follows:
```{r eval=FALSE}
library(shiny)
library(ggplot2)
datasets <- c("economics", "faithfuld", "seals")
ui <- fluidPage(
selectInput("dataset", "Dataset", choices = datasets),
verbatimTextOutput("summary"),
# 1. Change tableOutput to plotOutput.
plotOutput("plot")
)
server <- function(input, output, session) {
dataset <- reactive({
get(input$dataset, "package:ggplot2")
})
# 2. Change summry to summary.
output$summary <- renderPrint({
summary(dataset())
})
output$plot <- renderPlot({
# 3. Change dataset to dataset().
plot(dataset())
})
}
shinyApp(ui, server)
```
:::