-
Notifications
You must be signed in to change notification settings - Fork 55
/
09-dashboards.Rmd
185 lines (154 loc) · 4.38 KB
/
09-dashboards.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
---
title: "Intro to dashboards"
output: html_notebook
---
## Class catchup
```{r}
library(tidyverse)
library(dbplyr)
library(DBI)
library(dbplot)
# 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"))
```
## 9.1 - Basic structure
1. Create and preview a simple `shinydashboard`
```{r, eval = FALSE}
library(shinydashboard)
library(shiny)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Quick Example"),
dashboardSidebar(selectInput("select", "Selection", c("one", "two"))),
dashboardBody(
valueBoxOutput("total"),
dataTableOutput("monthly")
)
)
server <- function(input, output, session) {
output$total <- renderValueBox(valueBox(100, subtitle = "Flights"))
output$monthly <- renderDataTable(datatable(mtcars))
}
shinyApp(ui, server)
```
## 9.2 - Dropdown data
1. Use `purrr` to create a list with the correct structure for the `shiny` drop down
```{r}
airline_list <- carriers %>%
select(carrier, carriername) %>% # In case more fields are added
collect() %>% # All would be collected anyway
split(.$carriername) %>% # Create a list item for each name
map(~.$carrier) # Add the carrier code to each item
head(airline_list)
```
2. In the app code, replace `c("one", "two", "three")` with `airline_list`
```{r, eval = FALSE}
# Goes from this:
dashboardSidebar(selectInput("select", "Selection", c("one", "two"))),
# To this:
dashboardSidebar(selectInput("select", "Selection", airline_list)),
```
3. Re-run the app
## 9-3 - Update dashboard items
1. Save the base "query" to a variable. It will contain a carrier selection. To transition into `shiny` programming easier, the variable will be a function.
```{r}
base_dashboard <- function(){
flights %>%
filter(uniquecarrier == "DL")
}
head(base_dashboard())
```
3. Use the base query to figure the number of flights for that carrier
```{r}
base_dashboard() %>%
tally() %>%
pull()
```
4. In the app, remove the `100` number and pipe the `dplyr` code into the valueBox() function
```{r, eval = FALSE}
# Goes from this:
output$total <- renderValueBox(valueBox(100, subtitle = "Flights"))
# To this:
output$total <- renderValueBox(
base_dashboard() %>%
tally() %>%
pull() %>%
valueBox(subtitle = "Flights"))
```
5. Create a table with the month name and the number of flights for that month
```{r}
base_dashboard() %>%
group_by(month) %>%
tally() %>%
collect() %>%
mutate(n = as.numeric(n)) %>%
rename(flights = n) %>%
arrange(month)
```
6. In the app, replace `head(mtcars)` with the piped code, and re-run the app
```{r, eval = FALSE}
# Goes from this:
output$monthly <- renderTable(head(mtcars))
# To this:
output$monthly <- renderDataTable(datatable(
base_dashboard() %>%
group_by(month) %>%
tally() %>%
collect() %>%
mutate(n = as.numeric(n)) %>%
rename(flights = n) %>%
arrange(month)))
```
## 9.4 - Integrate the dropdown
1. In the original `base_dashboard()` code, replace `function` with `reactive`, and `"DL"` with `input$select`
```{r, eval = FALSE}
# Goes from this
base_dashboard <- function(){
flights %>%
filter(uniquecarrier == "DL")}
# To this
base_dashboard <- reactive({
flights %>%
filter(uniquecarrier == input$select)})
```
2. Insert the new code right after the `server <- function(input, output, session)` line. The full code should now look like this:
```{r, eval = FALSE}
ui <- dashboardPage(
dashboardHeader(title = "Quick Example"),
dashboardSidebar(selectInput("select", "Selection", airline_list)),
dashboardBody(
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)
))
}
shinyApp(ui, server)
```
9. Re-run the app
10. Disconnect form database
```{r}
dbDisconnect(con)
```