-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.R
652 lines (528 loc) · 37.2 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
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
## R Shiny app for visualization of CMIP6 global climate model simulations for BC and subregions
## author: Colin Mahony [email protected]
# Copyright 2021 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
library(shiny)
library(RColorBrewer)
library(DT)
library(scales)
library(shinydashboard)
library(markdown)
# ----------------------------------------------
# Load the input data
# ----------------------------------------------
modelMetadata <- read.csv("data/ModelList.csv")
# Define ecoprovinces (subregions of BC) and climate elements
files <- list.files("data/", pattern="^summary.mean")
ecoprovs <- unique(sapply(strsplit(files, "[.]"), "[", 3))
ecoprov.names <- c("British Columbia", "Boreal Plains", "Central Interior", "Coast and Mountains", "Georgia Depression", "Northern Boreal Mountains", "Sub-Boreal Interior", "Southern Interior Mountains", "Southern Interior", "Taiga Plains")
elements <- c("Tave", "Tmax", "Tmin", "PPT", "NFFD", "FFP", "PAS", "EMT", "EXT", "RH", "DD_0", "DD5", "DD_18", "DD18", "TD")
element.names <- c("Mean temperature" , "Mean daily maximum temperature (Tmax)", "Mean daily minimum temperature (Tmin)", "Precipitation", "Number of frost-free days", "Frost-free period", "Precipitation as snow", "Extreme minimum temperature", "Extreme maximum temperature", "Relative Humidity", "Degree Days below 0", "Degree Days above 5", "Degree Days below 18", "Degree Days above 18", "Temperature Difference")
element.names.units <- c(bquote(Mean~temperature~"("*degree*C*")"),bquote(Mean~daily~bold(maximum)~temperature~"("*degree*C*")"),bquote(Mean~daily~bold(minimum)~temperature~"("*degree*C*")"), "Precipitation (mm)", "Number of frost free days (Days)", "Frost-free period (Days)", "Precipitation as snow (mm)", "Extreme minimum temperature (°C)", "Extreme maximum temperature (°C)", "Relative Humidity (%)", "Degree Days below 0", "Degree Days above 5", "Degree Days below 18", "Degree Days above 18", "Temperature Difference (°C)")
variable.names <- read.csv("data/Variables_ClimateBC.csv")
annualElements <- c("Frost-free period", "Extreme minimum temperature", "Extreme maximum temperature")
seasonalElements <- c("Number of frost-free days", "Precipitation as snow", "Relative Humidity")
monthlyElements <- c("Mean temperature" , "Mean daily maximum temperature (Tmax)", "Mean daily minimum temperature (Tmin)", "Precipitation")
funs <- c("bias", "sd")
fun.names <- c("Bias in", "Standard deviation of")
# extract the global climate models and scenarios from an arbitrary file.
files <- list.files("data/", pattern="^ensmin.BC")
template <- read.csv(paste("data/", files[1], sep=""), stringsAsFactors = F)
gcms <- names(template)[-c(1:2, length(names(template)))]
scenarios <- unique(template[,1])
scenario.names <- c("Historical simulations", "SSP1-2.6", "SSP2-4.5", "SSP3-7.0", "SSP5-8.5")
gcm.names <- as.character(modelMetadata[,1])
# Other definitions
monthdays <- c(31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
monthcodes <- c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
seasonmonth.mat <- matrix(monthcodes[c(12, 1:11)],4, byrow=T)
seasons <- c("wt", "sp", "sm", "at")
season.names <- c("Winter", "Spring", "Summer", "Autumn")
yeartimes <- c("",seasons, monthcodes)
yeartime.names <- c("Annual",season.names, month.name)
ensstats <- c("ensmin", "ensmax", "ensmean")
# Define UI ----
ui <- fluidPage(
navbarPage(title = "CMIP6 viewer for British Columbia", theme = "bcgov.css",
tabPanel("App",
sidebarLayout(
sidebarPanel(
helpText("Compare CMIP6 climate model simulations to each other and to observations. Compile custom ensembles with and without bias correction. See projections for individual regions of BC."),
tags$head(tags$script('$(document).on("shiny:connected", function(e) {
Shiny.onInputChange("innerWidth", window.innerWidth);
});
$(window).resize(function(e) {
Shiny.onInputChange("innerWidth", window.innerWidth);
});
')),
checkboxGroupInput("gcms1", "Choose global climate models:",
choiceNames = gcm.names,
choiceValues = gcms,
selected = gcms[4],
inline = T
),
checkboxInput("compile", label = "Compile into custom ensemble", value = FALSE),
checkboxInput("biascorrect", label = "Bias correction (match 1961-80 model climate to observations)", value = FALSE),
checkboxGroupInput("scenarios1", "Choose emissions scenarios",
choiceNames = scenario.names[-1],
choiceValues = scenarios[-1],
selected = scenarios[c(2,3,4)],
inline = T
),
checkboxInput("showmean", label = "Show mean of projections", value = FALSE),
checkboxInput("refline", label = "Show 1961-1990 baseline for models", value = T),
checkboxInput("era5", label = "Show ERA5 reanalysis", value = F),
# ELEMENT NAMES. THIS IS WHERE THE DERIVED VARIABLES WILL BE ADDED TO THE LIST
selectInput("element1",
label = "Choose the climate element",
choices = as.list(element.names),
selected = element.names[1]),
selectInput("yeartime1",
label = "Choose the month/season",
choices = as.list(yeartime.names),
selected = yeartime.names[3]),
checkboxInput("compare", label = "Compare two variables", value = T),
conditionalPanel(
condition = "input.compare == true",
# ELEMENT NAMES. THIS IS WHERE THE DERIVED VARIABLES WILL BE ADDED TO THE LIST
selectInput("element2",
label = "Choose a climate element for comparison",
choices = as.list(element.names),
selected = element.names[1]),
selectInput("yeartime2",
label = "Choose a month/season for comparison",
choices = as.list(yeartime.names),
selected = yeartime.names[1]),
),
selectInput("ecoprov.name",
label = "Choose an ecoprovince",
choices = as.list(ecoprov.names),
selected = ecoprov.names[1]),
downloadButton(outputId = "downloadPlot", label = "Download plot"),
# ~~~ Download csv data button
# downloadButton(outputId = "downloadData", label = "Download data"),
img(src = "Ecoprovinces_Title.png", height = round(1861*1/5), width = round(1993*1/5))
),
mainPanel(
plotOutput(outputId = "timeSeries")
)
),
column(width = 12,
style = "background-color:#003366; border-top:2px solid #fcba19;",
tags$footer(class="footer",
tags$div(class="container", style="display:flex; justify-content:center; flex-direction:column; text-align:center; height:46px;",
tags$ul(style="display:flex; flex-direction:row; flex-wrap:wrap; margin:0; list-style:none; align-items:center; height:100%;",
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home", "Home", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/disclaimer", "Disclaimer", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/privacy", "Privacy", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/accessibility", "Accessibility", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/copyright", "Copyright", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/StaticWebResources/static/gov3/html/contact-us.html", "Contact", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;"))
)
)
)
)
),
tabPanel("bias",
sidebarLayout(
sidebarPanel(
helpText("compare bias and variance among models"),
tags$head(tags$script('$(document).on("shiny:connected", function(e) {
Shiny.onInputChange("innerWidth", window.innerWidth);
});
$(window).resize(function(e) {
Shiny.onInputChange("innerWidth", window.innerWidth);
});
')),
checkboxInput("ClimateBC", label = "Reduce to the ClimateBC ensemble", value = FALSE),
radioButtons("xfun",
label = "choose x-axis type",
choices = list("bias" = 1, "st. dev." = 2),
selected = 1),
radioButtons("yfun",
label = "Choose y-axis type",
choices = list("bias" = 1, "st. dev." = 2),
selected = 2),
selectInput("element3",
label = "Choose the climate element",
choices = as.list(element.names),
selected = element.names[1]),
selectInput("yeartime3",
label = "Choose the month/season",
choices = as.list(yeartime.names),
selected = yeartime.names[3]),
checkboxInput("compare2", label = "Compare two variables", value = F),
conditionalPanel(
condition = "input.compare2 == true",
# ELEMENT NAMES. THIS IS WHERE THE DERIVED VARIABLES WILL BE ADDED TO THE LIST
selectInput("element4",
label = "Choose a climate element for comparison",
choices = as.list(element.names),
selected = element.names[1]),
selectInput("yeartime4",
label = "Choose a month/season for comparison",
choices = as.list(yeartime.names),
selected = yeartime.names[1]),
),
selectInput("ecoprov.name.II",
label = "Choose an ecoprovince",
choices = as.list(ecoprov.names),
selected = ecoprov.names[9]),
img(src = "Ecoprovinces_Title.png", height = 1861*1/5, width = 1993*1/5)
),
mainPanel(
plotOutput(outputId = "scatterPlot")
)
),
column(width = 12,
style = "background-color:#003366; border-top:2px solid #fcba19;",
tags$footer(class="footer",
tags$div(class="container", style="display:flex; justify-content:center; flex-direction:column; text-align:center; height:46px;",
tags$ul(style="display:flex; flex-direction:row; flex-wrap:wrap; margin:0; list-style:none; align-items:center; height:100%;",
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home", "Home", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/disclaimer", "Disclaimer", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/privacy", "Privacy", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/accessibility", "Accessibility", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/copyright", "Copyright", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/StaticWebResources/static/gov3/html/contact-us.html", "Contact", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;"))
)
)
)
)
),
tabPanel("About",
includeMarkdown("about.Rmd"),
img(src = "ModelRes.png", height = 2000/3, width = 1000),
column(width = 12,
style = "background-color:#003366; border-top:2px solid #fcba19;",
tags$footer(class="footer",
tags$div(class="container", style="display:flex; justify-content:center; flex-direction:column; text-align:center; height:46px;",
tags$ul(style="display:flex; flex-direction:row; flex-wrap:wrap; margin:0; list-style:none; align-items:center; height:100%;",
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home", "Home", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/disclaimer", "Disclaimer", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/privacy", "Privacy", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/accessibility", "Accessibility", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/copyright", "Copyright", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/StaticWebResources/static/gov3/html/contact-us.html", "Contact", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;"))
)
)
)
)
),
tabPanel("Model Info",
DT::dataTableOutput("table"),
column(width = 12,
style = "background-color:#003366; border-top:2px solid #fcba19;",
tags$footer(class="footer",
tags$div(class="container", style="display:flex; justify-content:center; flex-direction:column; text-align:center; height:46px;",
tags$ul(style="display:flex; flex-direction:row; flex-wrap:wrap; margin:0; list-style:none; align-items:center; height:100%;",
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home", "Home", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/disclaimer", "Disclaimer", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/privacy", "Privacy", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/accessibility", "Accessibility", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/gov/content/home/copyright", "Copyright", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;")),
tags$li(a(href="https://www2.gov.bc.ca/StaticWebResources/static/gov3/html/contact-us.html", "Contact", style="font-size:1em; font-weight:normal; color:white; padding-left:5px; padding-right:5px; border-right:1px solid #4b5e7e;"))
)
)
)
)
)
)
)
# Define server logic ----
server <- function(input, output, session) {
print("INPUT ############")
#print(input$element1)
observe({
part_choices <- c("Annual",season.names, month.name)
if (input$element1 %in% seasonalElements) {
part_choices <- c("Annual",season.names)
} else if(input$element1 %in% annualElements) {
part_choices <- c("Annual")
}
updateSelectInput(session, "yeartime1", choices=part_choices)
})
observe({
part_choices <- c("Annual",season.names, month.name)
if (input$element2 %in% seasonalElements) {
part_choices <- c("Annual",season.names)
} else if(input$element2 %in% annualElements) {
part_choices <- c("Annual")
}
updateSelectInput(session, "yeartime2", choices=part_choices)
})
# ~~~ Function to return plot data, using mock data currently
# getPlotData <- function() {
#
# # TODO: Need to return actual plot data
# return(data.frame(A=1:10, B=2:11)) # Mock Data
# }
timeSeriesPlot <- function() {
# user specificationS
ecoprov <- ecoprovs[which(ecoprov.names==input$ecoprov.name)]
yeartime1 <- yeartimes[which(yeartime.names==input$yeartime1)]
yeartime2 <- if(input$compare==T) yeartimes[which(yeartime.names==input$yeartime2)] else yeartimes[which(yeartime.names==input$yeartime1)]
element1 <- elements[which(element.names==input$element1)]
element2 <- if(input$compare==T) elements[which(element.names==input$element2)] else elements[which(element.names==input$element1)]
variable1 <- paste(element1, yeartime1, sep= if(yeartime1%in%seasons) "_" else "")
variable2 <- paste(element2, yeartime2, sep= if(yeartime2%in%seasons) "_" else "")
gcms1 <- input$gcms1
scenarios1 <- c("historical", input$scenarios1)
nums <- if(input$compare==T) c(1,2) else c(1)
## Assemble the data that will be used in the plot
alldata <- vector() # a vector of all data on the plot for setting the ylim (y axis range)
num <- 1
for(num in nums){
# data for observations
yeartime <- get(paste("yeartime",num,sep=""))
element <- get(paste("element",num,sep=""))
variable <- get(paste("variable",num,sep=""))
obs.ts.mean <- read.csv(paste("data/ts.obs.mean.", ecoprov, ".csv", sep=""))
x1 <- unique(obs.ts.mean[,1])
y1 <- obs.ts.mean[,which(names(obs.ts.mean)==variable)]
baseline.obs <- mean(y1[which(x1%in%1961:1990)])
recent.obs <- mean(y1[(length(y1)-10):(length(y1))])
#data for GCMs
# ensstat <- ensstats[1]
for(ensstat in ensstats[c(3,1,2)]){ #need to reorder the enstats so that mean comes first, for bias correction
# scenario <- scenarios[1]
## Note: rather than reading in a single large data file, this app stores data in many (~2000) small files, and reads them in on demand by user input
data <- read.csv(paste("data/", paste(ensstat, ecoprov, get(paste("variable",num, sep="")), "csv", sep="."), sep=""))
temp.historical <- data[which(data[,1]=="historical"),-1]
for(scenario in scenarios1){
temp <- data[which(data[,1]==scenario),-1]
if(scenario != "historical"){
temp <- rbind(temp.historical[dim(temp.historical)[1],match(names(temp), names(temp.historical))], temp) # add last year of historical runs
}
if(scenario == "historical") if(ensstat=="ensmean") baseline.mod <- apply(temp[which(temp[,1]%in%1961:1990),-1], 2, mean)
alldata <- c(alldata, as.vector(unlist(temp[-1]))) #store values in a big vector for maintaining a constant ylim
# optional bias correction
if(input$biascorrect==T){
if(element=="PPT"){
delta <- baseline.obs/baseline.mod
delta <- delta[which(names(delta)!="compile")]
temp[,-1] <- sweep(temp[,-1], 2, delta, '*')
} else {
delta <- baseline.obs-baseline.mod
delta <- delta[which(names(delta)!="compile")]
temp[,-1] <- sweep(temp[,-1], 2, delta[match(names(temp[-1]), names(delta))], '+')
}
}
temp$compile <- if(length(gcms1)==0) rep(NA, dim(temp)[1]) else if(length(gcms1)==1) temp[,which(names(temp)==gcms1)] else apply(temp[,which(names(temp)%in%gcms1)], 1, substr(ensstat, 4, nchar(ensstat)), na.rm=T)
assign(paste(ensstat, scenario, num, sep="."), temp)
}
}
}
# PLOT
par(mfrow=c(1,1), mar=c(3,3,0.1,3), mgp=c(1.75, 0.25, 0), cex=1.4)
if(element1==element2){
ylab <- element.names.units[[which(elements==element1)]]
} else {
ylab <- if("PPT"%in%c(element1, element2)) bquote(Precipitation~"("*mm*")"~or~Mean ~ temperature ~ "(" * degree * C * ")") else element.names.units[[1]]
}
plot(0, col="white", xlim=c(1900, 2100), ylim=range(alldata, na.rm = T), xaxs="i", tck=0, xlab="", ylab=ylab)
num <- 1
for(num in nums){
yeartime <- get(paste("yeartime",num,sep=""))
element <- get(paste("element",num,sep=""))
variable <- get(paste("variable",num,sep=""))
# data for observations
x1 <- unique(obs.ts.mean[,1])
y1 <- obs.ts.mean[,which(names(obs.ts.mean)==variable)]
baseline.obs <- mean(y1[which(x1%in%1961:1990)])
recent.obs <- mean(y1[(length(y1)-10):(length(y1))])
# data for era5
if(input$era5==T){
era5.ts.mean <- read.csv(paste("data/ts.era5.mean.", ecoprov, ".csv", sep=""))
x2 <- unique(era5.ts.mean[,1])
y2 <- era5.ts.mean[,which(names(era5.ts.mean)==variable)]
}
if(input$compile==T) gcms1 <- "compile" #this prevents the plotting of individual GCM projections and plots a single envelope for the ensemble as a whole.
for(gcm in gcms1){
# scenario <- scenarios1[1]
for(scenario in scenarios1[order(c(1,4,5,3,2)[which(scenarios%in%scenarios1)])]){
for(ensstat in ensstats){
temp <- get(paste(ensstat, scenario, num, sep="."))
x <- temp[,1]
temp <- temp[,which(names(temp)==gcm)]
assign(ensstat, temp)
if(scenario == "historical"){
assign(paste(ensstat, scenario, sep="."), temp)
}
}
# colScheme <- c("gray60", "seagreen", "goldenrod4", "darkorange3", "darkred")
colScheme <- c("gray60", "dodgerblue4", "seagreen", "darkorange3", "darkred")
# colScheme <- c("gray80", "#1d3354", "#e9dc3d", "#f11111", "#830b22")
polygon(c(x, rev(x)), c(ensmin, rev(ensmax)), col=alpha(colScheme[which(scenarios==scenario)], if(gcm=="ensemble") 0.5 else 0.5), border=colScheme[which(scenarios==scenario)])
if(input$showmean==T) lines(x, ensmean, col=colScheme[which(scenarios==scenario)], lwd=2)
if(input$refline==T){
ref.temp <- mean(ensmean.historical[101:130])
lines(1961:1990, rep(ref.temp, 30), lwd=2)
lines(c(1990,2100), rep(ref.temp, 2), lty=2)
}
if(scenario != "historical"){
par(xpd=T)
baseline <- mean(ensmean.historical[1:50])
projected <- mean(ensmean[(length(x)-10):(length(x))])
if(element=="PPT"){
change <- round(projected/baseline-1,2)
if(is.na(change)==F) text(2098,projected, if(change>0) paste("+",change*100,"%", sep="") else paste(change*100,"%", sep=""), col=colScheme[which(scenarios==scenario)], pos=4, font=2, cex=1)
} else {
change <- round(projected-baseline,1)
if(is.na(change)==F) text(2098,projected, if(change>0) paste("+",change,"C", sep="") else paste(change,"C", sep=""), col=colScheme[which(scenarios==scenario)], pos=4, font=2, cex=1)
}
par(xpd=F)
}
print(scenario)
}
print(gcm)
}
# Text to identify the time of year
# if(input$compare==T){
if(element1==element2){
label <- yeartime.names[which(yeartimes==yeartime)]
} else {
label <- paste(yeartime.names[which(yeartimes==yeartime)], get(paste("element", num, sep="")))
}
temp <- get(paste("ensmax.historical", num, sep="."))
text(1915,mean(temp$compile[60:80]), label, col="black", pos=3, font=2, cex=1)
# }
# add in observations
obs.color <- "blue"
lines(x1[which(x1<1951)], y1[which(x1<1951)], lwd=3, lty=3, col=obs.color)
lines(x1[which(x1>1949)], y1[which(x1>1949)], lwd=3, col=obs.color)
if(element=="PPT"){
change <- round(recent.obs/baseline.obs-1,2)
text(2018,recent.obs, if(change>0) paste("+",change*100,"%", sep="") else paste(change*100,"%", sep=""), col=obs.color, pos=4, font=2, cex=1)
} else {
change <- round(recent.obs-baseline.obs,1)
text(2018,recent.obs, if(change>0) paste("+",change,"C", sep="") else paste(change,"C", sep=""), col=obs.color, pos=4, font=2, cex=1)
}
lines(1961:1990, rep(baseline.obs, 30), lwd=1, col=obs.color)
lines(c(1990,2019), rep(baseline.obs, 2), lty=2, col=obs.color)
era5.color <- "darkorange"
if(input$era5==T){
lines(x2, y2, col=era5.color, lwd=2)
}
#legend
a <- 1
b <- if(input$era5==T) 2 else NA
c <- if(length(gcms1>0)) 3 else NA
s <- !is.na(c(a,b,c))
legend("topleft", title = "Historical Period", legend=c("Station observations", "ERA5 reanalysis", "GCM simulations (min & max)")[s], bty="n",
lty=c(1,1,NA)[s], col=c(obs.color, era5.color, NA)[s], lwd=c(3,2,NA)[s], pch=c(NA,NA, 22)[s], pt.bg = c(NA, NA, colScheme[1])[s], pt.cex=c(NA,NA,2)[s])
s <- which(scenarios[-1]%in%input$scenarios1)
legend("top", title = "Future Scenarios", legend=scenario.names[-1][s], bty="n",
lty=c(NA,NA,NA,NA)[s], col=c(NA,NA,NA,NA)[s], lwd=c(NA,NA,NA,NA)[s], pch=c(22, 22, 22, 22)[s], pt.bg = colScheme[-1][s], pt.cex=c(2,2,2,2)[s])
mtext(ecoprov.names[which(ecoprovs==ecoprov)], side=1, line=-1.5, adj=0.95, font=2, cex=1.4)
print(num)
}
box()
}
output$timeSeries <- renderPlot({ timeSeriesPlot() },
height=reactive(ifelse(!is.null(input$innerWidth),input$innerWidth*0.5,0))
)
output$scatterPlot <- renderPlot({
# ecoprov <- ecoprovs[1]
# yeartime1 <- yeartimes[3]
# yeartime2 <- yeartimes[3]
# element1 <- elements[4]
# element2 <- elements[4]
# xfun <- 1
# yfun <- 2
ecoprov <- ecoprovs[which(ecoprov.names==input$ecoprov.name.II)]
yeartime1 <- yeartimes[which(yeartime.names==input$yeartime3)]
yeartime2 <- if(input$compare2==T) yeartimes[which(yeartime.names==input$yeartime4)] else yeartimes[which(yeartime.names==input$yeartime3)]
element1 <- elements[which(element.names==input$element3)]
element2 <- if(input$compare2==T) elements[which(element.names==input$element4)] else elements[which(element.names==input$element3)]
xfun <- as.numeric(input$xfun)
yfun <- as.numeric(input$yfun)
variable1 <- paste(element1, yeartime1, sep= if(yeartime1%in%seasons) "_" else "")
variable2 <- paste(element2, yeartime2, sep= if(yeartime2%in%seasons) "_" else "")
data.mean <- read.csv(paste("data/summary.mean", ecoprov, "csv", sep="."), stringsAsFactors = F)
data.sd <- read.csv(paste("data/summary.sd", ecoprov, "csv", sep="."), stringsAsFactors = F)
data.sd[,grep("PPT", names(data.bias))] <- data.sd[,grep("PPT", names(data.bias))]/data.mean[,grep("PPT", names(data.bias))] # convert sd to coefficient of variation for precipitation
data.sd[,-c(1:2)] <- sweep(data.sd[,-c(1:2)], MARGIN=2, STATS=as.vector(unlist(data.sd[1,-c(1:2)])), "/")-1 # express sd relative to observational
data.bias <- data.mean
data.bias[,-c(1:2)] <- sweep(data.mean[,-c(1:2)], MARGIN=2, STATS=as.vector(unlist(data.mean[1,-c(1:2)])), "-")
data.bias[,grep("PPT", names(data.bias))] <- sweep(data.mean[,grep("PPT", names(data.mean))], MARGIN=2, STATS=as.vector(unlist(data.mean[1,grep("PPT", names(data.mean))])), "/")-1 #express bias as relative for precipitation
x <- get(paste("data", funs[xfun], sep="."))[, which(names(data.mean)==variable1)]
y <- get(paste("data", funs[yfun], sep="."))[, which(names(data.mean)==variable2)]
# xlim=if(variable.type1=="ratio") range(x) else if(min(x)<0) range(x) else c(0, max(x))
# ylim=if(variable.type2=="ratio") range(y) else if(min(y)<0) range(y) else c(0, max(y))
xlim=range(x)*c(if(min(x)<0) 1.1 else 0.9, if(max(x)>0) 1.1 else 0.9)
ylim=range(y)*c(if(min(y)<0) 1.1 else 0.9, if(max(y)>0) 1.1 else 0.9)
par(mar=c(3,4,0,1), mgp=c(1.25, 0.25,0), cex=1.5)
plot(x,y,col="white", tck=0, xaxt="n", yaxt="n", xlim=xlim, ylim=ylim, ylab="",
xlab=paste(fun.names[xfun], variable.names$Variable[which(variable.names$Code==variable1)]),
)
par(mgp=c(2.5,0.25, 0))
title(ylab=paste(fun.names[yfun], variable.names$Variable[which(variable.names$Code==variable2)]))
lines(c(0,0), c(-99,99), lty=2, col="gray")
lines(c(-99,99), c(0,0), lty=2, col="gray")
gcms <- unique(data.mean$gcm[!is.na(data.mean$run)])
mods <- substr(gcms, 1, 3)
mods[which(mods=="CNR")] <- paste("MIR", c("c", "e"), sep="")
mods[which(mods=="MIR")] <- paste("MIR", c("e", "6"), sep="")
mods[which(mods=="MPI")] <- paste("MPI", c("h", "l"), sep="")
colors = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)][-1]
set.seed(2)
ColScheme <- c(brewer.pal(n=12, "Paired"),sample(colors,length(gcms)-12))
ColScheme[11] <- "blue"
for(gcm in gcms){
i=which(gcms==gcm)
s=which(data.mean$gcm==gcm)
points(x[s],y[s], pch=21, bg=ColScheme[i], cex=1)
points(mean(x[s]),mean(y[s]), pch=21, bg=ColScheme[i], cex=4)
text(mean(x[s]),mean(y[s]), mods[i], cex=0.7, font=2)
}
if(element1=="PPT"){
axis(1, at=seq(-99,99,0.1), labels=paste(round(seq(-99,99,0.1)*100), "%", sep=""), tck=0)
} else axis(1, at=pretty(x), labels=pretty(x), tck=0)
if(element2=="PPT"){
axis(2, at=seq(-99,99,0.1), labels=paste(round(seq(-99,99,0.1)*100), "%", sep=""), las=2, tck=0)
} else axis(2, at=pretty(y), labels=pretty(y), las=2, tck=0)
# legend("bottomleft", legend = c("2001-2019", "2011-2019"), title = "Observed change", pch=c(16, 1), pt.cex=2, col="red", bty="n")
},
height=reactive(ifelse(!is.null(input$innerWidth),input$innerWidth*0.4,0))
)
output$downloadPlot <- downloadHandler(
filename = "Plot.png",
content = function(file) {
pixelratio <- session$clientData$pixelratio
width <- session$clientData$output_timeSeries_width
height <- session$clientData$output_timeSeries_height
png(file, width = width*pixelratio, height = height*pixelratio, res = 72*pixelratio)
timeSeriesPlot()
dev.off()
}
)
# ~~~ Download csv data handler
# output$downloadData <- downloadHandler(
# filename = "Data.csv",
#
# content = function(file) {
# write.csv(getPlotData(), file)
# }
# )
output$table <- DT::renderDataTable({
DT::datatable(modelMetadata,
options = list(pageLength = dim(modelMetadata)[1]),
rownames= FALSE,
caption = 'Model Metadata. Number of runs for each scenario are what we have downloaded, not necessarily what are currently available.'
)
})
}
# Run the app ----
shinyApp(ui = ui, server = server)