-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
359 lines (281 loc) · 8.91 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
library(shiny)
library(bslib)
library(leaflet)
library(tidyverse)
library(plotly)
# setting the custom icons
earthQuakeIcons <- iconList(
earthquake_high = makeIcon(
'./assets/circle3_red.svg',
iconWidth = 24,
iconHeight = 24
),
earthquake_medium = makeIcon(
'./assets/circle3_orange.svg',
iconWidth = 40,
iconHeight = 40
),
earthquake_low = makeIcon(
'./assets/circle3_green.svg',
iconWidth = 40,
iconHeight = 40
)
)
# read the cleaned dataset
df_clean <- read.csv('./data/cleaned_data.csv')
# death and damages emdat data
emdat_df <- read.csv('./data/em_dat_public_earthquake.csv')
emdat_df <- emdat_df %>% mutate(across(where(is.character), ~ iconv(.x, from = "", to = "UTF-8", sub = "")))
# extract the year list
year_list <- as.list(unique(emdat_df$Start.Year))
# Convert the date column from DD/MM/YYYY format to Date object
df_clean$Date <- as.Date(df_clean$Date, format = "%d/%m/%Y")
# Split into day, month, and year
df_clean$Day <- as.integer(format(df_clean$Date, "%d"))
df_clean$Month <- as.integer(format(df_clean$Date, "%m"))
df_clean$Year <- as.integer(format(df_clean$Date, "%Y"))
#######################
#### GEOSPATIAL
###########################
world_earthquakes <- card(
card_header("Quake Analyser"),
# adds sidebar output
layout_sidebar(
fillable = TRUE,
# slider in sidebar
sidebar = sidebar(
# slider input to select the year
sliderInput(
"year", "Select the year", min = 1965, max = 2016, value = 1965,
animate = animationOptions(interval = 3000, loop = TRUE)
),
verbatimTextOutput("year"),
# select input to select the magnitude
sliderInput(
"mag", "Eartquake Magnitude",
min = 0, max = 10, step = 0.1,
value = c(6,10)
),
verbatimTextOutput("mag")
),
# sidebar ends
# plots the leaflet map
leafletOutput("map")
# plot ends hear
)
)
##########################
#### Timeseries Analyser
#############################
timeseries_analyser <- card(
card_header("Earthquakes over the years"),
# adds sidebar output
layout_sidebar(
fillable = TRUE,
# slider in sidebar
sidebar = sidebar(
# select input to select the magnitude
sliderInput(
"mag_2", "Eartquake Magnitude",
min = 0, max = 10, step = 0.1,
value = c(6,10)
),
verbatimTextOutput("mag_2")
),
# sidebar ends
# timeseries plot
plotlyOutput(outputId = "timeseries_plot")
)
)
########################
### HOME
#########################
home <- card(
card_header("Global Earthquake Dashboard"),
# sets the card size to full screen
full_screen = TRUE,
# adds sidebar output
layout_sidebar(
fillable = TRUE,
# slider in sidebar
sidebar = sidebar(
selectInput(
"home_year_select",
"Select Year:",
year_list,
selected="2023"
),
downloadButton("downloadData", "Download CSV")
),
# sidebar ends
# value boxes
layout_column_wrap(
width = 1/3,
value_box(
title = "Earthquakes",
textOutput("earthquakes"),
"Total number of significant earthquakes recorded",
showcase = icon("bullseye"),
theme = "bg-gradient-orange-indigo"
# full_screen = TRUE
),
value_box(
title = "Magnitude",
textOutput("magnitude"),
"Highest magnitude recorded",
showcase = icon("wave-square"),
theme = "bg-gradient-blue-orange"
# full_screen = TRUE
),
value_box(
title = "Fatalities",
textOutput("deaths"),
"Total number of deaths due to earthquakes",
showcase = icon("heart-pulse"),
theme = "bg-gradient-red-indigo"
# full_screen = TRUE
),
value_box(
title = "Damages",
textOutput("damages"),
"Total cost of damages in USD",
showcase = icon("coins"),
theme = "bg-gradient-yellow-indigo"
# showcase_layout = "top",
# full_screen = TRUE
),
value_box(
title = "Injured",
textOutput("injuries"),
"Total number of people injured",
showcase = icon("user-injured"),
theme = "bg-gradient-pink-indigo"
# showcase_layout = "top",
# full_screen = TRUE
),
value_box(
title = "Homeless",
textOutput("homeless"),
"Total number of people left homeless",
showcase = icon("house-circle-exclamation"),
theme = "bg-gradient-teal-indigo"
# full_screen = TRUE
),
),
# value boxes end here
card(
card_header("Most Disastrous Earthquakes"),
tableOutput("table")
)
)
)
ui <- page_navbar(
# page title
title="Earth Quake Analyser",
# sets the page theme
theme = bs_theme(version = 5, bootswatch = "materia"),
# adds spacer
nav_spacer(),
# home page
nav_panel("Home", home),
nav_panel("Geospatial Data Visualiser", world_earthquakes),
nav_panel("Timeseries Analyser", timeseries_analyser),
nav_panel("Advanced Visualisations", "Page C content")
)
server <- function(input, output) {
# bs_themer()
output$year <- renderPrint({ paste("year:", input$year) })
output$mag <- renderPrint({ paste("magnitude:", input$mag[1], "-", input$mag[2]) })
output$mag_2 <- renderPrint({ paste("magnitude:", input$mag_2[1], "-", input$mag_2[2]) })
output$deaths <- renderText({
sum(emdat_df %>% filter(Start.Year == input$home_year_select) %>% select(Total.Deaths), na.rm = T)
})
output$damages <- renderText({
paste("US $",
sum(emdat_df %>% filter(Start.Year == input$home_year_select) %>% select(Total.Damage..Adjusted...000.US..), na.rm = T)
)
})
output$injuries <- renderText({
sum(emdat_df %>% filter(Start.Year == input$home_year_select) %>% select(No..Injured), na.rm = T)
})
output$homeless <- renderText({
sum(emdat_df %>% filter(Start.Year == input$home_year_select) %>% select(No..Homeless), na.rm = T)
})
output$earthquakes <- renderText({
emdat_df %>%
filter(Start.Year == input$home_year_select) %>%
summarise(count = n()) %>%
pull(count)
})
output$magnitude <- renderText({
emdat_df %>%
filter(Start.Year == input$home_year_select) %>%
summarise(magnitude = max(Magnitude, na.rm = T)) %>%
pull(magnitude)
})
# table output
output$table <- renderTable(
emdat_df %>%
filter(Start.Year == input$home_year_select, !is.na(Total.Deaths)) %>%
select(Country, Location, Magnitude, Total.Deaths, Total.Affected) %>%
arrange(desc(Total.Deaths)) %>%
top_n(3)
, striped = TRUE)
output$downloadData <- downloadHandler(
filename = "emdat_earthquake_2000_2023.csv",
content = function(file) {
write.csv(emdat_df, file)
}
)
# renders the map output
output$map <- renderLeaflet({
df_filtered <- df_clean %>%
filter(
Year == as.integer(input$year),
Magnitude >= as.integer(input$mag[1]) & Magnitude <= as.integer(input$mag[2])
)
leaflet(df_filtered %>% filter()) %>%
addTiles() %>%
addMarkers(
lng = ~Longitude, lat = ~Latitude,
icon = earthQuakeIcons["earthquake_high"],
clusterOptions = markerClusterOptions(),
label = ~paste("Date:", Date, "\n", "Magnitude:", Magnitude, "\n", "Depth:", Depth)
) %>%
setView(138, 25, zoom=3)
})
# map ends here
# time series plot
output$timeseries_plot <- renderPlotly({
# prepares the dataframe and groups data by year
df <- df_clean %>%
filter(
Magnitude >= as.integer(input$mag_2[1]) & Magnitude <= as.integer(input$mag_2[2])
) %>%
filter(!is.na(Year), !is.na(Month)) %>%
group_by(Year) %>%
summarise(count=n())
# plot
# plots the time series data
plot_ly(df, type = "scatter", mode="lines") %>%
add_trace(x = ~Year, y = ~count) %>%
layout(
title='Time Series plot of EarthQuakes from 1965 - 2016',
showlegend = F,
xaxis = list(title = "Year", rangeslider = list(visible = T)),
yaxis = list(title = "Number of Earthquakes")
) %>%
layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6'
)
})
# time series plot ends here
}
# bootstraps the app
shinyApp(ui, server)