-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.R
177 lines (148 loc) · 6.77 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
library(shiny)
library(devtools)
library(ggplot2)
library(hurricaneexposuredata)
library(hurricaneexposure)
library(choroplethrMaps)
library(dplyr)
library(purrr)
library(shinydashboard)
data("hurr_tracks")
data("county_centers")
storms <- unique(hurr_tracks$storm_id)
storm_years <- as.numeric(gsub(".+-", "", storms))
storms <- storms[storm_years <= 2011]
years <- unique(storm_years)
years <- years[years <= 2011]
all_fips <- unique(county_centers$fips)
## Split storm_id based on same year
stm <- split(storms, gsub(".+-", "", storms))
stm <- lapply(stm, function (x) gsub("-.+", "", x))
### Define global variable outside of Shinys
tab_out <- NULL
shinyServer(function(input, output, session) {
output$stormname <- renderUI({
selectInput("storm_name", label = "Storm name", stm[input$year],
selected = "Alberto")
})
output$metric_input <- renderUI({
if(input$metric=="distance"){
numericInput(inputId = paste0("dist_limit"),
paste0("Distance limit (km)"), value = 100)
}else if( input$metric=="rainfall"){
list( ### use list to and ui() to make achieve when select rainfall, three limits appears
numericInput(inputId = paste0("rain_limit"), label = paste0("Rain limit (mm)"),
value = 75),
numericInput(inputId = paste0("dist_limit"), label = paste0("Distance limit (km)"),
value = 500),
sliderInput(inputId = paste0("days_included"),label = paste0("Days included"),
min = -3, max = 3, value = c(-2,1))
)
}else if (input$metric=="wind"){
numericInput(inputId = paste0("wind_limit"),
label = paste0("Wind speed limit (m / s)"), value = 15)
}
})
# First tab
output$exp <- renderPlot({
storm_id <- paste(input$storm_name, input$year, sep = "-")
if(input$metric == "distance"){
b <- map_distance_exposure(storm = storm_id,dist_limit = input$dist_limit)
} else if (input$metric == "rainfall"){
b <- map_rain_exposure(storm = storm_id,rain_limit = input$rain_limit,dist_limit = input$dist_limit,
days_included=input$days_included[1]:input$days_included[2])
} else if (input$metric == "wind"){
b <- map_wind_exposure(storm = storm_id,wind_limit = input$wind_limit)
}
map_tracks(storms = storm_id, plot_object = b, plot_points = FALSE) +
ggtitle(paste(input$storm_name, input$year, input$metric, input$limit, sep = ", "))
# +theme(plot.title = element_text(margin = margin(t = 10, b = -20)))
})
# Second tab
output$table <- DT::renderDataTable(DT::datatable({
if(input$metric == "distance"){
tab_out <<- county_distance(counties = all_fips, start_year = input$year,
end_year = input$year, dist_limit = input$dist_limit) %>%
dplyr::filter(storm_id == paste(input$storm_name,
input$year, sep = "-")) %>%
dplyr::left_join(county_centers, by = "fips") %>%
dplyr::mutate(county = paste(county_name, state_name, sep = ", ")) %>%
dplyr::select(county, fips, closest_date, storm_dist) %>%
arrange(storm_dist)
} else if (input$metric == "rainfall"){
tab_out <<- county_rain(counties = all_fips, start_year = input$year,
end_year = input$year, rain_limit = input$rain_limit,dist_limit = input$dist_limit,
days_included=input$days_included[1]:input$days_included[2]) %>%
### The function below takes day_included=c(-2,-1,0,1) so we need a seq to let the function work
dplyr::filter(storm_id == paste(input$storm_name,
input$year, sep = "-")) %>%
dplyr::left_join(county_centers, by = "fips") %>%
dplyr::mutate(county = paste(county_name, state_name, sep = ", ")) %>%
dplyr::select(county, fips, closest_date, tot_precip) %>%
dplyr::rename(rainfall_mm = tot_precip) %>%
arrange(desc(rainfall_mm))
} else if(input$metric == "wind"){
tab_out <<- county_wind(counties = all_fips, start_year = input$year,
end_year = input$year, wind_limit = input$wind_limit) %>%
dplyr::filter(storm_id == paste(input$storm_name,
input$year, sep = "-")) %>%
dplyr::left_join(county_centers, by = "fips") %>%
dplyr::mutate(county = paste(county_name, state_name, sep = ", ")) %>%
dplyr::select(county, fips, max_sust) %>%
dplyr::rename(wind_mps = max_sust) %>%
arrange(desc(wind_mps))
}
})
)
#Third tab
output$map <-renderPlot({
storm_id <- paste(input$storm_name, input$year, sep = "-")
a <- map_counties(storm = storm_id, metric = input$metric)
map_tracks(storms = storm_id, plot_object = a, plot_points = FALSE) +
ggtitle(paste(input$storm_name, input$year, input$metric, sep = ", "))+
theme(plot.title = element_text(margin = margin(t = 10, b = -20))) ### adjust title position
})
# Fourth tab
output$exp1 <-renderPlot({
storm_id <- paste(input$storm_name, input$year, sep = "-")
a <- map_event_exposure(storm = storm_id, event_type = input$metric)
map_tracks(storms = storm_id, plot_object = a, plot_points = FALSE) +
ggtitle(paste(input$storm_name, input$year, input$metric, sep = ", "))
# +
# theme(plot.title = element_text(margin = margin(t = 10, b = -20))) ### adjust title position
})
# Fifth tab
output$table1 <- DT::renderDataTable(DT::datatable({
c <- county_events(counties = all_fips,
start_year = input$year, end_year = input$year,
event_type = input$metric)
}))
##
observeEvent(input$metric, {
if (input$metric == "distance" | input$metric == "wind" | input$metric == "rainfall") {
output$content <- renderUI({
box(title = "Display", solidHeader = TRUE,status = "warning",
tabsetPanel(
tabPanel("Map_Exposure",plotOutput("exp")),
tabPanel("Table",DT::dataTableOutput("table"),downloadButton('downloadData', 'Download The Table')), ### make download button inside table tab
tabPanel("Map_Counties",plotOutput("map"))
)
)
})
} else {
output$content <- renderUI({
box(title = "Display", solidHeader = TRUE,status = "success",
tabsetPanel(type = "tabs",
tabPanel("Map_Exposure", plotOutput("exp1")),
tabPanel("Table",DT::dataTableOutput("table1"))
)
)
})
}
})
output$downloadData <- downloadHandler(
filename = function() {paste(input$storm_name, input$year, input$metric, input$limit, '.csv', sep='')},
content = function(file) {
write.csv(tab_out, file)
})
})