-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackages_i.Rmd
192 lines (141 loc) · 4.99 KB
/
packages_i.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
---
title: "Packages I"
description: |
dplyr and plots with ggplot2 & Plotly
output:
distill::distill_article:
toc: true
toc_float: true
toc_depth: 4
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
Shiny provides the basic tools for client-server interaction, but a lot of parts that make up an app--how it wrangles and displays data, can come from packages of your choice. All of the packages introduced in the R Learning Series so far (`dplyr`, `ggplot2`, `plotly`, `data.table`, `R markdown`, `sf`, `leaflet`) can be included in Shiny.
# Plots
`ggplot2` can be used with the standard `plotOutput()` and `renderPlot({})` functions.
## ggplot2
```{r}
# Server
output$plot <- renderPlot({
filtered_df() %>%
ggplot(aes(x = year, y = count, color = state)) +
geom_line() +
facet_wrap(vars(sex), nrow = 2, scales = "free_y")
})
```
To add more content to our plot (and table), enable the dropdown menu to allow the user to select multiple states.
```{r}
# UI
# add multiple argument to allow more than one selection
selectInput("state",
label = 'Select State',
choices = unique(df$state),
selected = 'Washington',
multiple = TRUE)
```
```{r}
# Server
# allow filtering of multiple states with `%in%`
filtered_df <- eventReactive(input$go, {
df %>%
filter(name == clean_name() & state %in% input$state)
})
```
## Plotly
[Plotly](https://plotly.com/ggplot2/), a package that helps create interactive graphs, can be used to convert a static `ggplot2` graph into an interactive one. [Plotly](https://plotly.com/r/) on its own can also be used in lieu of ggplot2.
To render Plotly graphs in Shiny, use `plotlyOutput()` and `renderPlotly({})` in lieu of the standard plot output and render functions.
Include the `plotly` package in the global section.
```{r}
library(plotly)
```
Convert existing plot output and render functions to their respective Plotly equivalent.
```{r}
# UI
# convert plotOutput to a Plotly Output
plotlyOutput("plot")
```
```{r}
# Server
# convert renderPlot to renderPlotly
output$plot <- renderPlotly({
# create ggplot object
p <- filtered_df() %>%
ggplot(aes(x = year, y = count, color = state)) +
geom_line() +
facet_wrap(vars(sex), nrow = 2, scales = "free_y")
# wrap ggplot object with ggplotly
ggplotly(p)
})
```
# Extra: Tidy Evaluation
Typically in `dplyr` functions, data-variables (a.k.a column names) are entered directly in the function without any extra syntax (`$` or quotation marks). Data-variables mapped in `ggplot2`’s `aes()` also follow the same concept.
- `df %>% arrange(count)`
- `ggplot(df, aes(x = year, y = count))`
However when the data-variable (e.g. `count`) is stored in a variable (e.g. `my_column <- 'count'`), or as part of another object (e.g. `input$my_input_widget`), additional syntax is required to accommodate this indirection.
- for single data-variables: `.data[[ <insert data-variable> ]]`
- for multiple data-variables in dplyr: `across(all_of( <insert data-variable> ))`
Let's add a dropdown menu that allows the user to choose which column to sort the table by.
```{r}
ui <- fluidPage(
title,
src,
sidebarLayout(
sidebarPanel(
txt_box,
actionButton("clear", label = "Clear Name"),
txt_disp,
selectInput("state",
label = 'Select State',
choices = unique(df$state),
selected = 'Washington',
multiple = TRUE),
# add dropdowns for user to decide how table is sorted
selectInput('table_sort',
label = 'Sort table by',
choices = c('count', 'sex', 'year')),
actionButton("go", label = "Enter")
),
mainPanel(
fluidRow(
column(
width = 6,
h3("Table"),
tbl_disp
),
column(
width = 6,
h3("Plot"),
plotlyOutput("plot")
)
)
)
)
)
```
In the server, apply the `.data[[]]` wrapper inside `arrange()`.
```{r}
# Server
filtered_df <- eventReactive(input$go, {
df %>%
filter(name == clean_name() & state %in% input$state) %>%
arrange(.data[[input$table_sort]]) # add arrange, pass input value using tidy evaluation syntax
})
```
If there are multiple values selected by the user for an input and applied in `dplyr`, wrap the reactive source within `across(all_of())`.
```{r}
# UI
# add dropdowns for user to decide how table is sorted
selectInput('table_sort',
label = 'Sort table by',
choices = c('count', 'sex', 'year'),
multiple = TRUE) # allow more than one selection
```
```{r}
# Server
filtered_df <- eventReactive(input$go, {
df %>%
filter(name == clean_name() & state %in% input$state) %>%
arrange(across(all_of(input$table_sort))) # add arrange, pass input value using tidy evaluation syntax
})
```