-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.R
More file actions
1250 lines (1039 loc) · 46.1 KB
/
app.R
File metadata and controls
1250 lines (1039 loc) · 46.1 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Define path of the project
# path <- "D:/GITHUB/ECOMIX_Explorer/ECOMIX-Explorer/"
path <- here::here()
# Load Packages
library(leaflet)
library(shiny)
library(bslib)
library(sf)
library(dplyr)
library(ggsci)
library(DT)
library(ggplot2)
library(here)
library(arrow)
# Decimal numbers
options(scipen = 999)
safe_open_dataset <- function(dataset_path) {
open_dataset(
dataset_path,
factory_options = list(
exclude_invalid_files = TRUE,
selector_ignore_prefixes = c(".", "_")
)
)
}
## Load required datasets
# Study area
catchment_shp <- read_sf(dsn = here("data"), layer = "Wharfe_catchments_wgs")
# HYPE Subbasins (modelling units)
subbasin_shp <- read_sf(dsn = here("data"), layer = "Wharfe_subbasins_wgs")
# Table with climate information
df_stats_climate <- read.csv(here("data/subbasin_climate.csv"))
df_stats_climate$subbasin <- as.numeric(gsub("X", "", df_stats_climate$subbasin))
# Table with subbasin statistics
df_stats_lc <- read.csv(here("data/subbasin_lc.csv"))
df_stats_lc$subbasin <- as.numeric(gsub("X", "", df_stats_lc$subbasin))
# Table with monthly model predictions
#df_climate_data <- read.csv(here("data/Dummy_Data_Climate.csv"))
#df_climate_data$scenario[df_climate_data$scenario == "baseline"] <- "Baseline"
#df_climate_data$scenario[df_climate_data$scenario == "Projection"] <- "SSP585"
#df_climate_data_long <- reshape2::melt(df_climate_data, id.vars = c("scenario", "subbasin", "month"))
# Table with daily Water temperature (used for dummy testing plot)
df_temp <- read.csv(here("data/Dummy_Data_TT2.csv"))
df_temp$date <- as.Date(df_temp$date)
# Updates
# Historical simulations at observation sites
db_name <- here("data/DB_Historical_Sim_Obs")
df_historical_observations <- safe_open_dataset(db_name) %>%
collect()
df_observed_subbasins <- df_historical_observations %>% group_by(subbasin, variable) %>% slice(1) %>%
dplyr::select(subbasin, variable)
df_historical_observations$date <- as.Date(df_historical_observations$date)
# HYPE Projections - read in during plot generation
#df_projections_year <- open_dataset(here("data/DB_Proj_Year")) %>% collect()
#df_projections_month <- open_dataset(here("data/DB_Proj_Month")) %>% collect()
#df_projections_percentile <- open_dataset(here("data/DB_Proj_Percentiles")) %>% collect()
#df_projections_percentile$period <- paste0(df_projections_percentile$start_year, "-", df_projections_percentile$end_year)
# Keep Arrow dataset handles open once to avoid repeated metadata scans in renderers.
ds_proj_forcing <- safe_open_dataset(here("data/DB_Proj_Forcing"))
ds_proj_year <- safe_open_dataset(here("data/DB_Proj_Year"))
ds_proj_month <- safe_open_dataset(here("data/DB_Proj_Month"))
ds_proj_percentiles <- safe_open_dataset(here("data/DB_Proj_Percentiles"))
# read map input
df_map_input <- read_parquet(here("data/Subbasin_Extremes.gz.parquet"))
df_map_input <- df_map_input %>% filter(prediction_percentile == 99.9,
ssp == "SSP585",
period == "2070-2080")
# Cache expensive tabular download extracts per variable to avoid repeatedly
# scanning large Arrow datasets when users toggle between options.
tabular_download_cache <- new.env(parent = emptyenv())
build_tabular_download <- function(dl_variable) {
if (dl_variable %in% c("precip", "temp")) {
dl_lookup <- c("precip" = "Precipitation", "temp" = "Temperature")
dl_variable_label <- unname(dl_lookup[dl_variable])
df_download <- ds_proj_forcing %>%
filter(variable == dl_variable_label, time_aggregation == "monthly") %>%
select(subbasin, ssp, period, month, variable, p50, unit) %>%
collect() %>%
rename("scenario" = ssp, "value" = p50)
} else {
hype_lookup <- c(
"runoff" = "discharge",
"soil_moisture" = "Soil moisture",
"water_temperature" = "water temperature",
"susp_sediments" = "Susp. Sediments",
"inorganic_nitrogen" = "Inorganic Nitrogen"
)
hype_variable_label <- unname(hype_lookup[dl_variable])
df_download <- ds_proj_month %>%
filter(
hype_variable == hype_variable_label,
prediction_percentile == "p50"
) %>%
select(subbasin, ssp, period, month, hype_variable, prediction_percentile, p50_ensemble, unit) %>%
collect() %>%
rename("scenario" = ssp, "value" = p50_ensemble)
}
df_download %>%
mutate("value" = round(value, 3)) %>%
dplyr::select(subbasin, scenario, period, month, value, unit)
}
get_tabular_download <- function(dl_variable) {
if (!exists(dl_variable, envir = tabular_download_cache, inherits = FALSE)) {
tabular_download_cache[[dl_variable]] <- build_tabular_download(dl_variable)
}
tabular_download_cache[[dl_variable]]
}
# Climate projections
#df_ukcp_climate <- open_dataset(here("data/DB_Proj_Forcing")) %>% collect()
## Definition of widgets
# Widget to select one or more scenarios
widget_scenario <- selectizeInput(
inputId = "scenario", # This is used in the server part as reactive element (i.e. input$variable)
label = "Choose multiple scenarios (press del to remove variable)",
choices =
c("Baseline", "SSP126", "SSP585"),
selected = "Baseline",
multiple = TRUE)
# Widget for card 1 - climate variable (drop down menu - only one selection)
widget_climate_variable <- selectInput(
inputId = "climate_variable", # This is used in the server part as reactive element (i.e. input$variable)
label = "Select climate variables",
choices =
c("Precipitation", "Temperature"),
selected = "Precipitation")
# Widget to select temporal resoluton
widget_climate_resolution <- selectInput(
inputId = "climate_resolution", # This is used in the server part as reactive element (i.e. input$variable)
label = NULL,
choices =
c("Monthly" = "monthly",
"Yearly" = "annual"),
selected = "Monthly")
# Widget to select time periods
widget_climate_period <- selectInput(
inputId = "climate_period", # This is used in the server part as reactive element (i.e. input$variable)
label = "Select the period(s)",
choices =
c("2000-2022", "2020-2029", "2030-2039", "2040-2049", "2050-2059", "2060-2069", "2070-2080"),
selected = "2000-2022",
multiple = TRUE)
# Widget for card 2 - observational variables
# this widget has no input yet, because the input depends on the selected subbasin
widget_observed_variable <- selectizeInput(
inputId = "observation_variable",
label = "Choose a variable",
choices = NULL, # placeholder
multiple = FALSE
)
## Widgets for card 3 - Tab 1
# HYPE output variable
widget_prediction_variable <- selectInput(
inputId = "prediction_variable", # This is used in the server part as reactive element (i.e. input$variable)
label = "Select a variable",
choices =
c("Discharge" = "discharge",
"Soil Moisture" = "Soil moisture",
"Water Temperature" = "water temperature",
"Susp. Sediments" = "Susp. Sediments",
"Inorganic Nitrogen" = "Inorganic Nitrogen"),
selected = "Discharge")
# Output conditions - i.e. Prediction percentile
widget_prediction_percentile <- selectInput(
inputId = "prediction_percentile", # This is used in the server part as reactive element (i.e. input$variable)
label = "Select the conditions(s)",
choices =
c("Low (10th percentile)" = "p10",
"Average (50th percentile)" = "p50",
"High (90th percentile)" = "p90"),
selected = "p50",
multiple = TRUE)
# Prediction period
widget_prediction_period <- selectInput(
inputId = "prediction_period", # This is used in the server part as reactive element (i.e. input$variable)
label = "Select the period(s)",
choices =
c("2020-2029", "2030-2039", "2040-2049", "2050-2059", "2060-2069", "2070-2080"),
selected = "2070-2080",
multiple = TRUE)
# Plot type (Absolute or relative change)
widget_plot_type <- selectInput(
inputId = "plot_type", # This is used in the server part as reactive element (i.e. input$variable)
label = "Select plot type",
choices =
c("Absolute", "Relative"),
selected = "Absolute")
widget_download_variable <- selectInput(
inputId = "dl_variable", # This is used in the server part as reactive element (i.e. input$variable)
label = "Choose a variable",
choices =
c("Precipitation" = "precip",
"Temperature" = "temp",
"Discharge" = "runoff",
"Soil Moisture" = "soil_moisture",
"Water Temperature" = "water_temperature",
"Susp. Sediments" = "susp_sediments",
"Inorganic Nitrogen" = "inorganic_nitrogen"),
selected = "Precipitation")
widget_download_data_type <- selectInput(
inputId = "dl_data_type",
label = "Data type",
choices = c("Tabular" = "tabular", "Spatial" = "spatial"),
selected = "tabular"
)
widget_download_spatial_layer <- selectInput(
inputId = "dl_spatial_layer",
label = "Spatial layer",
choices = c("Subbasins" = "subbasins", "Catchment" = "catchment"),
selected = "subbasins"
)
widget_download_format <- selectInput(
inputId = "dl_format",
label = "Download format",
choices = c("CSV" = "csv", "XLSX" = "xlsx", "Parquet" = "parquet"),
selected = "csv"
)
### 1. User Interface
ui <- page_navbar(
# General aesthetics
id = "main_nav",
title = "ECOMIX Explorer",
bg = "#A26BCDFF", # Background color of the navbar
inverse = TRUE, # This inverts the colors - looks nicer
tags$style(HTML("\n .de-summary-boxes .value-box,\n .de-summary-boxes .bslib-value-box {\n height: 78px !important;\n min-height: 78px !important;\n max-height: 78px !important;\n overflow: hidden;\n }\n .de-summary-boxes .value-box .card-body,\n .de-summary-boxes .bslib-value-box .card-body {\n padding-top: 0.45rem;\n padding-bottom: 0.45rem;\n }\n ")),
## Definition of the Tabs
# Panel 1: Map for subbasin selection
nav_panel(title = "Map",
fluid = TRUE,
# If map is not the full page enable this:
# Uses a custom style script
#div(class="outer",
# tags$head(
# # Include our custom CSS
# includeCSS("styles.css"), # File from: https://github.com/rstudio/shiny-examples/blob/main/063-superzip-example/styles.css
# ),
# # Add page content
# leafletOutput("basemap", width="100%", height="100%")
#
# # Add information panel
# absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
# draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
# width = 330, height = "auto",
# h2("ZIP explorer"),
# ),
# ),
# Add the map
leafletOutput("basemap", width="100%", height="100%"),
# Add a panel with of the selected subbasin.
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 80, left = "auto", right = 20, bottom = "auto",
width = 500, height = "auto", style = "background: white",
# Title
h2("Subbasin information"),
# Add summary statistics of the selected subbasin
uiOutput("selected_subbasin") # uiOutput to implement linebreaks
),
# Credits
tags$div(id="cite", 'Data compiled by Durham University (2026)'
)),
## Panel 2: Data explorer
nav_panel(title = "Data Explorer",
fluid = TRUE,
##Add local (nav_page) sidebar layout and content
# Define the elements in the sidebar
layout_sidebar(
sidebar = sidebar(
title = "Scenario selection",
position = "left",
helpText("Some instructions here"),
# Interactive widget that lets select (and deselect multiple scenarios)
widget_scenario
),
## Add the page content here
## Heading Widgets - General Information
div(
class = "de-summary-boxes",
layout_columns(
fill = FALSE,
value_box(
title = "Selected Subcatchment",
value = textOutput("text_subbasin"),
# showcase = bsicons::bs_icon("pin-map-fill")
),
value_box(
title = "Upstream Area",
value = textOutput("text_upstream_area"),
# showcase = bsicons::bs_icon("hexagon")
),
value_box(
title = "Average precipitation",
value = textOutput("text_precip"),
# showcase = bsicons::bs_icon("cloud-hail-fill")
),
value_box(
title = "Annual Temperature",
value = textOutput("text_maat"),
#showcase = bsicons::bs_icon("brightness-high-fill")
# showcase = bsicons::bs_icon("thermometer-half")
)
)
),
## Plot information
# Output of the Plot cards
layout_columns(
# Climate Plot
card(
full_screen = TRUE,
card_header("Climate"),
# Add a local widget to select the climate variable
layout_sidebar(
sidebar = sidebar(
#title = "Climate variable",
# Widget to select climate variable (drop down menu - only one selection)
widget_climate_variable,
widget_climate_resolution,
widget_climate_period
),
plotOutput("climate_plot")
),
),
# Plot to compare Simulations and Observations
card(
full_screen = TRUE,
card_header("Simulations vs Observations"),
layout_sidebar(
sidebar = sidebar(
#title = "Observed variable",
# Widget to select climate variable (drop down menu - only one selection)
widget_observed_variable
),
plotOutput("observation_plot")
),
),
# Projection plots - one card with multiple tabs
navset_card_pill(
full_screen = TRUE,
# First tab: Yearly output variables
nav_panel("Yearly",
#"Yearly projections",
layout_sidebar(
sidebar = sidebar(
#title = "HYPE variable",
# Widget to select the HYPE output variable (drop down menu - only one selection)
widget_prediction_variable,
widget_prediction_percentile,
widget_plot_type,
),
plotOutput("projections_yearly_plot"),
),
),
# Second tab: Monthly output variables
nav_panel("Monthly",
#"Monthly Projections",
layout_sidebar(
sidebar = sidebar(
#title = "HYPE variable",
# Widget to select the HYPE output variable (drop down menu - only one selection)
widget_prediction_variable,
widget_prediction_period,
widget_prediction_percentile,
widget_plot_type,
),
plotOutput("projections_monthly_plot"),
),
),
# Third tab: Cumulative frequency curves
nav_panel("Distributions ",
#"Cumulative Frequency Curves",
layout_sidebar(
sidebar = sidebar(
#title = "HYPE variable",
# Widget to select the HYPE output variable (drop down menu - only one selection)
widget_prediction_variable,
widget_prediction_period
),
plotOutput("projections_cfc_plot"),
),
),
),
# Define dimensions
col_widths = c(6, 6, 12),
row_heights = c(1, 2)
),
),
),
# Panel 3: Spatial mapping
nav_panel(title = "Spatial Datasets",
fluid = TRUE,
# Define the elements in the sidebar
layout_sidebar(
sidebar = sidebar(
title = "Data selection",
position = "left",
helpText("Some instructions here"),
# Interactive widget that lets select a hype output variable
widget_prediction_variable
),
# Define the output map
leafletOutput("prediction_map", width="100%", height="100%")
),
),
# Panel 4: Download of data (or tables)
nav_panel(title = "Data Downloader",
fluid = TRUE,
# Define the elements in the sidebar
layout_sidebar(
sidebar = sidebar(
title = "Data selection",
position = "left",
helpText("Some instructions here"),
# Interactive widget that lets select (and deselect multiple scenarios)
widget_download_variable,
widget_download_data_type,
widget_download_spatial_layer,
widget_download_format,
downloadButton("download_data", "Download Data")
),
# Define the output table
DT::dataTableOutput("data_table")
),
),
## PANEL 5: Food Web Dynamic Model (Embedded Julia Dash)
nav_panel(
title = "Food Web Dynamics",
fluid = TRUE,
tags$iframe(
src = "http://127.0.0.1:8050",
height = "700px",
width = "100%",
frameborder = "0",
style = "margin: 0;"
)
),
nav_spacer(),
## Navigation menu
nav_menu(
title = "Links",
align = "right",
nav_item(tags$a("Posit", href = "https://posit.co")),
nav_item(tags$a("Shiny", href = "https://shiny.posit.co"))
)
)
### 2. Server
## Interactive Map ##
server <- function(input, output, session) {
### NAVBAR 1 - MAP ###
## Function for selecting a subbasin
# use reactive values to store the id from observing the shape click
rv <- reactiveVal()
selected_climate <- reactive({
req(rv())
df_stats_climate %>% filter(subbasin == rv())
})
selected_lc <- reactive({
req(rv())
df_stats_lc %>% filter(subbasin == rv())
})
selected_historical <- reactive({
req(rv())
df_historical_observations %>% filter(subbasin == rv())
})
# Track clicks
observeEvent(input$basemap_shape_click, {
rv(input$basemap_shape_click$id)
})
# Open Data Explorer from map popup link and keep selected subbasin in sync.
observeEvent(input$open_data_explorer, {
rv(as.numeric(input$open_data_explorer))
bslib::nav_select("main_nav", selected = "Data Explorer", session = session)
})
## Reactive selection of observed variables
observation_choices <- reactive({
if (is.null(rv())) {
c("Please select a subbasin")
} else {
# Check if observations are available for the subbasin
df_tmp <- df_observed_subbasins %>% filter(subbasin == rv())
if (nrow(df_tmp) == 0) {
c("No Observations available")
} else{
df_tmp[['variable']]
}
}
})
observeEvent(observation_choices(), {
updateSelectizeInput(
session,
inputId = "observation_variable",
choices = observation_choices(),
selected = NULL
)
})
# Create the map (catchment part works)
output$basemap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -1.16, lat = 53.75, zoom = 8.5) %>%
addPolygons(data=subbasin_shp,
fill = T, # Has to be filled to get the hitmarker
fillOpacity = 0.01,
color = "black",
opacity = 0.5,
weight = 2,
popup = ~paste0(
"<strong>Subbasin id: </strong>", Id,
"<br><a href='#' onclick=\"Shiny.setInputValue('open_data_explorer', '",
Id,
"', {priority: 'event'}); return false;\">Open in Data Explorer</a>"
),
layerId = ~Id)
})
# Map information output
output$selected_subbasin <- renderUI({
# If no subbasin was selected
if (is.null(rv())) return ("Please select a subbasin by clicking on the map")
# If subbasin is selected
# Subset data
df_climate_tmp <- selected_climate()
df_stats_lc_tmp <- selected_lc()
HTML(paste("Selected polygon: ", rv(), "<br>",
"Upstream area: ", round(df_stats_lc_tmp$value[df_stats_lc_tmp$variable == "Upstream area"] / 1000000, 2), " km2 <br>",
"Annual Precipitation: ", round(df_climate_tmp$precip[1], 0), "mm <br>",
"Mean Annual Temperature: ", round(df_climate_tmp$maat[1], 2), " deg. C <br>",
sep = ""))
})
### NAVBAR 2 - PLOTS ###
## Heading Widgets - General Information
output$text_subbasin <- renderText({
if (is.null(rv())) return ("Please select a subbasin in the map tab")
df_climate_tmp <- selected_climate()
as.character(df_climate_tmp$subbasin[1])
})
output$text_upstream_area <- renderText({
if (is.null(rv())) return (" ")
df_stats_lc_tmp <- selected_lc()
paste(round(df_stats_lc_tmp$value[df_stats_lc_tmp$variable == "Upstream area"] / 1000000, 2), "km²")
})
output$text_precip <- renderText({
if (is.null(rv())) return (" ")
df_climate_tmp <- selected_climate()
paste(as.character(round(df_climate_tmp$precip[1]), 0), "mm")
})
output$text_maat <- renderText({
if (is.null(rv())) return (" ")
df_climate_tmp <- selected_climate()
paste(as.character(round(df_climate_tmp$maat[1]), 1), "°C")
})
## Plot 1: Climate
output$climate_plot <- renderPlot({
# Dont do anything if no subbasin was selected
if (is.null(rv())) return ("Please select a subbasin by clicking on the map")
sub_subbasin <- rv()
sub_climate_variable <- unique(input$climate_variable)
sub_climate_resolution <- input$climate_resolution[1]
sub_scenarios <- unique(input$scenario)
sub_periods <- unique(input$climate_period)
if ("Baseline" %in% sub_scenarios) {
sub_periods <- c("2000-2022", sub_periods)
}
# open data
df_plot <- ds_proj_forcing %>%
filter(
variable == sub_climate_variable,
subbasin %in% sub_subbasin,
ssp %in% sub_scenarios,
time_aggregation == sub_climate_resolution) %>%
collect()
# Data wrangling
if(sub_climate_resolution == "monthly") {
df_plot <- df_plot %>% filter(period %in% sub_periods) %>%
mutate("scenario" = paste0(ssp, " (", period, ")"))
df_plot$xaxis <- df_plot$month
xlab <- "Month"
}
if(sub_climate_resolution == "annual") {
df_plot$scenario <- df_plot$ssp
df_plot$xaxis <- df_plot$year
xlab <- "Year"
}
ylab <- paste0(sub_climate_resolution, " ", sub_climate_variable, " [", unique(df_plot$unit), "]")
# plotting
ggplot(df_plot, aes(x = xaxis, y = p50, color = scenario, fill = scenario)) +
geom_line() +
geom_ribbon(aes(ymin = p10, ymax = p90), alpha = 0.2, linewidth = 0.05)+
scale_color_jco()+
scale_fill_jco()+
scale_x_continuous(expand = c(0,0))+
scale_y_continuous(expand = c(0,0)) +
labs(x = xlab, y = ylab,
title = toupper(paste(sub_climate_resolution, sub_climate_variable))) +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
legend.title = element_blank(),
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
})
## Plot 2 - Observations
output$observation_plot <- renderPlot({
# Dont do anything if no subbasin was selected
if (is.null(rv())) return ("Please select a subbasin by clicking on the map")
# Filter subbasin
df_data <- selected_historical()
# Subset the variable based on widget
df_plot <- df_data[df_data$variable == input$observation_variable, ]
## Discharge plot
if (input$observation_variable == "discharge") {
# data wrangling
df_tmp <- df_plot %>% dplyr::select(-prediction_percentile, -variable, -sim_P10, -sim_P50, -sim_P90) %>%
rename("low" = obs_min, "med" = obs, "high" = obs_max) %>% mutate("type" = "Observation")
df_plot <- df_plot %>% dplyr::select(-prediction_percentile, -variable, -obs_min, -obs_max, -obs) %>%
rename("low" = sim_P10, "med" = sim_P50, "high" = sim_P90) %>% mutate("type" = "Simulation")
df_plot <- rbind(df_plot, df_tmp)
# plotting
ggplot(df_plot, aes(x = date, y = med, color = type, fill = type)) +
geom_line() +
geom_ribbon(aes(ymin = low, ymax = high), alpha = 0.5, linewidth = 0.05)+
scale_color_manual(values = c("#5B84B1FF", "#FC766AFF"))+
scale_fill_manual(values = c("#5B84B1FF", "#FC766AFF"))+
scale_x_date(expand = c(0,0))+
scale_y_continuous(limits = c(0, max(df_plot$high)*1.1), expand = c(0,0)) +
labs(x = "Year", y = "Discharge [m³/s]",
title = toupper(paste0(unique(df_plot$station_label), " (Station ", unique(df_plot$id_station), ")"))) +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
legend.title = element_blank(),
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
# Substance Plotting
} else{
# plotting
if (nrow(df_plot) == 0 || all(is.na(c(df_plot$sim_P90, df_plot$obs)))) {
return(NULL)
}
upper <- max(c(df_plot$sim_P90, df_plot$obs), na.rm = TRUE)
ylab <- paste0(unique(df_plot$variable), " [", unique(df_plot$unit), "]")
ggplot(df_plot, aes(x = date, y = sim_P50)) +
geom_line(color = "#FC766AFF") +
geom_ribbon(aes(ymin = sim_P10, ymax = sim_P90), fill = "#FC766AFF", alpha = 0.5, linewidth = 0.05)+
geom_point(aes(x = date, y = obs), shape = 4, color = "grey3", size = 2) +
scale_x_date(expand = c(0,0))+
scale_y_continuous(limits = c(0, upper*1.1), expand = c(0,0)) +
labs(x = "Year", y = ylab, title = toupper(paste0(unique(df_plot$station_label), " (Station ", unique(df_plot$id_station), ")"))) +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
legend.title = element_blank(),
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
}
})
## Plot 3: Yearly Projections
output$projections_yearly_plot <- renderPlot({
# Dont do anything if no subbasin was selected
if (is.null(rv())) return ("Please select a subbasin by clicking on the map")
# Filter dataset
#df_plot <- df_projections_year %>% filter(subbasin == rv())
# Subset the dataset based on widget inputs
sub_subbasin <- rv()
sub_variable <- input$prediction_variable[1]
sub_scenarios <- unique(input$scenario)
sub_percentiles <- unique(input$prediction_percentile)
sub_plot_type <- unique(input$plot_type)
# open database
df_projections_year <- ds_proj_year %>%
filter(subbasin %in% sub_subbasin,
hype_variable %in% sub_variable,
ssp %in% c("Baseline",sub_scenarios),
prediction_percentile %in% sub_percentiles) %>%
collect()
## Plotting of Absolute Projections
# Absolute change
if (sub_plot_type == "Absolute") {
df_plot <- df_projections_year %>% filter(ssp %in% sub_scenarios)
# Data wrangling
df_plot <- df_plot[!(df_plot$ssp == "Baseline" & df_plot$year > 2020), ] # filter overlap
df_plot$percentile_label <- factor(df_plot$prediction_percentile, levels = c("p10", "p50", "p90"),
labels = c("Low (10th percentile)", "Average (50th percentile)", "High (90th percentile)"))
ylab <- paste0(sub_scenarios, " [", unique(df_plot$unit), "]")
ggplot(df_plot, aes(x = year, y = p50_ensemble, color = ssp, fill = ssp, linetype = percentile_label)) +
geom_line() +
geom_ribbon(aes(ymin = p10_ensemble, ymax = p90_ensemble), alpha = 0.2, linewidth = 0.05)+
scale_x_continuous(expand = c(0,0)) +
scale_color_jco()+
scale_fill_jco()+
labs(x = "Year", y = ylab, title = toupper(sub_variable), linetype= "Frequency", fill = "Scenario", color = "Scenario") +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
} else {
# subset projections
df_proj <- df_projections_year %>% filter(ssp != "Baseline")
# subset baseline and aggregate the years to a single reference value
df_base <- df_projections_year %>% filter(ssp == "Baseline") %>%
group_by(subbasin, prediction_percentile) %>%
summarise("p10_base" = mean(p10_ensemble), "p50_base" = mean(p50_ensemble), "p90_base" = mean(p90_ensemble))
# Calculate anomalies
df_proj <- left_join(df_proj, df_base, by = c("subbasin", "prediction_percentile"))
df_proj$p10_anomaly <- df_proj$p10_ensemble - df_proj$p10_base
df_proj$p50_anomaly <- df_proj$p50_ensemble - df_proj$p50_base
df_proj$p90_anomaly <- df_proj$p90_ensemble - df_proj$p90_base
# uci bands - lower intervals may have larger anomalies than higher intervals
df_proj$low_uci <- apply(df_proj[,c("p10_anomaly", "p50_anomaly", "p90_anomaly")], 1, min, na.rm = TRUE)
df_proj$high_uci <- apply(df_proj[,c("p10_anomaly", "p50_anomaly", "p90_anomaly")], 1, max, na.rm = TRUE)
# Data wrangling
df_plot <- df_proj
ylab <- paste0("Change to baseline: ", sub_variable, " [", unique(df_plot$unit), "]")
# plotting
ggplot(df_plot, aes(x = year, color = ssp, fill = ssp, linetype = prediction_percentile)) +
geom_line(aes(y = low_uci)) +
geom_line(aes(y = high_uci)) +
geom_ribbon(aes(ymin = low_uci, ymax = high_uci), alpha = 0.2, linewidth = 0.05)+
scale_x_continuous(expand = c(0,0)) +
scale_color_jco()+
scale_fill_jco()+
labs(x = "Year", y = ylab, title = toupper(paste(sub_variable, "anomalies (change to 2000-2020 baseline)")),
linetype= "Frequency", fill = "Scenario", color = "Scenario") +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
}
})
## Plot 4: Monthly Projections
output$projections_monthly_plot <- renderPlot({
# Dont do anything if no subbasin was selected
if (is.null(rv())) return ("Please select a subbasin by clicking on the map")
# Store widget inputs
sub_subbasin <- rv()
sub_variable <- input$prediction_variable[1]
sub_scenarios <- unique(input$scenario)
sub_percentiles <- unique(input$prediction_percentile)
sub_periods <- unique(input$prediction_period)
# add baseline period
if ("Baseline" %in% sub_scenarios) {
sub_periods <- c("2000-2022", sub_periods)
}
sub_plot_type <- unique(input$plot_type)
df_projections_month <- ds_proj_month %>%
filter(subbasin %in% sub_subbasin,
hype_variable %in% sub_variable,
ssp %in% c("Baseline", sub_scenarios),
prediction_percentile %in% sub_percentiles,
period %in% sub_periods) %>%
collect()
# Absolute change plot
if (sub_plot_type == "Absolute") {
#Subset the dataset based on widget inputs
df_plot <- df_projections_month %>% filter(ssp %in% sub_scenarios)
# Data wrangling
df_plot <- df_plot %>% mutate("scenario" = paste0(ssp, " (", period, ")"))
df_plot$percentile_label <- factor(df_plot$prediction_percentile, levels = c("p10", "p50", "p90"),
labels = c("Low (10th percentile)", "Average (50th percentile)", "High (90th percentile)"))
ylab <- paste0(sub_variable, " [", unique(df_plot$unit), "]")
ggplot(df_plot, aes(x = month, y = p50_ensemble, color = scenario, fill = scenario, linetype = percentile_label)) +
geom_line() +
geom_ribbon(aes(ymin = p10_ensemble, ymax = p90_ensemble), alpha = 0.2, linewidth = 0.05)+
scale_x_continuous(expand = c(0,0)) +
scale_color_jco()+
scale_fill_jco()+
labs(x = "Month", y = ylab, title = toupper(sub_variable), linetype= "Frequency", fill = "Scenario", color = "Scenario") +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
# Anomaly Plots
} else{
# subset projections
df_proj <- df_projections_month %>% filter(ssp != "Baseline")
# subset baseline and aggregate the years to a single reference value
df_base <- df_projections_month %>% filter(ssp == "Baseline") %>%
group_by(subbasin, month, prediction_percentile) %>%
summarise("p10_base" = mean(p10_ensemble), "p50_base" = mean(p50_ensemble), "p90_base" = mean(p90_ensemble))
# Calculate anomalies
df_proj <- left_join(df_proj, df_base, by = c("subbasin", "month", "prediction_percentile"))
df_proj$p10_anomaly <- df_proj$p10_ensemble - df_proj$p10_base
df_proj$p50_anomaly <- df_proj$p50_ensemble - df_proj$p50_base
df_proj$p90_anomaly <- df_proj$p90_ensemble - df_proj$p90_base
# uci bands - lower intervals may have larger anomalies than higher intervals
df_proj$low_uci <- apply(df_proj[,c("p10_anomaly", "p50_anomaly", "p90_anomaly")], 1, min, na.rm = TRUE)
df_proj$high_uci <- apply(df_proj[,c("p10_anomaly", "p50_anomaly", "p90_anomaly")], 1, max, na.rm = TRUE)
#df_proj$p10_uci <- df_proj$p10_anomaly
#df_proj$p10_uci[df_proj$p90_anomaly < df_proj$p10_anomaly] <- df_proj$p90_anomaly[df_proj$p90_anomaly < df_proj$p10_anomaly]
#df_proj$p90_uci <- df_proj$p90_anomaly
#df_proj$p90_uci[df_proj$p10_anomaly > df_proj$p90_anomaly] <- df_proj$p10_anomaly[df_proj$p10_anomaly > df_proj$p90_anomaly]
# Data wrangling
df_plot <- df_proj
ylab <- paste0("Change to baseline: ", sub_variable, " [", unique(df_plot$unit), "]")
# plotting
ggplot(df_plot, aes(x = month, color = ssp, fill = ssp, linetype = prediction_percentile)) +
geom_line(aes(y = low_uci)) +
geom_line(aes(y = high_uci)) +
geom_ribbon(aes(ymin = low_uci, ymax = high_uci), alpha = 0.2, linewidth = 0.05)+
scale_x_continuous(expand = c(0,0)) +
scale_color_jco()+
scale_fill_jco()+
labs(x = "Month", y = ylab, title = toupper(paste(sub_variable, "anomalies (change to 2000-2020 baseline)")),
linetype= "Frequency", fill = "Scenario", color = "Scenario") +
theme_bw() +
theme(text = element_text(size = 11),
legend.position = "bottom",
plot.margin=unit(c(.2,.5,.2,.2),"cm"))
}
})
## Plot 5: Cumulative Frequency Curves
output$projections_cfc_plot <- renderPlot({
# Dont do anything if no subbasin was selected
if (is.null(rv())) return ("Please select a subbasin by clicking on the map")
# Subset the dataset based on widget inputs
sub_subbasin <- rv()
sub_variable <- input$prediction_variable[1]
sub_scenarios <- unique(input$scenario)
sub_periods <- unique(input$prediction_period)
if ("Baseline" %in% sub_scenarios) {
sub_periods <- c("2000-2022", sub_periods)
}
df_plot <- ds_proj_percentiles %>%
filter(subbasin %in% sub_subbasin,
hype_variable %in% sub_variable,
ssp %in% sub_scenarios,
period %in% sub_periods) %>%
collect()
# Data wrangling
df_plot <- df_plot %>% mutate("scenario" = paste0(ssp, " (", period, ")"))
ylab <- paste0(sub_variable, " [", unique(df_plot$unit), "]")