-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
411 lines (344 loc) · 15.3 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#
# OSU CS361: Intro to Software Development 1
# Individual Project
# Winter 2022
#
# Author: Maggie Liu
# Version: 1.3.0
# Description: A web app that allows a user to choose a city, and then takes
# air quality data for that city and displays a historical
# trend. Users can add up to 3 cities to the same trend.
#
library(shiny)
library(shiny.router)
library(ggplot2)
library(reshape2)
library(leaflet)
library(reticulate)
library(Rcpp)
subprocess <- reticulate::import("subprocess")
#
# --- UI PAGES -----------------------------------------------------------------
#
# Search page
search_page <- div(
headerPanel("Choose a city from the map below"),
mainPanel(
# Instruction text
h5("Choose a pre-selected city from the map, or enter the name or ZIP
code of a US city of your choice to see air quality trends from that
area. You can trend up to three cities."),
br(),
# Help tips depend on whether this is a new search or a city addition
div(style ="font-size:10pt;",
"TIP: Not every city has data available",
conditionalPanel(
condition = "(input.add_city - input.back_to_trend) % 2 != 0",
style ="font-size:10pt;",
"TIP: Comparing more than 2 cities at once may make trends
difficult to read.")
),
br(),
# Map with pre-selected cities to choose from
leafletOutput('usa_map', width = "100%"),
br(),
br(),
# Submission form for text entry
fluidRow(
column(width = 6,
textInput("text_location",
"Or, enter your own city / 5-digit zip code:")),
column(2, style = "margin-top:30px;",
actionButton("search",
label = "Submit",
icon = NULL,
width = NULL)),
column(2, style = "margin-top:30px;",
conditionalPanel(condition = "(input.add_city -
input.back_to_trend) % 2 != 0",
actionButton("back_to_trend",
label = "Go Back",
icon = NULL,
width = NULL)))
)
)
)
# Trend page
dash_page <- div(
titlePanel("Trends"),
# Sidebar with a slider for chart axes
sidebarLayout(
sidebarPanel(
helpText("Move sliders to change the ranges on the graph"),
br(),
sliderInput("time_slide", "Time",
min = as.Date("2018-12-31"),
max = as.Date("2022-03-01"),
value = c(as.Date("2018-12-31"), as.Date("2022-03-01")),
timeFormat = "%F"),
sliderInput("aq_slide", "PM2.5",
min = 0,
max = 300,
value = c(0, 300)),
actionButton("start_over", label = "New Search", icon = NULL),
actionButton("add_city", label = "Add City", icon = NULL)
),
# Display trend and text descriptions
mainPanel(
plotOutput("aq_plot"),
br(),
br(),
textOutput("avg_desc_1"),
textOutput("avg_desc_2"),
textOutput("avg_desc_3")
)
)
)
#
# --- ROUTER -------------------------------------------------------------------
#
router <- make_router(
route("/", search_page),
route("trends", dash_page)
)
#
# --- UI -----------------------------------------------------------------------
#
ui <- fluidPage(
theme = bslib::bs_theme(bootswatch = "yeti"),
title = "Historical Air Quality Trends",
fluid = TRUE,
router$ui
)
#
# --- SERVER MODULES -----------------------------------------------------------
#
main_server <- function(id, city, chosen, df) {
moduleServer(id, function(input, output, session) {
if (validate_input_server(id, city, chosen) == TRUE) {
df_new <- fetch_data_server(id, city)
if (!is.null(df_new)) {
# Populate the reactive df or merge the new data with the old
if(is.null(dim(df$vals))){
df$vals <- df_new
} else {
df$vals <- merge(x = df$vals, y = df_new, all = TRUE)
}
update_city_list_server(id, chosen, city)
# Prepare text output to be rendered in the main server
desc_1 <- description_server(id, df$vals[, chosen$city_1],
chosen$city_1)
cities <- c(chosen$city_1)
desc_2 <- ""
desc_3 <- ""
if (chosen$city_2 != "") {
cities <- c(chosen$city_1, chosen$city_2)
desc_2 <- description_server(id, df$vals[, chosen$city_2],
chosen$city_2)
}
if (chosen$city_3 != "") {
cities <- c(chosen$city_1, chosen$city_2, chosen$city_3)
desc_3 <- description_server(id, df$vals[, chosen$city_3],
chosen$city_3)
}
return(list(df$vals, cities, desc_1, desc_2, desc_3))
}
}
return(NULL)
})
}
# Check whether to make a request from the user input; if not, notify user
validate_input_server <- function(id, user_text, chosen) {
moduleServer(id, function(input, output, session) {
# Check if input box left blank
if (user_text == "") {
showModal(
modalDialog(title = "Error",
"Please enter a zip code or city name,
or choose a city from the map.",
footer = tagList(modalButton("Ok."))
)
)
} else if (chosen$city_3 != "") {
# Limit trend to 3 cities at once
showModal(
modalDialog(title = "Error",
"Only 3 cities can be trended at one time.",
footer = tagList(modalButton("Ok."))))
} else if (chosen$city_1 == user_text || chosen$city_2 == user_text) {
# Check if same city already requested
showModal(
modalDialog(title = "Error", "That city is already on the trend.
Please choose another.",
footer = tagList(modalButton("Ok."))))
} else {
return(TRUE)
}
return(FALSE)
})
}
# Perform search for air quality data. Source: AQICN database on dbnomics
fetch_data_server <- function(id, given_location) {
moduleServer(id, function(input, output, session) {
# Request data
writeLines(as.character(given_location), "historic_aqi.txt")
subprocess$run('py .\\get_historic_pm25.py')
# Check if the request was successful
confirm_request <- readLines("historic_aqi.txt")
if (confirm_request == "Location not found.") {
showModal(modalDialog(title = "Sorry!",
"No data found for that location.
Please try another.",
footer = tagList(modalButton("Ok."))))
} else if (confirm_request == "Entry not recognized.") {
showModal(modalDialog(title = "Sorry!",
"Entry not recognized. Please check for typos.",
footer = tagList(modalButton("Ok."))))
} else {
# Load data from file into data frame
df <- read.csv("pm25py.csv")
df <- df[,2:3]
colnames(df) <- c('Date', as.character(confirm_request))
df$Date <- as.Date(df$Date)
df <- na.omit(df)
return(df)
}
return(NULL)
})
}
# Update the names of cities chosen in the reactive ledger
update_city_list_server <- function(id, chosen, city) {
moduleServer(id, function(input, output, session) {
if (chosen$city_1 == "") {
chosen$city_1 = city
} else if (chosen$city_2 == "") {
chosen$city_2 = city
} else {
chosen$city_3 = city
}
})
}
# Generate a plot for the given data frame
plot_server <- function(id, df, city_names, time_slide, aq_slide) {
moduleServer(id, function(input, output, session) {
melty_df <- melt(df, id = "Date")
places <- paste(city_names, collapse = ", ")
plot <- ggplot(melty_df, aes(Date, value)) +
geom_point(aes(color = variable, group = variable)) +
labs(x = "Date", y = "PM2.5",
title = paste("Air Quality in", places), color = "City") +
scale_x_date(date_labels = "%b-%Y") +
xlim(time_slide()) +
ylim(aq_slide())
return(plot)
})
}
# Uses teammate's microservice to take PM2.5 value and return text description.
# Breakpoints from: https://aqicn.org/faq/2013-09-09/revised-pm25-aqi-breakpoints/
description_server <- function(id, city_data, city) {
moduleServer(id, function(input, output, session) {
pm25_avg <- round(mean(city_data, na.rm = TRUE), digits = 0)
write(pm25_avg, "pm25_avg.txt")
subprocess$run('py .\\Weather_goodinel_mod.py')
avg_desc <- readLines("response.txt")
return(paste("The average air quality for", city,"was",
pm25_avg,"μg/m^3. This is", avg_desc,".")
)
})
}
#
# --- SERVER -------------------------------------------------------------------
#
server <- function(input, output, session) {
router$server(input, output, session)
thematic::thematic_shiny()
# Interactive map for input pages with markers for default locations
city_names <- c("Seattle", "Los Angeles", "Chicago", "Houston", "Boston")
default_lat <- c(47.606209, 34.052235, 41.878113, 29.760427, 42.3601)
default_lon <- c(-122.332069, -118.243683, -87.629799, -95.369804, -71.0589)
output$usa_map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(-94, 42, zoom = 3) %>%
addCircleMarkers(lng = default_lon, lat = default_lat,
layerId = city_names)
})
# Keep track of chosen cities for this session
chosen <- reactiveValues(city_1 = "", city_2 = "", city_3 = "")
# Keep track of data loaded for this session
df <- reactiveValues(vals = NULL)
#
# --- EVENT HANDLERS -------------------------------------
#
# Gather and render data from map click
observeEvent(input$usa_map_marker_click, {
id <- "usa_map_marker_click"
results <- main_server(id, input$usa_map_marker_click$id, chosen, df)
if (!is.null(results)) {
df$vals <- results[[1]]
# Associate plot and description objects with an output to render
output$avg_desc_1 <- renderText(results[[3]][1])
output$avg_desc_2 <- renderText(results[[4]][1])
output$avg_desc_3 <- renderText(results[[5]][1])
output$aq_plot <- renderPlot({plot_server(id,
df$vals,
results[[2]],
reactive(input$time_slide),
reactive(input$aq_slide))},
res = 96)
change_page("trends")
}
})
# Gather and render data from user text input
observeEvent(input$search, {
id <- "search"
results <- main_server(id, input$text_location, chosen, df)
if (!is.null(results)) {
df$vals <- results[[1]]
# Associate plot and description objects with an output to render
output$avg_desc_1 <- renderText(results[[3]][1])
output$avg_desc_2 <- renderText(results[[4]][1])
output$avg_desc_3 <- renderText(results[[5]][1])
output$aq_plot <- renderPlot({plot_server(id,
df$vals,
results[[2]],
reactive(input$time_slide),
reactive(input$aq_slide))},
res = 96)
change_page("trends")
}
})
# Start over with new search, but confirm choice first
observeEvent(input$start_over, {
showModal(
modalDialog(title = "Caution",
"Starting a new search will clear all existing data.",
footer = tagList(modalButton("Cancel"),
actionButton("confirm_new", "Confirm new search")))
)
})
# Clear values and change page if new search confirmed
observeEvent(input$confirm_new, {
updateTextInput(session, "text_location", value = "")
chosen$city_1 <- ""
chosen$city_2 <- ""
chosen$city_3 <- ""
df$vals <- NULL
updateSliderInput(session, "time_slide",
value = c(as.Date("2018-12-31"),
as.Date("2022-03-01")))
updateSliderInput(session, "aq_slide", value = c(0, 300))
change_page("/")
removeModal()
})
# Go to the search page
observeEvent(input$add_city, {
updateTextInput(session, "text_location", value = "")
change_page("/")
})
# Return to trend page
observeEvent(input$back_to_trend, {
change_page("trends")
})
}
shinyApp(ui = ui, server = server)