-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
1012 lines (862 loc) · 43.2 KB
/
server.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
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
server <- function(input, output, session) {
# READ DATA ####
carbon_budgets <- importIPCCData()
countries_emissions <- importCountriesEmissionData()
continents_emissions <- importContinentsEmissionData()
historical_temperatures <- read.csv("src/ourworldindata/temperature-anomaly.csv", stringsAsFactors = F) %>%
filter(entity == "Global") %>%
select(-entity) %>%
mutate(data_id = "asd")
global_emissions <- countries_emissions %>%
group_by(year) %>%
summarize(emissions = sum(emissions, na.rm = T))
projection_data <- read.csv("src/climateactiontracker/EmissionsGaps_mod.csv", stringsAsFactors = F)
# INPUTS ####
# Base year: Year before allocation starts
base_year <- reactive({
# On page "Years left"
allocation_date() - 1
})
selected_probability <- reactive({
input$selected_probability
})
selected_warming_degrees <- reactive({
input$selected_warming_degrees
})
calculation_approach <- reactive({
input$selected_calculation_approach
})
# Selected countries (these inputs are synchronized by means of observers)
selected_countries <- reactive({
# On page "Years left"
input$selected_countries
}) %>%
# Debounce skips intermediary values and therefore avoids "bouncing"
debounce(1000)
selected_countries_2 <- reactive({
# On page "Budget left"
input$selected_countries_2
}) %>%
debounce(1000)
# Selected allocation date: Date when allocation starts (beginning of a year)
# (these inputs are synchronized by means of observers)
allocation_date <- reactive({
input$alloc_date
}) %>%
debounce(1000)
allocation_date_2 <- reactive({
input$alloc_date_2
}) %>%
debounce(1000)
allocation_date_3 <- reactive({
input$alloc_date_3
}) %>%
debounce(1000)
# OPTIONS ####
options(scipen = 999) # Disable scientific notation
# IMPORTANT VARIABLES ####
# Date where the IPCC budgets used were published (Beginning of year)
ipcc_budget_date <- 2018 # For the IPCC SR1.5 report
# Minimum and maximum years of available emissions data
maximum_year <- max(countries_emissions$year)
minimum_year <- min(countries_emissions$year)
# Total country emissions since allocation date
total_country_emis_since_allocation_date <- reactive({
countries_emissions %>%
filter(year >= allocation_date()) %>%
group_by(country) %>%
summarize(total_emis_since_by_gt = sum(emissions)/1000000000) %>%
ungroup()
})
# Cumulated country emissions since allocation date
cumulated_country_emis_since_allocation_date <- reactive({
countries_emissions %>%
filter(year >= allocation_date()) %>%
group_by(country) %>%
mutate(cumulated_emis_since_ad_gt = cumsum(emissions)/1000000000) %>%
ungroup() %>%
select(country, year, cumulated_emis_since_ad_gt)
})
# Total global emissions between allocation date and latest year with data available
total_global_emis_since_allocation_date <- reactive({
global_emissions %>%
filter(year >= allocation_date() & year <= maximum_year) %>%
summarize(total_global_emis = sum(emissions)) %>%
as.numeric()
})
# Total global emissions between allocation date and reference date of IPCC budgets
total_global_emis_between_allocdate_ipccbudgetdate <- reactive({
global_emissions %>%
filter(year >= allocation_date() & year < ipcc_budget_date) %>% # Emissions of years before IPCC budgets apply
summarize(total_global_emis = sum(emissions)) %>%
as.numeric()
})
# Global emissions in base year
global_emissions_base_year <- reactive({
global_emissions %>%
filter(year == base_year()) %>%
select(emissions) %>%
as.numeric()
})
selected_carbon_budget_sr15 <- reactive({
carbon_budgets %>%
filter(probability == selected_probability(),
warming_degrees == selected_warming_degrees()) %>%
select(budget_gt) %>%
as.numeric()
})
# All IPCC carbon budgets related to the allocation date (incl. additional emissions between allocation date and reference date of IPCC budgets)
carbon_budgets_at_allocation_date <- reactive({
carbon_budgets %>%
mutate(budget_gt = budget_gt + total_global_emis_between_allocdate_ipccbudgetdate() / 1000000000)
})
selected_carbon_budget_at_allocation_date <- reactive({
# Budget refers to all emissions between allocation date and the year when emissions reach zero (calculated_zero_emissions_year())
# Base year emissions do not count to this budget
carbon_budgets_at_allocation_date() %>%
filter(probability == selected_probability(),
warming_degrees == selected_warming_degrees()) %>%
select(budget_gt) %>%
as.numeric()
})
ggplot_transparent_theme <- theme_test() +
theme(
panel.background = element_rect(fill = "transparent"), # bg of the panel
panel.grid.major = element_blank(), # get rid of major grid
panel.grid.minor = element_blank(), # get rid of minor grid
panel.border = element_rect(colour = "transparent", fill = NA, size = 0),
plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
title = element_text(colour = "white"),
axis.text = element_text(colour = "white"),
)
country_list <- unique(countries_emissions[["country"]])
# VISUALIZATIONS OF STATUS QUO ####
# Line chart: Historical temperatures ####
linechart_temperatures <- prepareLineChartTemperatures(
data = historical_temperatures,
theme = ggplot_transparent_theme
) %>%
makeLabelArrows(x = 1965, xend = 1970, y = .3, yend = .1, label = "Temporary cooling, possibly\n man-made according to studies") %>%
makeLabelArrows(x = 1985, xend = 1990, y = .6, yend = .4, label = "Rapid warming since the 90s") %>%
makeLabelArrows(x = 1860, xend = 1865, y = .2, yend = .01, label = "This line marks the average \nbetween 1961 and 1990")
output$linechart_temperatures <- renderGirafe(
girafe(ggobj = linechart_temperatures, width_svg = 10, height_svg = 5) %>%
girafe_options(opts_hover(css = "stroke:grey; stroke-width:2px; fill:none; fill-opacity:0") )
)
# Iframe: Sea level rise ####
# output$iframe_sea_level <- renderUI({
# tags$iframe(src = "https://seeing.climatecentral.org/#12/40.7298/-74.0070?show=lockinAnimated&level=0&unit=feet&pois=hide", style = "height:70vh; width:70vw;")
# })
# Bar chart: Emissions per continent + year ####
barchart_continents <- prepareBarChartContinents(data = continents_emissions, theme = ggplot_transparent_theme) %>%
makeLabelArrows(x = 2006, xend = 2008.8, y = 36, yend = 31, label = "Financial crisis 2009 lets\nemissions decline temporarily") %>%
makeLabelArrows(x = 1980, xend = 1982, y = 22, yend = 19, label = "Oil crisis") %>%
makeLabelArrows(x = 1989, xend = 1991, y = 26, yend = 23, label = "Dissolution of the Soviet Union")
output$barchart_continents <- renderGirafe({
girafe(ggobj = barchart_continents, width_svg = 10, height_svg = 5)
})
# Line chart: Emissions per continent + year ####
linechart_continents <- prepareLineChartContinents(continents_emissions, theme = ggplot_transparent_theme) %>%
makeLabelArrows(x = 2002, xend = 2005, y = 12.8, yend = 10.8, label = "China's emissions start \n to rise drastically") %>%
makeLabelArrows(x = 1989, xend = 1992, y = 9.2, yend = 7.4, label = "Europe's emissions decline after \n the dissolution of Soviet Union") %>%
makeLabelArrows(x = 2006, xend = 2008.8, y = 9, yend = 6.8, label = "Financial crisis 2009 lets\nemissions decline temporarily")
output$linechart_continents <- renderGirafe(
girafe(ggobj = linechart_continents, width_svg = 10, height_svg = 5) %>%
girafe_options(opts_hover(css = "stroke:grey; stroke-width:2px; fill:none;") )
)
# Chloropleth: Emissions per capita and country in the most recent (maximum) year ####
world_map <- loadWorldMap(quality = "medium")
world_map_emission_per_capita <- countries_emissions %>%
mutate(emissions = emissions / 1000000) %>% # Convert to Million tons
filter(year == maximum_year)
world_map_emission_per_capita <- sp::merge(world_map, world_map_emission_per_capita, by.x = "admin", by.y = "country")
palette_world_map_emission_per_capita <- colorNumeric(palette = c("white", "darkred", "slateblue3"),
domain = world_map_emission_per_capita@data$emissions_per_cap, na.color = "lightgrey")
tooltip_world_map_emission_per_capita <- paste(
world_map_emission_per_capita@data$admin, ": ",
sprintf("%.1f", round(world_map_emission_per_capita@data$emis_per_capita, 1), nsmall = 0, big.mark = " ", scientific = F),
" t CO2 per capita (",
world_map_emission_per_capita@data$year, ")",
sep = "")
output$chloropleth_emissions_per_capita <- renderLeaflet(
createChloroplethChart(
data = world_map_emission_per_capita,
data_column = "emis_per_capita",
palette = palette_world_map_emission_per_capita,
tooltip = tooltip_world_map_emission_per_capita,
chart_title = "Emissions per <br>capita (t CO2)"
)
)
# Rect chart: Emissions per capita and continent ####
cols_emissions_per_cap_continent <- colorRampPalette(colors = brewer.pal(9, "Paired")[4:9])
chart_emissions_per_cap_continents <- reactive({
continents_emissions %>%
filter(year == input$year_emissions_per_region) %>%
mutate(population = population / 1000000000) %>%
arrange(-emis_per_capita) %>%
mutate(emis_per_capita = round(emis_per_capita, 1),
population_perc = round(population / sum(population, na.rm = T) * 100, 1),
emission_perc = round(emissions / sum(emissions, na.rm = T) * 100, 1),
xmax = cumsum(population),
xmin = lag(xmax, default = 0)) %>%
mutate(pos_label = rowMeans(select(., c("xmin", "xmax")))) %>%
createRectPlotEmissionsRegion(
data = .,
theme = ggplot_transparent_theme,
cols = cols_emissions_per_cap_continent,
year = input$year_emissions_per_region,
pos_label = pos_label
)
})
output$rect_emissions_per_cap <- renderGirafe(
girafe(ggobj = chart_emissions_per_cap_continents(), width_svg = 10, height_svg = 5)
)
# Scatterplot: Emissions per GDP ####
output$scatterplot_emissions_gdp_year <- renderUI({
sliderInput("animate_emissions_gdp", label = "", min = 1971, max = 2018, step = 1, value = 1971, animate = T, sep = "")
})
scatterplot_emissions_gdp_palette <- colorRampPalette(brewer.pal(9, "Paired"))
scatterplot_emissions_gdp_colors <- setNames(scatterplot_emissions_gdp_palette(length(country_list)),
country_list) # To keep colors constant
scatterplot_emissions_gdp_xaxis_limits <- summary(countries_emissions$gdp_per_capita[is.finite(countries_emissions$gdp_per_capita)])[c("Min.", "Max.")]
scatterplot_emissions_gdp_yaxis_limits <- summary(countries_emissions$emis_per_capita[is.finite(countries_emissions$emis_per_capita)])[c("Min.", "Max.")]
scatterplot_emissions_gdp_scale_size_limits <- summary(countries_emissions$emissions[is.finite(countries_emissions$emissions)])[c("Min.", "Max.")]
scatterplot_emissions_gdp <- reactive({
req(input$animate_emissions_gdp)
countries_emissions %>%
mutate(label = ifelse(country %in% input$selected_countries_gdp, country, "")) %>%
filter(is.finite(gdp_per_capita),
year == input$animate_emissions_gdp) %>%
prepareScatterPlotGDP(data = .,
theme = ggplot_transparent_theme,
limits_x = scatterplot_emissions_gdp_xaxis_limits,
limits_y = scatterplot_emissions_gdp_yaxis_limits,
limits_scale = scatterplot_emissions_gdp_scale_size_limits,
colors = scatterplot_emissions_gdp_colors,
year = input$animate_emissions_gdp)
})
output$scatterplot_emissions_gdp <- renderGirafe({
girafe(ggobj = scatterplot_emissions_gdp(), width_svg = 10, height_svg = 5) %>%
girafe_options(opts_toolbar(position = "top"))
})
# Point chart: Remaining budgets ####
# Global emission budgets as defined in the IPCC SR1.5 report
global_emissions_sr15_gt <- reactive({
global_emissions %>%
filter(year == 2018) %>%
select(emissions) %>%
mutate(emissions = emissions / 1000000000) %>%
as.numeric()
})
scatterplot_carbon_budgets <- reactive({
carbon_budgets %>%
mutate(probability = as.character(probability),
data_id = as.character(row_number()),
years_left = budget_gt / global_emissions_sr15_gt()) %>%
prepareScatterPlotBudgets(data = ., theme = ggplot_transparent_theme)
})
output$scatterplot_carbon_budgets <- renderGirafe({
girafe(ggobj = scatterplot_carbon_budgets(), width_svg = 10, height_svg = 5)
})
# Justice Approaches compared ####
# Heading of the main box
output$justice_approaches_heading <- renderUI({
req(input$selected_justice_approach)
if(input$selected_justice_approach == "budget") {
tags$div(tags$h3("Budget Approach"), style = "text-align:center;")
} else if(input$selected_justice_approach == "convergence") {
tags$div(tags$h3("Convergence and Contraction"), style = "text-align:center;")
} else if(input$selected_justice_approach == "grandfathering") {
tags$div(tags$h3("Grandfathering Approach"), style = "text-align:center;")
} else if(input$selected_justice_approach == "other") {
tags$div(tags$h3("Other approaches"), style = "text-align:center;")
}
})
# Output: Text in the main box
output$justice_approaches_text <- renderUI({
tags$div(id = "justice_approaches_content",
hidden(tags$div(id = "budget_content", # Gets visible by means of an observer when user selects this justice approach
tags$h4("Intuition", style = "font-variant:small-caps;"),
"Every human has an equal right to emissions, regardless of nationality.",
tags$h4("How it works", style = "font-variant:small-caps; margin-top:30px;"),
"The remaining budget is allocated based on equal per capita emissions.",
tags$h4("Consequences", style = "font-variant:small-caps; margin-top:30px;"),
"Industrialized states currently exceed their budget and accumulate large 'carbon debts'
due to their high per capita emissions. Developing states are allocated large emission
budgets. If historic emissions are taken into account, budgets of industrialized countries
often are already exceeded by now."
)),
hidden(tags$div(id = "convergence_content",
tags$h4("Intuition", style = "font-variant:small-caps;"),
"Every human has an equal right to emissions, but we should implement it gradually.",
tags$h4("How it works", style = "font-variant:small-caps; margin-top:30px;"),
"Initially, the remaining global budget is distributed based on the existing distribuion ('grandfathering'). Over the years, this is gradually replaced by a distribution based on equal per capita emissions.",
tags$h4("Consequences", style = "font-variant:small-caps; margin-top:30px;"),
"Industrialized countries have to reduce their emissions, but not as drastically as with the Budget Approach. Developing countries can moderately increase their emissions."
)),
hidden(tags$div(id = "grandfathering_content",
tags$h4("Intuition", style = "font-variant:small-caps;"),
"Budgets should be allocated based on pragmatic considerations. The current distribution of emissions is not totally unjust.",
tags$h4("How it works", style = "font-variant:small-caps; margin-top:30px;"),
"Each country's share on the global emissions is held constant.",
tags$h4("Consequences", style = "font-variant:small-caps; margin-top:30px;"),
"Economic development of developing countries could be hindered, as they have to hold their (low) share on global emissions constant. Developed countries are in a comfortable situation."
)),
hidden(tags$div(id = "other_content", # Gets visible by means of an observer when user selects this justice approach
tags$br(), "We might think of other approaches to allocate the carbon budget:",
tags$br(), tags$br(),
tags$ul(tags$li(
"We could distribute emission rights based on the ", tags$u("capabilities"), " of states to reduce emissions, and use a metric such as GDP to approximate this capability"
),
tags$li(
"We could have a closer look at the ", tags$u("individual situation"), " states are in: Are they located in geographical latitude where their energy consumption is
necessarily higher? Did they just face a civil war and need more resources to recover?"
),
tags$li(
"Instead of looking at where emissions accrue, we could look at ", tags$u("on whose behalf"), " they accrue - a large portion of China's emissions, for example,
accrues for products which are sold in Europe or the US! In this so called ", tags$a(href = "https://ourworldindata.org/co2-and-other-greenhouse-gas-emissions#consumption-based-trade-adjusted-co2-emissions",
"\"consumption-based view\""), ", even more emissions would be attributed to industrialized countries."
)
), tags$br(), tags$br()
))
)
})
# Observers which trigger appearance of content based on user selection
observeEvent(input$selected_justice_approach, {
req(input$selected_justice_approach)
if(input$selected_justice_approach == "budget") {
shinyjs::hide("convergence_content")
shinyjs::hide("grandfathering_content")
shinyjs::hide("other_content")
shinyjs::show("budget_content")
shinyjs::show("exemplary_years_left")
updateSelectInput(session, "selected_calculation_approach", selected = "budget")
} else if(input$selected_justice_approach == "convergence") {
shinyjs::hide("budget_content")
shinyjs::hide("grandfathering_content")
shinyjs::hide("other_content")
shinyjs::show("convergence_content")
shinyjs::show("exemplary_years_left")
updateSelectInput(session, "selected_calculation_approach", selected = "convergence")
} else if(input$selected_justice_approach == "grandfathering") {
shinyjs::hide("convergence_content")
shinyjs::hide("budget_content")
shinyjs::hide("other_content")
shinyjs::show("grandfathering_content")
shinyjs::show("exemplary_years_left")
updateSelectInput(session, "selected_calculation_approach", selected = "grandfathering")
} else if(input$selected_justice_approach == "other") {
shinyjs::hide("convergence_content")
shinyjs::hide("budget_content")
shinyjs::hide("grandfathering_content")
shinyjs::hide("exemplary_years_left")
shinyjs::show("other_content")
# If "other" is selected, choose the Budget Approach as calculation method and set base year to 1990 (so that the next page "Years left" starts with this combination)
updateSelectInput(session, "selected_calculation_approach", selected = "budget")
updateSliderInput(session, "base_year", value = 1990)
}
})
# When the user selects the inclusion of past emissions, set 1990 as base year
observe({
if(input$selection_past_emissions == T) {
updateSliderInput(session, "alloc_date", value = 1990)
shinyjs::hide("text_historical_emissions_no")
shinyjs::show("text_historical_emissions_yes")
} else {
updateSliderInput(session, "alloc_date", value = maximum_year)
shinyjs::hide("text_historical_emissions_yes")
shinyjs::show("text_historical_emissions_no")
}
})
# Create texts below the slider for historical emissions
output$text_historical_emissions_yes <- renderUI({
paste0("Allocation starts in ", allocation_date(), " ", ifelse(allocation_date() == 1990, "(this is the year where climate change became a problem widely known)", ""))
})
output$text_historical_emissions_no <- renderUI({
paste0("Allocation starts in ", allocation_date())
})
# Bar chart: Approaches compared ####
# Small bar chart with exemplary changes to states' budgets
selected_justice_approach_text <- reactive({
if(input$selected_justice_approach == "budget") {
"Budget Approach"
} else if(input$selected_justice_approach == "convergence") {
"Convergence and Contraction"
} else {
"Grandfathering Approach"
}
})
barchart_exemplary_years_left <- reactive({
just_emission_budgets_countries() %>%
filter(country %in% c("United States", "Mexico", "Botswana")) %>%
left_join(countries_emissions) %>%
mutate(emissions = emissions / 1000000000) %>%
mutate(budget_reach = base_year() + floor(total_country_budget_gt / emissions)) %>%
createBarChartExemplaryStates(
data = .,
theme = ggplot_transparent_theme,
cols = cols_countries_years_left,
selected_budget = selected_carbon_budget_sr15(),
start_year = maximum_year,
temperature_target = input$selected_warming_degrees
)
})
output$exemplary_years_left <- renderGirafe({
girafe(ggobj = barchart_exemplary_years_left())
})
# Chart: Future projections
projection_data_2100 <- projection_data %>% filter(year == 2100) %>%
gather(-year, key = "key", value = "value") %>%
filter(!key %in% c("optimistic_policy", "d2_median", "d15_median", "historical")) %>%
mutate(split = str_detect(key, "high")) %>%
rowwise() %>%
mutate(cat = str_sub(key, 0, str_locate(key, "_")[1]-1)) %>%
ungroup() %>%
select(-key) %>%
group_by(split) %>%
spread(split, value) %>%
rename(ymin = "FALSE", ymax = "TRUE") %>%
mutate(xmin = year+1, xmax = year+3) %>%
mutate(cat = c("Baseline: 4.1 - 4.8°C", "Current policies: 2.8 - 3.2°C", "1.5°C consistent: 1.3°C", "2°C consistent: 1.7 - 1.7°C", "Pledges & Targets: 2.5 - 2.8°C"),
desc = paste0(round(ymin, 0), " to ", round(ymax, 0), " Gt yearly emissions"))
ribbon_chart_projections <- createRibbonChartScenarios(
data_ribbon = projection_data,
data_column = projection_data_2100,
theme = ggplot_transparent_theme
)
output$ribbon_chart_projections <- renderGirafe({
girafe(ggobj = ribbon_chart_projections, width_svg = 10, height_svg = 5)
})
# CALCULATION OF JUST BUDGET ####
population_percentages_allyears <- countries_emissions %>%
group_by(year) %>%
mutate(population_percentage = population / sum(population, na.rm = T)) %>%
ungroup() %>%
select(country, year, population_percentage)
# Calculate global emissions pathway for Contraction & Convergence ####
# Assumption: Global pathway is a linear path to zero
calculated_zero_emissions_year <- reactive({
# The calculated zero emissions year is always an integer; in reality, it is very probable that emissions go to zero in between the year.
# Due to this, the cumulated emissions of the global future linear pathway do not exactly meet the IPCC budget
ceiling(
# ceiling() rounds up to the next integer
base_year() + 2 * selected_carbon_budget_at_allocation_date() / (global_emissions_base_year() / 1000000000)
)
})
calculated_reduction_per_year <- reactive({
(global_emissions_base_year() / 1000000000) / (calculated_zero_emissions_year() - base_year())
})
global_future_pathway_linear <- reactive({
# Forecasted pathway from base year until the year when emissions reach zero
data.frame(
year = base_year() : calculated_zero_emissions_year(),
cumulated_reductions_absolute = calculated_reduction_per_year(),
global_emissions = global_emissions_base_year() / 1000000000
) %>%
mutate(cumulated_reductions_absolute = ifelse(year == base_year(), 0, cumulated_reductions_absolute)) %>% # Base year: No reductions
mutate(
cumulated_reductions_absolute = cumsum(cumulated_reductions_absolute),
global_emissions = global_emissions - cumulated_reductions_absolute,
# Ct is zero for base year; base year emissions are however not part of the global carbon budget
Ct = (year - base_year()) / (calculated_zero_emissions_year() - base_year())
) %>%
select(-cumulated_reductions_absolute) %>%
mutate(global_emissions = global_emissions * 1000000000)
})
# Per country: Assigned emission budgets (only for base year) ####
just_emission_budgets_countries <- reactive({
if(input$selected_calculation_approach == "budget") {
countries_emissions %>%
left_join(population_percentages_allyears) %>%
filter(year == base_year()) %>%
mutate(total_country_budget_gt = population_percentage * selected_carbon_budget_at_allocation_date()) %>%
select(country, year, total_country_budget_gt, data_id)
} else if(input$selected_calculation_approach == "grandfathering") {
countries_emissions %>%
filter(year == base_year()) %>%
mutate(emission_share = emissions / global_emissions_base_year()) %>%
mutate(total_country_budget_gt = emission_share * selected_carbon_budget_at_allocation_date()) %>%
select(country, year, total_country_budget_gt, data_id)
} else if(input$selected_calculation_approach == "convergence") {
# Very simple approach: Countries' population share is assumed to be constant between base and target year.
# Linear version of Convergence & Contraction: Linear transition from allocation according to base year emissions to alloc. acc. to population share
countries_emissions %>%
left_join(population_percentages_allyears) %>%
# Take base year emissions as starting point, which are then linearly reduced from the next year on
filter(year == base_year()) %>%
select(country, emissions, population_percentage, data_id) %>%
rename(emission_forecast = emissions) %>%
sp::merge(global_future_pathway_linear()) %>%
group_by(country) %>%
arrange(year) %>%
mutate(emission_forecast = calculateContractionConvergence(emission_forecast, Ct, global_emissions, population_percentage)) %>%
mutate(total_country_budget_gt = sum(emission_forecast[-1]) / 1000000000) %>% # Base year emissions (row 1) do not count to the remaining budget
ungroup() %>%
filter(year == base_year()) %>% # Choose one random year, just to remove duplicates
select(country, year, total_country_budget_gt, data_id)
}
})
# Per country: Budget still left per year ####
just_emission_budgets_countries_left <- reactive({
countries_emissions %>%
filter(year >= base_year()) %>%
left_join(just_emission_budgets_countries() %>% select(-year, -data_id)) %>% # Join with total budget per country for the base year
left_join(cumulated_country_emis_since_allocation_date()) %>%
mutate(budget_left = total_country_budget_gt - cumulated_emis_since_ad_gt,
budget_left_perc = budget_left / total_country_budget_gt * 100) %>%
select(country, year, total_country_budget_gt, budget_left, budget_left_perc, data_id)
})
# VISUALIZATIONS OF CALCULATIONS ####
# Barchart: How many years are left per country? ####
cols_countries_years_left <- colorRampPalette(colors = brewer.pal(9, "Paired"))
barchart_countries_years_left <- reactive({
req(selected_countries(), just_emission_budgets_countries(), total_country_emis_since_allocation_date())
just_emission_budgets_countries() %>%
filter(country %in% c(selected_countries())) %>%
left_join(total_country_emis_since_allocation_date()) %>%
left_join(countries_emissions) %>%
mutate(emissions = emissions / 1000000000) %>%
mutate(budget_reach = base_year() + floor(total_country_budget_gt / emissions)) %>%
createBarChartYearsLeft(
data = .,
base_year = base_year(),
theme = ggplot_transparent_theme,
cols = cols_countries_years_left(length(selected_countries()))
)
})
output$years_left <- renderGirafe({
girafe(ggobj = barchart_countries_years_left(), width_svg = 10, height_svg = 4.5)
})
# Heatmap: How many of the just budget is left? (for all years between base year and maximum year) ####
heatmap_just_budget_scales <- reactive({
req(selected_countries(), just_emission_budgets_countries_left())
just_emission_budgets_countries_left() %>%
filter(country %in% selected_countries()) %>%
select(budget_left_perc) %>%
createChartScales()
})
heatmap_budget_left_allyears <- reactive({
req(just_emission_budgets_countries_left(), selected_countries())
just_emission_budgets_countries_left() %>%
filter(country %in% selected_countries()) %>%
createHeatmapEmissionBudgetLeft(data = .,
scales = heatmap_just_budget_scales(),
from = base_year(),
to = maximum_year,
theme = ggplot_transparent_theme)
})
output$heatmap_budget_left_allyears <- renderGirafe(
girafe(ggobj = heatmap_budget_left_allyears(), width_svg = 10, height_svg = 4.5)
)
# Barchart: Budget left (percent), leaders & laggards (facets) ####
barchart_leaders_laggards_scales <- reactive({
if(sum(just_emission_budgets_countries_left()$budget_left_perc[just_emission_budgets_countries_left()$year == maximum_year] < 0, na.rm = T) > 0) {
scale_fill_gradientn(colours = c("red", "white", "green"),
values = scales::rescale(c(-2000, 0, 100)))
} else {
scale_fill_gradientn(colours = c("tan1", "lightgreen", "darkgreen"),
values = scales::rescale(c(0, 10, 100)))
}
})
barchart_leaders_laggards <- reactive({
just_emission_budgets_countries_left() %>%
filter(year == maximum_year) %>%
group_by(budget_left_perc < 0) %>%
top_n(10, abs(budget_left_perc)) %>%
ungroup() %>%
head(20) %>% # If all countries have the same value, this ensures that not all countries are plotted, only the first 20
mutate(country = reorder(country, budget_left_perc)) %>%
createBarChartLeadersLaggards(
data = .,
theme = ggplot_transparent_theme,
scales = barchart_leaders_laggards_scales()
)
})
output$barchart_leaders_laggards <- renderGirafe(
girafe(ggobj = barchart_leaders_laggards(), width_svg = 10, height_svg = 5)
)
# OBSERVERS ####
# Trigger "Sources" ####
onevent("click", "sources_temperature", toggle("sources_temperature_text"))
onevent("click", "sources_consequences", toggle("sources_consequences_text"))
onevent("click", "sources_scenarios", toggle("sources_scenarios_text"))
onevent("click", "sources_emissions_timeseries", toggle("sources_emissions_timeseries_text"))
onevent("click", "sources_continent_emissions", toggle("sources_continent_emissions_text"))
onevent("click", "sources_chloropleth", toggle("sources_chloropleth_text"))
onevent("click", "sources_rect", toggle("sources_rect_text"))
onevent("click", "sources_gdp", toggle("sources_gdp_text"))
onevent("click", "sources_ipcc", toggle("sources_ipcc_text"))
onevent("click", "sources_justice_approaches", toggle("sources_justice_approaches_text"))
onevent("click", "sources_years_left", toggle("sources_years_left_text"))
onevent("click", "sources_budget_left", toggle("sources_budget_left_text"))
onevent("click", "sources_laggards", toggle("sources_laggards_text"))
# Sync all panels ####
# In Shiny, you only can display an inputId once, but we need to have the same inputs (basie data for budget calculation) on several tabs
# Thus, they have different inputIds, and we need this observer to synchronize them
# Selected countries
observe({
updateSelectizeInput(
session, inputId = "selected_countries", selected = c(selected_countries_2())
)
})
observe({
updateSelectizeInput(
session, inputId = "selected_countries_2", selected = c(selected_countries())
)
})
# Base year
observe({
updateSliderInput(
session, inputId = "alloc_date", value = c(allocation_date_2())
)
})
observe({
updateSliderInput(
session, inputId = "alloc_date", value = c(allocation_date_3())
)
})
observe({
updateSliderInput(
session, inputId = "alloc_date_2", value = c(allocation_date())
)
})
observe({
updateSliderInput(
session, inputId = "alloc_date_3", value = c(allocation_date_2())
)
})
observe({
updateSliderInput(
session, inputId = "alloc_date_3", value = c(allocation_date())
)
})
# Selected probability
observe({
updateSelectInput(
session, inputId = "selected_probability", selected = c(input$selected_probability_2)
)
})
observe({
updateSelectInput(
session, inputId = "selected_probability", selected = c(input$selected_probability_3)
)
})
observe({
updateSelectInput(
session, inputId = "selected_probability_2", selected = c(input$selected_probability)
)
})
observe({
updateSelectInput(
session, inputId = "selected_probability_3", selected = c(input$selected_probability_2)
)
})
observe({
updateSelectInput(
session, inputId = "selected_probability__3", selected = c(input$selected_probability)
)
})
# Warming degrees
observe({
updateSelectInput(
session, inputId = "selected_warming_degrees", selected = c(input$selected_warming_degrees_2)
)
})
observe({
updateSelectInput(
session, inputId = "selected_warming_degrees", selected = c(input$selected_warming_degrees_3)
)
})
observe({
updateSelectInput(
session, inputId = "selected_warming_degrees_2", selected = c(input$selected_warming_degrees)
)
})
observe({
updateSelectInput(
session, inputId = "selected_warming_degrees_3", selected = c(input$selected_warming_degrees_2)
)
})
observe({
updateSelectInput(
session, inputId = "selected_warming_degrees_3", selected = c(input$selected_warming_degrees)
)
})
# Calculation approach
observe({
updateSelectInput(
session, inputId = "selected_calculation_approach", selected = c(input$selected_calculation_approach_2)
)
})
observe({
updateSelectInput(
session, inputId = "selected_calculation_approach", selected = c(input$selected_calculation_approach_3)
)
})
observe({
updateSelectInput(
session, inputId = "selected_calculation_approach_2", selected = c(input$selected_calculation_approach)
)
})
observe({
updateSelectInput(
session, inputId = "selected_calculation_approach_3", selected = c(input$selected_calculation_approach_2)
)
})
observe({
updateSelectInput(
session, inputId = "selected_calculation_approach_3", selected = c(input$selected_calculation_approach)
)
})
# Jump to next / previous page ####
observeEvent(input$forwardToPage2,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Temperatures")
})
observeEvent(input$forwardToPage3,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Emissions")
})
observeEvent(input$forwardToPage4,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Consequences")
})
observeEvent(input$forwardToPage5,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Scenarios")
})
observeEvent(input$forwardToPage6,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Emitters")
})
observeEvent(input$forwardToPage7,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Per capita")
})
observeEvent(input$forwardToPage8,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "History")
})
observeEvent(input$forwardToPage9,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Affluence")
})
observeEvent(input$forwardToPage10,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Carbon budgets")
})
observeEvent(input$forwardToPage11,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Climate Justice")
})
observeEvent(input$forwardToPage12,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Years left")
})
observeEvent(input$forwardToPage13,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Budget left")
})
observeEvent(input$forwardToPage14,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Leaders")
})
observeEvent(input$forwardToPage15,{
updateTabsetPanel(session, inputId = "tabset-panel", selected = "Conclusion")
})
# Create choice values for gdp scatterplot country selection ####
observe({
updateSelectizeInput(session, "selected_countries_gdp",
choices = country_list,
selected = c("China", "Mozambique", "Sweden"),
server = T
)
})
# Create choice values for budget left country selection ####
observe({
updateSelectizeInput(session, "selected_countries",
choices = country_list,
selected = c("Australia", "Brazil", "Canada", "China", "Chile", "Ethiopia", "France",
"Germany", "India", "Japan", "Qatar", "Russia", "Spain", "United Kingdom", "United States", "United Arab Emirates"),
server = T
)
})
observe({
updateSelectizeInput(session, "selected_countries_2",
choices = country_list,
selected = c("Australia", "Brazil", "Canada", "China", "Chile", "Ethiopia", "France",
"Germany", "India", "Japan", "Qatar", "Russia", "Spain", "United Kingdom", "United States", "United Arab Emirates"),
server = T
)
})
observe({
updateSelectizeInput(session, "selected_countries_3",
choices = country_list,
selected = c("Australia", "Brazil", "Canada", "China", "Chile", "Ethiopia", "France",
"Germany", "India", "Japan", "Qatar", "Russia", "Spain", "United Kingdom", "United States", "United Arab Emirates"),
server = T
)
})
# Images on start page ####
value_startpage <- reactiveVal(0)
# Create endless chain of numbers 1 to 3
observe({
invalidateLater(10000, session)
if(isolate(value_startpage() < 3)) {
isolate(value_startpage(value_startpage() + 1))
} else {
isolate(value_startpage(1))
}
})
# Depending on value, change the picture displayed
observe({
if(value_startpage() == 1) {
shinyjs::show("startpage_image_1")
shinyjs::hide("startpage_image_2")
shinyjs::hide("startpage_image_3")
} else if(value_startpage() == 2) {
shinyjs::hide("startpage_image_1")
shinyjs::show("startpage_image_2")
shinyjs::hide("startpage_image_3")
} else {
shinyjs::hide("startpage_image_1")
shinyjs::hide("startpage_image_2")
shinyjs::show("startpage_image_3")
}
})
# Images on consequences page ####
value_consequences <- reactiveVal(0)
# Create endless chain of numbers 1 to 5
observe({
invalidateLater(7000, session)
if(isolate(value_consequences() < 5)) {
isolate(value_consequences(value_consequences() + 1))
} else {
isolate(value_consequences(1))
}
})
observe({
if(value_consequences() == 1) {
shinyjs::show("consequences_1")
shinyjs::hide("consequences_2")
shinyjs::hide("consequences_3")
shinyjs::hide("consequences_4")
shinyjs::hide("consequences_5")
} else if(value_consequences() == 2) {
shinyjs::hide("consequences_1")
shinyjs::show("consequences_2")
shinyjs::hide("consequences_3")
shinyjs::hide("consequences_4")
shinyjs::hide("consequences_5")
} else if(value_consequences() == 3) {
shinyjs::hide("consequences_1")
shinyjs::hide("consequences_2")
shinyjs::show("consequences_3")
shinyjs::hide("consequences_4")
shinyjs::hide("consequences_5")
} else if(value_consequences() == 4) {
shinyjs::hide("consequences_1")
shinyjs::hide("consequences_2")
shinyjs::hide("consequences_3")
shinyjs::show("consequences_4")
shinyjs::hide("consequences_5")
} else {
shinyjs::hide("consequences_1")
shinyjs::hide("consequences_2")
shinyjs::hide("consequences_3")
shinyjs::hide("consequences_4")
shinyjs::show("consequences_5")