-
Notifications
You must be signed in to change notification settings - Fork 55
/
10-drill-thru.Rmd
227 lines (189 loc) · 5.4 KB
/
10-drill-thru.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
---
title: "Dashboard drill-down"
output: html_notebook
---
## Class catchup
```{r}
library(dplyr)
library(dbplyr)
library(DBI)
library(dbplot)
library(purrr)
# Class catchup
con <- DBI::dbConnect(odbc::odbc(), "Postgres Dev")
airports <- tbl(con, in_schema("datawarehouse", "airport"))
flights <- tbl(con, in_schema("datawarehouse", "flight"))
carriers <- tbl(con, in_schema("datawarehouse", "carrier"))
airline_list <- carriers %>%
select(carrier, carriername) %>%
collect() %>%
split(.$carriername) %>%
map(~.$carrier)
```
## 10.1 -Add a tabset to the dashboard
1. Wrap the "output" functions in the **ui** with a `tabPanel()`
```{r, eval = FALSE}
# Goes from this
valueBoxOutput("total"),
dataTableOutput("monthly")
# To this
tabPanel(
valueBoxOutput("total"),
dataTableOutput("monthly")
)
```
2. Set the panel's `title` and `value`. The new code should look like this
```{r, eval = FALSE}
tabPanel(
title = "Dashboard",
value = "page1",
valueBoxOutput("total"),
dataTableOutput("monthly")
)
```
3. Wrap that code inside a `tabsetPanel()`, set the `id` to `tabs`
```{r, eval = FALSE}
tabsetPanel(
id = "tabs",
tabPanel(
title = "Dashboard",
value = "page1",
valueBoxOutput("total"),
dataTableOutput("monthly")
)
)
```
4. Re-run the app
## 10.2 - Add interactivity
1. Set the `selection` and `rownames` in the current `datatable()` function
```{r, eval = FALSE}
output$monthly <- renderDataTable(datatable({
base_dashboard() %>%
group_by(month) %>%
tally() %>%
collect() %>%
mutate(n = as.numeric(n)) %>%
rename(flights = n) %>%
arrange(month)},
list( target = "cell"), # New code
rownames = FALSE)) # New code
```
2. Use `observeEvent()` and `appendTab()` to add the interactivity
```{r, eval = FALSE}
observeEvent(input$monthly_cell_clicked, {
appendTab(
inputId = "tabs", # This is the tabsets panel's ID
tabPanel(
"test_new", # This will be the label of the new tab
renderDataTable(mtcars, rownames = FALSE)
)
)
})
```
3. Re-run the app
4. Click on a row inside the `datatable` and then select the new tab called `test_new` to see the `mtcars` data
## 10.3 - Add title to the new tab
1. Load the clicked cell's info into a variable, and create a new lable by concatenating the cell's month and the selected airline's code
```{r, eval = FALSE}
observeEvent(input$monthly_cell_clicked, {
cell <- input$monthly_cell_clicked # New code
if (!is.null(cell$value)) { # New code
tab_title <- paste0(month.name[cell$value], "_", input$select)
appendTab(
inputId = "tabs",
tabPanel(
tab_title, # Changed code
renderDataTable(mtcars, rownames = FALSE)
)
)
}
})
```
2. Re-run the app, and click on one of the month's to confirm that the new label works
3. Use `updateTabsetPanel` to switch the dashboard's focus to the newly created tab. It goes after the `tabPanel()` code
```{r, eval = FALSE}
updateTabsetPanel(session, "tabs", selected = tab_title)
```
## 10-4 - pool package
1.Change `dbConnect()` to `dbPool()`
```{r, eval = FALSE}
# Goes from this
con <- DBI::dbConnect(odbc::odbc(), "Postgres Dev")
# To this
con <- pool::dbPool(odbc::odbc(), dsn = "Postgres Dev")
```
2. Add an `onStop()` step to close the pool connection
```{r, eval = FALSE}
onStop(function() {
poolClose(con)
})
```
3. The completed app should now look like this
```{r}
con <- pool::dbPool(odbc::odbc(), dsn = "Postgres Dev")
onStop(function() {
poolClose(con)
})
airports <- tbl(con, in_schema("datawarehouse", "airport"))
flights <- tbl(con, in_schema("datawarehouse", "flight"))
carriers <- tbl(con, in_schema("datawarehouse", "carrier"))
airline_list <- carriers %>%
select(carrier, carriername) %>%
collect() %>%
split(.$carriername) %>%
map(~.$carrier)
ui <- dashboardPage(
dashboardHeader(title = "Quick Example"),
dashboardSidebar(selectInput("select", "Selection", airline_list)),
dashboardBody(
tabsetPanel(id = "tabs",
tabPanel(
title = "Dashboard",
value = "page1",
valueBoxOutput("total"),
dataTableOutput("monthly")
)
)
)
)
server <- function(input, output, session) {
base_dashboard <- reactive({
flights %>%
filter(uniquecarrier == input$select)})
output$total <- renderValueBox({
base_dashboard() %>%
tally() %>%
pull() %>%
valueBox(subtitle = "Flights")})
output$monthly <- renderDataTable(datatable({
base_dashboard() %>%
group_by(month) %>%
tally() %>%
collect() %>%
mutate(n = as.numeric(n)) %>%
rename(flights = n) %>%
arrange(month)},
list( target = "cell"),
rownames = FALSE)
)
observeEvent(input$monthly_cell_clicked, {
cell <- input$monthly_cell_clicked
if(!is.null(cell$value)){
tab_title <- paste0(month.name[cell$value], "_", input$select)
appendTab(inputId = "tabs",
tabPanel(
tab_title,
DT::renderDataTable({
base_dashboard() %>%
filter(month == cell$value) %>%
select(3:10) %>%
head(100) %>%
collect()
}, rownames = FALSE)
))
updateTabsetPanel(session, "tabs", selected = tab_title)
}
})
}
shinyApp(ui, server)
```