-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
553 lines (412 loc) · 19.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
library(readxl)
library(tidyverse)
library(shiny)
library(plotly)
library(bslib)
library(scales)
library(RColorBrewer)
library(shinydashboard)
library(DT)
# ---------- Preprocessing required is in the script data_processing ------------
# The output of this script is the combined_historic_and_projected csv dataset
combined_historic_and_projected <- read.csv("data/comparative_to_baseline_test.csv") |> # just changed this line here
mutate(year = as.Date(year, format = "%Y")) |>
select(-"X")
list_of_pollutants_and_units <- read.csv("data/list_of_pollutants.csv") |>
select(-"X") |>
mutate(pollutant = ifelse(Pollutant == "Total 1-4", "Total PAHs", Pollutant))
totals <- read.csv("data/totals.csv") |>
mutate(year = as.Date(year, format = "%Y")) |>
select(-"X") |>
pivot_longer(cols = c(emission, emissions_relative_to_baseline), names_to = "emission_type", values_to = "emission_long")
ghg_data <- read.csv("data/ghg_data.csv") |>
mutate(year = as.Date(paste0(year, "-01-01"), format = "%Y-%m-%d"))
# Air Pollution Colours....
n_colours <- length(unique(combined_historic_and_projected$source_description))
set.seed(123)
source_colour_mappings <- data.frame(
source_description = unique(combined_historic_and_projected$source_description),
colour = sample(colorRampPalette(brewer.pal(9, "Paired"))(n_colours), n_colours) # Sample makes the colours random rather than in order
)
colour_mappings <- setNames(source_colour_mappings$colour, source_colour_mappings$source_description)
# Greenhouse gas colours....
n_colours_ghg <- length(unique(interaction(ghg_data$Activity, ghg_data$Source)))
source_colour_mappings_ghg <- data.frame(
Source = unique(interaction(ghg_data$Activity, ghg_data$Source)),
colour = sample(colorRampPalette(brewer.pal(9, "Paired"))(n_colours_ghg), n_colours_ghg) # Sample makes the colours random rather than in order
)
colour_mappings_ghg <- setNames(source_colour_mappings_ghg$colour, source_colour_mappings_ghg$Source)
# ----- The user interface ------------
ui <- dashboardPage(
skin = "blue",
dashboardHeader(title = "NAEI Visualiser"),
dashboardSidebar(
sidebarMenu(
menuItem(" Air Pollutants", icon = icon("head-side-cough"),
menuSubItem("Totals", tabName = "air_pollutants_totals", icon = icon("chart-line")),
menuSubItem("By Source", tabName = "air_pollutants_by_source", icon = icon("layer-group"))),
menuItem(" Greenhouse Gases", icon = icon("cloud"),
menuSubItem("Totals", tabName = "ghg_totals", icon = icon("chart-line")),
menuSubItem("By Source", tabName = "ghg_by_source", icon = icon("layer-group")))
)),
dashboardBody(
tags$head(
tags$link(
href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap",
rel = "stylesheet"
),
tags$style(HTML("
body {
font-family: 'Open Sans', sans-serif;
font-size: 14px;
line-height: 1.6;
}
.sidebar-menu > li > a {
font-weight: 400;
}
.skin-blue .main-header .navbar {
background-color: #C3E1E3;
}
.skin-blue .main-sidebar {
background-color: #383F51;
transition: background-color 0.3s, color 0.3s;
}
.skin-blue .sidebar-menu > li.active > a,
.skin-blue .sidebar-menu > li:hover > a {
background-color: #ff69b4;
color: #FFFFFF;
}
.skin-blue .sidebar-menu > li > a {
color: #FFFFFF;
border-radius: 4px;
}
.skin-blue .sidebar-menu > li > a:hover {
background-color: #ff69b4;
color: #FFFFFF;
transition: background-color 0.3s, color 0.3s;
}
.content-wrapper {
background-color: #ffffff;
}
.table-striped-green tbody tr:nth-child(odd) {
background-color: #ACC3B8;
}
.table-striped-green tbody tr:nth-child(even) {
background-color: #f8f9fa;
}
.skin-blue .main-header .logo {
text-overflow: ellipsis; /* Show ellipsis for overflow text */
background-color: #C3E1E3; /* Correct background-color */
color: #000000; /* Ensure title text is visible */
font-size: 24px; /* Adjust title size */
font-weight: 700; /* Bold text for emphasis */
padding-left: 10px; /* Add some spacing */
}
.box {
border-radius: 8px;
}
.box {
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
}
"))),
tabItems(
# First page - Air Pollutant Totals
tabItem(tabName = "air_pollutants_totals",
h2("Relative and absolute change in pollutants over time:"),
fluidRow(
column(4, selectInput("relative_or_absolute", "Change the y axis scale:", choices = c("Relative to baseline" = "emissions_relative_to_baseline", "Absolute" = "emission")))
),
fluidRow(column(8, plotlyOutput("totals_plot")))
),
# Second page - Air Pollutants By Source
tabItem(tabName = "air_pollutants_by_source",
h2("Air Pollutants By Source:"),
# Top-level layout
fluidRow(
# Sidebar with inputs and table
column(
width = 4,
wellPanel(
p("Use the drop-down boxes below to select a pollutant, a NFR category, and the subcategories of interest."),
selectInput(
"selected_pollutant",
"Select Pollutants",
choices = unique(combined_historic_and_projected$pollutant),
multiple = TRUE
),
selectInput(
"selected_source",
"Select Source",
choices = unique(combined_historic_and_projected$source_description),
multiple = TRUE
),
downloadButton("source_data_download", label = "Download this data")
),
br(), # Add spacing between the sidebar and the table
div(
style = "background-color: #F9F9F9; padding: 15px; border-radius: 10px; box-shadow: 0px 2px 5px rgba(0,0,0,0.1);",
h3("2022 Top Pollution Sources:"),
dataTableOutput("top_sources")
)),
# Main panel for graph
column(
width = 8,
h3("Trends Over Time:"),
plotlyOutput("line_graphs_one_category")
)
)
),
# Third page - Greenhouse Gases Totals -- IN DeVELOPMENT
tabItem(tabName = "ghg_totals",
h2("Relative and absolute change in greenhouse gases over time:"),
h3("In development...."),
fluidRow(
column(4, selectInput("relative_or_absolute", "Change the y axis scale:", choices = c("Relative to baseline" = "emissions_relative_to_baseline", "Absolute" = "emission")))
),
fluidRow(column(8, plotlyOutput("totals_plot_ghg")))
),
# Fourth page - Greenhouse Gases by Source
tabItem(tabName = "ghg_by_source",
h2("Greenhouse Gases By Source:"),
# Main layout
fluidRow(
# Sidebar for GHG inputs and table
column(
width = 4,
wellPanel(
p("Use the drop-down boxes below to select a greenhouse gas, a NFR category, and the subcategories of interest."),
selectInput(
"selected_ghg",
"Select Greenhouse Gases",
choices = unique(ghg_data$greenhouse_gas),
multiple = TRUE
),
selectInput(
"selected_source_ghg",
"Select Source",
choices = unique(ghg_data$Source),
multiple = TRUE
),
downloadButton("source_data_download_ghg", label = "Download this data")
),
br(),
div(
style = "background-color: #f9f9f9; padding: 15px; border-radius: 5px; box-shadow: 0px 2px 5px rgba(0,0,0,0.1);",
h3("2022 Top GHG Sources:"),
dataTableOutput("top_sources_ghg")
)
),
# Main panel for GHG graph
column(
width = 8,
h3("Trends Over Time:"),
plotlyOutput("line_graphs_one_category_ghg")
)
)
)
)
))
# ---- And the server function ------
server <- function(input, output, session){
selected_data <- reactive(combined_historic_and_projected |> # Test to see what one of these looks like
filter(pollutant %in% input$selected_pollutant) |>
filter(NFR_mid != "NA")
)
totals_data <- reactive({
totals %>%
filter(emission_type == input$relative_or_absolute)
})
selected_source <- reactive(combined_historic_and_projected |> # Test to see what one of these looks like
filter(pollutant %in% input$selected_pollutant) |>
filter(source_description %in% input$selected_source)
)
top_sources_table <- reactive({
req(input$selected_pollutant)
filter(combined_historic_and_projected, pollutant %in% input$selected_pollutant) |>
mutate(year = substring(as.character(year), 1,4), emission = round(emission,2) ) |>
filter(year == "2022") |>
group_by(pollutant) |>
arrange(desc(emission))
})
output$top_sources <- DT::renderDT({
top_sources_table() |>
select("NFR_code", "pollutant", "emission", "source_description") |>
rename("NFR Code" = NFR_code, "Pollutant" = "pollutant", "Emission (variable units)" = "emission", "Source" = "source_description")
},
options = list(
scrollX = TRUE,
pageLength = 5, # Number of rows per page
dom = 'Bfrtip', # Add buttons for export and search
style = "bootstrap4" # Modern styling
))
y_axis_label <- reactive({
if (input$relative_or_absolute == "emission") {
return("Emissions (kilotonnes)")
} else {
return("Emissions (%, relative to baseline)")
}
})
output$totals_plot <- renderPlotly({
ggplotly(
ggplot(totals_data()) +
geom_point(aes(x = year, y = emission_long, colour = pollutant, shape = data_source,
text = paste(
"Year:", format(year, "%Y"), "<br>",
"Emission:", round(emission_long, 2), "<br>",
"Pollutant:", pollutant, "<br>",
"Data Source:", data_source
)), show.legend = FALSE) +
geom_line(aes(x = year, y = emission_long, colour = pollutant, linetype = data_source), show.legend = FALSE) +
scale_color_brewer(name = "Pollutant", palette = "Dark2") +
scale_x_date(name = "Year", limits = as.Date(c("1970-01-01", "2050-01-01")), breaks = seq(as.Date("1970-01-01"), as.Date("2050-01-01"), by = "10 years"), labels = date_format("%Y")) +
scale_y_continuous(name =
y_axis_label(), limits = c(0, NA), expand = c(0,0)) +
theme_classic() +
theme(panel.grid.major.y = element_line(colour = "lightgrey"),
strip.background = element_rect(colour = "white"),
strip.placement = "bottom") +
theme(legend.position = "right") +
guides(colour = guide_legend(override.aes = list(shape = NA, linetype = NA))) +
theme(axis.title = element_text(size = 20)),
height = 600,
width = 1000,
tooltip = "text"
)
})
# NEED TO CHANGE BECAUSE CAN HAVE MORE THAN ONE POLLUTANT SELECTED AND HENCE MORE THAN ONE UNIT!!!
y_axis_label_correct_units <- reactive({
# Filter and create a named vector of units for each selected pollutant
units_vector <- list_of_pollutants_and_units |>
filter(Pollutant %in% input$selected_pollutant) |> # Get the pollutants selected, allowing for the fact there can be more than one
select(Pollutant, Units) |>
deframe() # Converts to a named vector where Pollutant is the name and Units is the value
return(units_vector)
})
output$line_graphs_one_category <- renderPlotly({
req(input$selected_source)
req(input$selected_pollutant)
# Check if there is data available in selected_source
data <- selected_source()
# Use validate and need to show a message if no data is available...
validate(
need(nrow(data) > 0 & !all(is.na(data$emission)), "No data available for the selected source and pollutant. Please select an alternative"))
pollutant_labeller <- function(variable, value) {
units <- y_axis_label_correct_units() # Takes the unit vectors from the y_axis_label function call
paste0(value, " (", units[value], ")") # Returns the label values
}
ggplotly(
ggplot(selected_source()) +
geom_line(aes(x=year, y = emission, colour = source_description, linetype = status)) +
geom_point(aes(x=year, y = emission, colour = source_description, shape = status,
text = paste(
"Year:", format(year, "%Y"), "<br>",
"Emissions:", round(emission, 2), y_axis_label_correct_units(), "<br>",
"Pollution source:", source_description, "<br>",
"Data Source:", status, "<br>",
"NFR Code:", NFR_code
))) +
scale_y_continuous(name = "Emissions", limits = c(0,NA)) +
facet_wrap(~pollutant , scales = "free_y", ncol = 2, labeller = pollutant_labeller) +
scale_x_date(name = "Year", limits = as.Date(c("1990-01-01", "2050-01-01")), breaks = seq(as.Date("1990-01-01"), as.Date("2050-01-01"), by = "10 years"), labels = date_format("%Y")) +
scale_colour_manual(values = colour_mappings) +
theme_classic() +
theme(panel.grid.major.y = element_line(colour = "lightgrey"),
strip.background = element_rect(colour = "white"),
strip.placement = "bottom") +
theme(legend.position = "none") +
theme(axis.title = element_text(size = 20),
plot.margin = margin(0.25,0.25,0.25,0.55, unit = "cm"),
strip.text = element_text(size = 10)), height = 600, width = 1000, tooltip = "text")
})
# If the user wants to download that data.....
output$source_data_download <- downloadHandler(
filename = function() {
paste0(input$selected_pollutant,"_", input$selected_source,".csv")
},
content = function(file) {
download_content <- selected_source() |>
select(NFR_wide.x:status) |>
rename(NFR_decsription = NFR_wide.x) |>
left_join(list_of_pollutants_and_units, by = "pollutant")
write.csv(download_content, file, row.names = FALSE)
}
)
# Now the GHG section.....
selected_data_ghg <- reactive(ghg_data |> # Test to see what one of these looks like
filter(greenhouse_gas %in% input$selected_ghg) |>
filter(NFR_mid != "NA")
)
selected_source_ghg <- reactive(ghg_data |> # Test to see what one of these looks like
filter(greenhouse_gas %in% input$selected_ghg) |>
filter(Source %in% input$selected_source_ghg)
)
output$line_graphs_one_category_ghg <- renderPlotly({
req(input$selected_source_ghg)
req(input$selected_ghg)
# Check if there is data available in selected_source
data <- selected_source_ghg()
# Use validate and need to show a message if no data is available...
validate(
need(nrow(data) > 0 & !all(is.na(data$emission)), "No data available for the selected source and greenhouse gas. Please select an alternative"))
ggplotly(
ggplot(selected_source_ghg()) +
geom_line(aes(x=year, y = emission, colour = interaction(Activity, Source), group = source_description)) +
geom_point(aes(x=year, y = emission, colour = interaction(Activity, Source), group = source_description,
text = paste(
"Year:", format(year, "%Y"), "<br>",
"Emissions:", round(emission, 2), `Units`, "<br>",
"Pollution source:", Source, "<br>",
"Activity:", Activity, "<br>",
"NFR Code:", NFR_code
))) +
scale_y_continuous(name = "Emissions", limits = c(NA,NA)) +
facet_wrap(~greenhouse_gas , scales = "free_y", ncol = 2) +
scale_x_date(name = "Year", limits = as.Date(c("1990-01-01", "2025-01-01")), breaks = seq(as.Date("1990-01-01"), as.Date("2025-01-01"), by = "5 years"), labels = date_format("%Y")) +
theme_classic() +
scale_colour_manual(values = colour_mappings_ghg) +
theme(panel.grid.major.y = element_line(colour = "lightgrey"),
strip.background = element_rect(colour = "white"),
strip.placement = "bottom") +
theme(legend.position = "none") +
theme(axis.title = element_text(size = 20),
strip.text = element_text(size = 10)), height = 600, width = 1000, tooltip = "text")
})
top_sources_ghg_table <- reactive({
req(input$selected_ghg)
filter(ghg_data, greenhouse_gas %in% input$selected_ghg) |>
mutate(year = substring(as.character(year), 1,4) ) |>
filter(year == "2022") |>
group_by(greenhouse_gas) |>
arrange(desc(emission)) #|>
# do(head(., n=25)) |>
# arrange(desc(emission))
})
output$top_sources_ghg <- DT::renderDT({
top_sources_ghg_table() |>
select("NFR_code", "greenhouse_gas", "emission", "Units", "Source") |>
rename("NFR Code" = NFR_code, "Greenhouse Gas" = greenhouse_gas,"Emission" = emission)
},
options = list(
scrollX = TRUE,
pageLength = 5, # Number of rows per page
dom = 'Bfrtip', # Add buttons for export and search
style = "bootstrap4" # Modern styling
),
class = "table table-striped table-bordered"
)
output$source_data_download_ghg <- downloadHandler(
filename = function() {
paste0(input$selected_ghg,"_", input$selected_source,".csv")
},
content = function(file) {
download_content <- selected_source_ghg() |>
select(greenhouse_gas:emission)
write.csv(download_content, file, row.names = FALSE)
}
)
session$onSessionEnded(function() {
stopApp()
})
}
# Run the application
shinyApp(ui = ui, server = server)