-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic-ui.qmd
284 lines (207 loc) · 6.58 KB
/
basic-ui.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
---
title: "Introduction to Shiny"
subtitle: "Session - Basic User Interface (UI)"
---
```{r}
#| label: "libs"
#| include: false
#| eval: true
#| echo: false
library(countdown)
```
## Three parts
::: {.incremental}
- Inputs - text, numeric values, dates, file uploads
- Outputs - text, tables, plots, downloads
- Layout - sidebars, tabsets, themes
:::
## Inputs
Input functions are often camelCase with the word `Input` in their name:
* `sliderInput()`
* `selectInput()`
* `textInput()`
* `numericInput()`
Each have the first argument or parameter as `inputId =` and is the link between the UI with the Server.
## Functions and parameters
Like all functions the arguments names can be dropped and read by the order.
So instead of:
```{r}
sliderInput(inputId = "min",
name = "Limit (minimum)",
value = 50,
min = 0)
```
you will often see:
```{r}
sliderInput("min",
"Limit (minimum)",
value = 50,
min = 0)
```
. . .
:::{.callout-tip collapse=false appearance='default' icon=true}
## First two argument names
All inputs need an `inputId` and a `label` so these are conventionally omitted.
:::
## Rules for `inputId = `
Only letters, numbers and underscores, never use:
* spaces
* dashes
* full stops
* any other special characters
. . .
It must also be unique so that the UI and Server are linked.
## `label = ` parameter
The second argument or parameter is the human readable label for the control.
There are no restrictions for this at all but be mindful of the reader!
## Built in options for UI controls
[Mastering Shiny](https://mastering-shiny.org/basic-ui.html#inputs) details a number of the controls including:
* free text
* dates
* limited choice (radio buttons)
* multiple choice (check boxes)
* file uploads
. . .
And there are always extension packages - {shinyWidgets}, {colorpicker}, {sorttable}
## Outputs
::: {.incremental}
- The UI outputs act like place holders that are later filled by the Server.
- Naming of these needs to also be unique
- Argument names are usually omitted (like the input functions `inputId` and `label`)
- Outputs are mainly what you find in a report: text, tables, plots, file downloads
:::
## Text UI output functions (paired)
::: {.incremental}
- For regular text the UI function `textOutput()` is usually paired with the server function `renderText()`
- For a console printed look the UI function `verbatimTextOutput()`is usually pared with the server function `renderPrint()`
:::
. . .
```{r}
ui <- fluidPage(
textOutput("text"),
verbatimTextOutput("print")
)
server <- function(input, output, session) {
output$text <- renderText("hello!")
output$print <- renderPrint("hello!")
}
```
Let's see what happens by creating a new shiny app, saving it and running it.
## `{}` and reducing computation
The curly brackets are useful for running code over multiple lines:
```{r}
ui <- fluidPage(
textOutput("text"),
verbatimTextOutput("print")
)
server <- function(input, output, session) {
output$text <- renderText({
"hello!"
})
output$code <- renderPrint({
"hello!"
})
}
```
But the recommendation is to do as little computation as possible in the render functions which means they are often omitted.
## Tables
::: {.incremental}
- For static tables the UI function `tableOutput()` is paired with the server function `renderTable()`
- For dymanic tables the UI function `dataTableOutput()`is pared with the server function `renderDataTable()`
:::
. . .
```{r}
ui <- fluidPage(
tableOutput("static"),
dataTableOutput("dynamic")
)
server <- function(input, output, session) {
output$static <- renderTable(head(mtcars))
output$dynamic <- renderDataTable(mtcars, options = list(pageLength = 5))
}
```
Let's see what happens by creating a new shiny app, saving it and running it.
## Plots
::: {.incremental}
- For plots the UI function `plotOutput()` is paired with the server function `renderPlot()`
- This can be used for base R, {ggplot2} and other packages
- Plots can be made interactive
:::
. . .
```{r}
ui <- fluidPage(
plotOutput("plot", width = "400px")
)
server <- function(input, output, session) {
output$plot <- renderPlot(plot(1:5), res = 96)
}
```
Let's see what happens by creating a new shiny app, saving it and running it.
## [Exercise (from Mastering Shiny)](https://mastering-shiny.org/basic-ui.html#exercises-2)
Which of `textOutput()` and `verbatimTextOutput()` should each of the following render functions be paired with?
```{r}
renderPrint(summary(mtcars))
renderText("Good morning!")
renderPrint(t.test(1:5, 2:6))
```
```{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}
ui <- fluidPage(
textOutput("greeting"),
verbatimTextOutput("summary"),
verbatimTextOutput("ttest")
)
server <- function(input, output, session) {
output$summary <- renderPrint(summary(mtcars))
output$greeting <- renderText("Good morning!")
output$ttest <- renderPrint(t.test(1:5, 2:6))
}
```
## Adding Alt text
Putting the `{}` back in to make the code clearer, parameters like `res` and `alt` for alt text go outside the brackets
```{r}
#| code-line-numbers: 9
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
plot(1:5)
}, res = 96, alt = "Scatterplot with random points")
}
```
. . .
:::{.callout-tip collapse=false appearance='default' icon=true}
## Checking Alt Text (tip)
- To check that alt text has worked right click on the chart and select `Inspect Element`.
- All the html will appear but you should also see `alt = "Scatterplot...`.
:::
## Reactive Alt text
Because Shiny is reactive and you may want Alt Text to reflect what is selected by the user:
```{r}
renderPlot({
# code to generate plot goes here
},
alt = reactive({
# code to add alt text goes here
})
)
```
## End session
### Acknowledgements
[Mastering Shiny](https://mastering-shiny.org/basic-app.html) by Hadley Wickham
Jumping Rivers [blog about accessibility in R](https://www.jumpingrivers.com/blog/accessibility-alt-text-in-r/)
Read more from Jumping Rivers on [accessible standards in Shiny](https://www.jumpingrivers.com/blog/accessible-shiny-standards-wcag/)