-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
298 lines (265 loc) · 9.49 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
library(plotly)
library(tidyverse)
# Read the data
co2_df <- read_csv("www/data/Countries.csv")
cities_df <- read_csv("www/data/CityAnnualTemps.csv")
# Function to get a list of countries from the data-set
get_countries <- function() {
country_list <- co2_df %>%
distinct(Country) %>%
pull(Country)
return(country_list)
}
# Function to get a list of years from the data-set
get_years <- function() {
countries <- get_countries()
df <- co2_df %>%
filter(Country %in% countries)
year_list <- unique(df$dt)
# Initialize min and max value
min_year <- max(year_list, na.rm = TRUE)
max_year <- min(year_list, na.rm = TRUE)
# Find the maximum among the minimum years
min_year <- df %>%
group_by(Country) %>%
summarise(min_year_country = min(dt)) %>%
pull(min_year_country) %>%
max()
# Find the minimum among the maximum years
max_year <- df %>%
group_by(Country) %>%
summarise(max_year_country = max(dt)) %>%
pull(max_year_country) %>%
min()
year_vec <- c(min_year, max_year)
return(year_vec)
}
# Return a list of all continents.
get_contitents <- function() {
continents <- c(
"Africa", "Asia", "Europe", "Oceania", "North America", "South America"
)
return(continents)
}
# Create vectors for each continent
africa <- c(
"Algeria", "Angola", "Benin", "Botswana", "Burkina Faso", "Burundi",
"Cabo Verde", "Cameroon", "Central African Republic", "Chad", "Comoros",
"Congo", "Côte d’Ivoire", "Djibouti", "Egypt", "Equatorial Guinea", "Eritrea",
"Eswatini", "Ethiopia", "Gabon", "Gambia", "Ghana", "Guinea", "Guinea Bissau",
"Kenya", "Lesotho", "Liberia", "Libya", "Madagascar", "Malawi", "Mali",
"Mauritania", "Mauritius", "Morocco", "Mozambique", "Namibia", "Niger",
"Nigeria", "Rwanda", "Sao Tome and Principe", "Senegal", "Seychelles",
"Sierra Leone", "Somalia", "South Africa", "Sudan", "Tanzania",
"Togo", "Tunisia", "Uganda", "Zambia", "Zimbabwe"
)
asia <- c(
"Afghanistan", "Armenia", "Azerbaijan", "Bahrain", "Bangladesh", "Bhutan",
"Brunei Darussalam", "Cambodia", "China", "China, Hong Kong",
"North Korea", "South Korea", "India", "Indonesia", "Iran", "Iraq", "Israel",
"Japan", "Jordan", "Kazakhstan", "Kuwait", "Kyrgyzstan",
"Laos", "Lebanon", "Malaysia", "Maldives", "Mongolia", "Myanmar", "Nepal",
"Oman", "Pakistan", "Palestine", "Philippines", "Qatar", "Saudi Arabia",
"Singapore", "Sri Lanka", "Syria", "Taiwan", "Tajikistan", "Thailand",
"Timor Leste", "Turkey", "Turkmenistan", "United Arab Emirates",
"Uzbekistan", "Vietnam", "Yemen"
)
europe <- c(
"Åland", "Albania", "Andorra", "Austria", "Belarus", "Belgium",
"Bosnia and Herzegovina", "Bulgaria", "Croatia", "Cyprus", "Czechia",
"Denmark", "Estonia", "Faroe Islands", "Finland", "France", "Germany",
"Greece", "Hungary", "Iceland", "Ireland", "Isle of Man",
"Italy", "Jersey", "Kosovo", "Latvia", "Liechtenstein", "Lithuania",
"Luxembourg", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands",
"North Macedonia", "Norway", "Poland", "Portugal", "Romania",
"Russian Federation", "San Marino", "Serbia", "Slovakia", "Slovenia",
"Spain", "Svalbard and Jan Mayen Islands", "Sweden", "Switzerland", "Ukraine",
"United Kingdom"
)
north_america <- c(
"Bermuda", "Canada", "Greenland", "Saint Pierre and Miquelon",
"United States", "Mexico"
)
oceania <- c(
"American Samoa", "Australia", "Christmas Island", "Fiji", "French Polynesia",
"Guam", "Kiribati", "Federated States Of Micronesia", "New Caledonia",
"New Zealand", "Niue", "Northern Mariana Islands",
"Palau", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga",
"Virgin Islands"
)
south_america <- c(
"Argentina", "Bolivia", "Brazil", "Chile", "Colombia", "Ecuador",
"Guyana", "Paraguay", "Peru", "Suriname", "Uruguay", "Venezuela"
)
# Function to create a table of co2 vs temperature increases
temp_vs_co2_increase <- function(continents, min_year, max_year) {
# Add continents column to DF
selected_df <- co2_df %>%
mutate(Continent = ifelse(
Country %in% asia, "Asia",
ifelse(
Country %in% africa, "Africa",
ifelse(
Country %in% europe, "Europe",
ifelse(
Country %in% north_america, "North America",
ifelse(
Country %in% oceania, "Oceania",
ifelse(
Country %in% south_america, "South America", NA
)
)
)
)
)
))
selected_df <- selected_df %>%
filter(Continent %in% continents) %>%
filter(dt %in% as.character(c(min_year: max_year))) %>%
group_by(Country) %>%
mutate(co2 = sum(co2, na.rm = TRUE)) %>%
filter(dt %in% as.character(c(min_year, max_year))) %>%
mutate(avg_temp_change = ifelse(
!is.na(AverageTemperature),
diff(AverageTemperature),
NA
)) %>%
mutate(max_temp_change = ifelse(
!is.na(MaxAverageTemperature),
diff(MaxAverageTemperature),
NA
)) %>%
mutate(min_temp_change = ifelse(
!is.na(MinAverageTemperature),
diff(MinAverageTemperature),
NA
)) %>%
filter(dt == as.character(max_year)) %>%
select(dt, Country, Continent,
avg_temp_change,
max_temp_change,
min_temp_change,
co2)
return(selected_df)
}
server <- function(input, output){
# Code for creating a bar chart of C02 by continent
# Dynamically set choices based on the list of countries
observe({
updateSelectInput(
inputId = "country_name",
choices = get_contitents(),
selected = get_contitents()
)
})
# Create a reactive that holds the selected countries
selected_countries_reactive <- reactive({
input$country_name
})
# For debugging.
output$selected_countries <- renderPrint({
selected_countries <- selected_countries_reactive()
if (length(selected_countries) > 0) {
paste(selected_countries, collapse = ", ")
}
})
# Code for creating a map of C02 by year
# Dynamically set slider choices based on available years
observe({
updateSliderInput(
inputId = "selected_year",
label = "Select Year",
min = 1856,
max = 2013,
value = c(1856, 2013),
step = 1
)
})
# Create a reactive that holds the selected countries
selected_year_reactive <- reactive({
input$country_year
})
# For debugging.
output$selected_years <- renderPrint({
selected_year <- selected_year_reactive()
if (!is.null(selected_year)) {
selected_year
}
})
output$co2Table <- renderTable({
countries <- selected_countries_reactive()
years <- selected_year_reactive()
min_year <- years[1]
max_year <- years[2]
selected_df <- temp_vs_co2_increase(countries, min_year, max_year) %>%
arrange(desc(co2)) %>%
reframe(
`Year` = as.character(dt),
Country,
`Average Temperature Change` = avg_temp_change,
`Maximum Temperature Change` = max_temp_change,
`Minimum Temperature Change` = min_temp_change,
`Total CO2 Emissions` = co2
)
return(selected_df)
})
output$co2_plotly <- renderPlotly({
countries <- selected_countries_reactive()
years <- selected_year_reactive()
min_year <- years[1]
max_year <- years[2]
selected_df <- temp_vs_co2_increase(countries, min_year, max_year) %>%
filter(!(Country %in% get_contitents()))
selected_df$countryID <- factor(
selected_df$Continent, levels = unique(selected_df$Continent)
)
co2_plotl <- ggplot(selected_df) +
geom_col(aes(
y = co2,
x = countryID,
fill = avg_temp_change,
text = paste0("Region: ", Country, "<br>",
"Year: ", min_year, "-", max_year, "<br>",
"Average Temperature Change: ", avg_temp_change, "<br>",
"Maximum Temperature Change: ", max_temp_change, "<br>",
"Minimum Temperature Change: ", min_temp_change, "<br>",
"Absoluate CO2 Emissions: ", co2)
)) +
labs(title = paste("Climate Change from ", min_year,
"to", max_year,"by Continent"),
fill = "Average <br>Temperature <br>Change (°C)",
x = "Continent", y = "Total CO2 Emissions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_gradient(low = "darkkhaki", high = "darkgreen")
co2_plotly <- ggplotly(co2_plotl, tooltip = "text")
return(co2_plotly)
})
# # Render the second plot.
# output$co2_plotly2 <- renderPlotly({
# years <- selected_year_reactive()
# countries <- get_countries()
# min_year <- years[1]
# max_year <- years[2]
# selected_df <- co2_df %>%
# filter(dt %in% as.character(c(min_year: max_year))) %>%
# filter(Country %in% countries)
#
# co2_plot2 <- ggplot(selected_df) +
# geom_point(aes(
# y = co2_growth_abs,
# x = population,
# fill = abs_co2,
# text = paste0("Region: ", Country, "<br>",
# "Year: ", year, "<br>",
# "CO2 Growth: ", co2_growth_abs, "<br>",
# "Absoluate CO2 Emissions: ", abs_co2)
# )) +
# labs(title = "Annual CO2 Emissions by Region",
# fill = "Absolute CO2 Emmisions",
# x = "Population", y = "Absolute Annual CO2 Growth") # +
# # scale_x_continuous(limits = c(1840, 2020), breaks = seq(1840, 2020, 20))
# co2_plotly2 <- ggplotly(co2_plot2, tooltip = "text")
# return(co2_plotly2)
# })
#
}