This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.R
314 lines (246 loc) · 9.16 KB
/
app.R
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# created by: Anh Le
# edited by:
# date: Mar 20 2020
"This script is the main file that creates a Dash app for Group 4 project on the pollutants dataset.
Usage: app.R
"
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
library(dashTable)
library(tidyverse)
library(plotly)
library(gapminder)
# load in the functions that make graphs
source('Scripts/dash_functions.R')
######################################################
# > Load data
data <- readr::read_csv(here::here("Data","clean_aq.csv"))
#Aggregate Daily Average
airq_daily <- data %>%
group_by(Date) %>%
summarise_all(list(mean), na.rm = TRUE)
# set up column for day of the week
newdata <- data %>%
mutate(DOTW = factor(case_when(weekdays(Date) == 'Monday' ~ 'Monday',
weekdays(Date) == 'Tuesday' ~ 'Tuesday',
weekdays(Date) == 'Wednesday' ~ 'Wednesday',
weekdays(Date) == 'Thursday' ~ 'Thursday',
weekdays(Date) == 'Friday' ~ 'Friday',
weekdays(Date) == 'Saturday' ~ 'Saturday',
weekdays(Date) == 'Sunday' ~ 'Sunday'),
levels = c("Monday" , "Tuesday","Wednesday", "Thursday","Friday", "Saturday", "Sunday")))
######################################################
# 2. Assign components to variables ----
# >> Heading ----
title <- htmlH1('Air quality and weather explorer')
intro_text <- dccMarkdown('The adverse affects of air pollution on health are well documented and air pollution can
lead to a large range of diseases and increased morbidity and mortality. Adverse health impacts include,
but are not limited to, lung cancer risk, respiritory infections, allergic disease and asthma.
These health risks can affect a large proportion of the population as many different groups are vulnerable
to the effects of air pollution including infants, children, the elderly, people with impaired immune systems,
and people who work or are physically active outdoors.
Because of the many, and severe, impacts of air quality, it is important to understand patterns in the data.
We have a dataset of air quality observations as well as temperature and humidity data which we will use to gain
understanding of the patterns and impacts of weather on air quality.
For this reason our research question is: What is the affect of temperature and humidity on the concentration
of air pollutants, such as benzene, titania, and tin oxide?')
annotation <- dccMarkdown('Here you can examine the effect of temperature and humidity on air pollutants.
Choose the **Time Trend** tab to display Plot 1 & 2. Choose the **Weather Indexes** tab to display Plot 3.
**Plot 1** shows the daily concentration of pollutants over the year of which the data was collected.
You can zoom in on certain time period by using the date slider below the graph. Hover the mouse over the graph
to see the detailed information of each data point, including the date recorded and the concentration value.
**Plot 2** shows the daily distribution of pollutants concentration by day of the week.
**Plot 3** shows the relationship of the pollutant
concentrations with different weather indexes.
Both the pollutant and weather indexes can be selected in a dropdown on the
left hand side of the page.
[The data source is the UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/Air+Quality)')
# >> Dropdown component for Pollutants ----
# Storing the labels/values as a tibble means we can use this both
# to create the dropdown and convert colnames -> labels when plotting
yaxisKey <- tibble(label = c("Carbon monoxide",
"Tin oxide",
"Hydrocarbons",
"Benzene",
"Titania",
"NOx",
"Tungsten oxide (NOx)",
"NO2",
"Tungsten oxide (NO2)",
"Indium oxide"),
value = c("CO", "Tin_oxide", "Hydro_carbons", "Benzene",
"Titania", "NOx", "Tungsten_oxide_NOx", "NO2",
"Tungsten_oxide_NO2", "Indium_oxide"))
#Create the dropdown
yaxisDropdown <- dccDropdown(
id = "y-axis",
options = map(
1:nrow(yaxisKey), function(i){
list(label=yaxisKey$label[i], value=yaxisKey$value[i])
}),
value = "Benzene"
)
# >> Dropdown component for Weather:
# Storing the labels/values as a tibble means we can use this both
# to create the dropdown and convert colnames -> labels when plotting
weatherKey <- tibble(label = c("Temperature (C)",
"Absolute Humidity (g/m3)", "Relative Humidity (%)"),
value = c("Temp", "AH", "RH"))
#Create the dropdown
weatherDropdown <- dccDropdown(
id = "weather",
options = map(
1:nrow(weatherKey), function(i){
list(label=weatherKey$label[i], value=weatherKey$value[i])
}),
value = "Temp"
)
# >> Graphs
graph1 <- dccGraph(
id = 'graph1',
figure=plot_aq_w_time() # gets initial data using argument defaults
)
dist_graph <- dccGraph(
id = 'dist',
figure=dist_plot()
)
aq_wx <- dccGraph(
id = 'aqwx',
figure=plot_aq_w_wx()
)
# >> Tabs
tabs_styles = list(
'height'= '66px'
)
tab_style = list(
'borderBottom'= '1px solid #d6d6d6',
'padding'= '10px',
'fontWeight'= 'bold',
'fontFamily' = 'Tahoma'
)
tab_selected_style = list(
'borderTop'= '1px solid #d6d6d6',
'borderBottom'= '1px solid #d6d6d6',
'backgroundColor'= '#2C3E50',
'color'= 'white',
'padding'= '6px'
)
tabs <- dccTabs(id="tabs", value='tab-1', children=list(
dccTab(label='Time Trend', value='tab-1', style = tab_style, selected_style = tab_selected_style),
dccTab(label='Weather Indexes', value='tab-2', style = tab_style, selected_style = tab_selected_style)
), style = tab_style)
######################################################
# 3. Create instance of a Dash App ----
app <- Dash$new(external_stylesheets = "https://codepen.io/chriddyp/pen/bWLwgP.css")
######################################################
# 4. Specify App layout ----
# layout with side bar etc.
div_header <- htmlDiv(
list(
title,
intro_text
)
)
div_sidebar <- htmlDiv(
list(
#selection components
tabs,
htmlBr(),
htmlLabel('Select Pollutant:'),
yaxisDropdown,
htmlLabel('Select Weather Index:'),
weatherDropdown,
htmlBr(),
annotation
), style= list('flex-basis' = '27%')
)
div_space <- htmlDiv(
list(
htmlIframe(height=70, width=10, style=list(borderWidth = 0))
), style= list('flex-basis' = '3%')
)
div_main1 <- htmlDiv(
list(htmlBr(),
graph1,
dist_graph,
htmlBr()
), style= list('flex-basis' = '70%')
)
div_main2 <- htmlDiv(
list(htmlBr(),
htmlBr(),
aq_wx,
htmlBr()
), style= list('flex-basis' = '70%')
)
# specify layout:
app$layout(
htmlDiv(
list(
div_header
), style = list( backgroundColor = '#2C3E50',
textAlign = 'center',
color = 'white',
margin = 5,
marginTop = 0)
),
htmlDiv(
list(
div_sidebar,
div_space,
htmlDiv(id='tabs-content')
), style = list('display' = 'flex', 'background-color' = 'white'),
)
)
app$callback(output('tabs-content', 'children'),
params = list(input('tabs', 'value')),
function(tab){
if(tab == 'tab-1'){
return(div_main1)
}
else if(tab == 'tab-2'){
return(div_main2)
}
}
)
######################################################
# 5. App Callbacks ----
# graph1
app$callback(
#update figure of plot_aq_w_time
output=list(id = 'graph1', property='figure'),
#based on values of date, y-axis components
params=list(input(id = 'y-axis', property='value')),
#this translates your list of params into function arguments
function(yaxis) {
plot_aq_w_time(yaxis)
})
# graph2
app$callback(
#update distribution plot
output=list(id = 'dist', property='figure'),
#based on values of date, y-axis components
params=list(input(id = 'y-axis', property='value')),
#this translates your list of params into function arguments
function(yaxis) {
dist_plot(yaxis)
})
# plot 3
app$callback(
#update figure of plot_aq_w_wx
output=list(id = 'aqwx', property='figure'),
#based on values of date, y-axis components
params=list(input(id = 'y-axis', property='value'),
input(id = 'weather', property = 'value')),
#this translates your list of params into function arguments
function(yaxis, weather) {
plot_aq_w_wx(yaxis, weather)
})
######################################################
# 6. Update Plot ----
######################################################
# 7. Run app ----
app$run_server(host = '0.0.0.0', port = Sys.getenv('PORT', 8050))
# command to add dash app in Rstudio viewer:
# rstudioapi::viewer("http://127.0.0.1:8050")