-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
379 lines (347 loc) · 11.7 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
server <- function(input, output, session) {
rv <- reactiveValues()
observeEvent(input$DELWP_REGION, {
if (input$DELWP_REGION != "ALL") {
updateSelectInput(session, inputId = "FIRE_REGION", selected = "ALL")
updateSelectInput(session, inputId = "FIRE_DISTRICT", selected = "ALL")
}
})
observeEvent(input$FIRE_REGION, {
if (input$FIRE_REGION != "ALL") {
updateSelectInput(session, inputId = "DELWP_REGION", selected = "ALL")
updateSelectInput(session, inputId = "FIRE_DISTRICT", selected = "ALL")
}
})
observeEvent(input$FIRE_DISTRICT, {
if (input$FIRE_DISTRICT != "ALL") {
updateSelectInput(session, inputId = "DELWP_REGION", selected = "ALL")
updateSelectInput(session, inputId = "FIRE_REGION", selected = "ALL")
}
})
# Observer for TFI Status plots ----
observe({
tfiFiltered <- TFI %>%
filter(SEASON %in% input$SEASONS[1]:input$SEASONS[2]) %>%
{
if (input$DELWP_REGION != "ALL") {
filter(., DELWP_REGION == input$DELWP_REGION)
} else {
(.)
}
} %>%
{
if (input$FIRE_REGION != "ALL") {
filter(., FIRE_REGION_NAME == input$FIRE_REGION)
} else {
(.)
}
} %>%
{
if (input$FIRE_DISTRICT != "ALL") {
filter(., DISTRICT_N == input$FIRE_DISTRICT)
} else {
(.)
}
} %>%
{
if (input$FMZ != "ALL") {
filter(., FIRE_FMZ_NAME == input$FMZ)
} else {
(.)
}
} %>%
{
if (input$EFG_NAME != "ALL EFG") {
filter(., EFG_NAME == input$EFG_NAME)
} else {
(.)
}
} %>%
group_by(EFG_NAME, TFI_STATUS, SEASON) %>%
summarise(Hectares = sum(Hectares, na.rm = T))
tfiOverall <- tfiFiltered %>%
group_by(TFI_STATUS, SEASON) %>%
summarise(Hectares = sum(Hectares, na.rm = T))
# TFI status plot ----
tfiPlot <- tfiFiltered %>%
{
if (input$SEASONS[1] < input$SEASONS[2]) {
ggplot(., aes(x = SEASON, y = Hectares, fill = TFI_STATUS)) +
geom_col() +
tfiScale() +
facet_wrap(
facets = ~EFG_NAME,
scales = "free",
ncol = 6
)
} else {
ggplot(., aes(x = EFG_NAME, y = Hectares, fill = TFI_STATUS)) +
geom_col(position = "fill") +
tfiScale() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
}
} +
labs(x = "Fire Year") +
theme(
axis.text = element_text(size = 12),
axis.title = element_text(size = 14)
)
# TFI status overall summary plot ----
tfiOverallPlot <- ggplot(tfiOverall, aes(x = SEASON, y = Hectares, fill = TFI_STATUS)) +
geom_col(position = "fill") +
tfiScale() +
theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = 1)) +
labs(x = "Fire Year", y = "Proportion of total area")
output$tfiPlot <- renderPlot(tfiPlot)
output$tfiOverallPlot <- renderPlot(tfiOverallPlot)
})
# Observer for BBTFI plots -----
observe({
bbtfiFiltered <- BBTFI %>%
filter(SEASON %in% input$SEASONS[1]:input$SEASONS[2]) %>%
filter(!is.na(EFG_NAME)) %>%
{
if (input$DELWP_REGION != "ALL") {
filter(., DELWP_REGION == input$DELWP_REGION)
} else {
(.)
}
} %>%
{
if (input$FIRE_REGION != "ALL") {
filter(., FIRE_REGION_NAME == input$FIRE_REGION)
} else {
(.)
}
} %>%
{
if (input$FIRE_DISTRICT != "ALL") {
filter(., DISTRICT_N == input$FIRE_DISTRICT)
} else {
(.)
}
} %>%
{
if (input$FMZ != "ALL") {
filter(., FIRE_FMZ_NAME == input$FMZ)
} else {
(.)
}
} %>%
{
if (input$EFG_NAME != "ALL EFG") {
filter(., EFG_NAME == input$EFG_NAME)
} else {
(.)
}
} %>%
group_by(EFG_NAME, TBTFI, SEASON) %>%
summarise(Hectares = sum(Hectares, na.rm = T))
bbtfiOverall <- bbtfiFiltered %>%
group_by(TBTFI, SEASON) %>%
summarise(Hectares = sum(Hectares, na.rm = T))
bbtfiPlot <- bbtfiFiltered %>%
ggplot(aes(x = SEASON, y = Hectares, fill = TBTFI)) +
geom_col() +
scale_fill_brewer(palette = "YlOrRd", direction = -1) +
facet_wrap(
facets = ~EFG_NAME,
scales = "free_y",
ncol = 6
) +
labs(x = "Fire Year") +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14)
)
bbtfiOverallPlot <- bbtfiOverall %>%
ggplot(aes(x = SEASON, y = Hectares, fill = TBTFI)) +
geom_col() +
scale_fill_brewer(palette = "YlOrRd", direction = -1) +
labs(x = "Fire Year") +
theme(
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14),
panel.background = element_rect(fill = "grey70"),
axis.line = element_line(color = "black"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.border = element_blank()
)
output$bbtfiPlot <- renderPlot(bbtfiPlot)
output$bbtfiOverallPlot <- renderPlot(bbtfiOverallPlot)
})
# Observer for RA plots and summary -----
observe({
sppSumm <- RA %>%
filter(SEASON %in% input$SEASONS[1]:input$SEASONS[2]) %>%
filter(!is.na(EFG_NAME)) %>%
{
if (input$DELWP_REGION != "ALL") {
filter(., DELWP_REGION == input$DELWP_REGION)
} else {
(.)
}
} %>%
{
if (input$FIRE_REGION != "ALL") {
filter(., FIRE_REGION_NAME == input$FIRE_REGION)
} else {
(.)
}
} %>%
{
if (input$FIRE_DISTRICT != "ALL") {
filter(., DISTRICT_N == input$FIRE_DISTRICT)
} else {
(.)
}
} %>%
{
if (input$FMZ != "ALL") {
filter(., FIRE_FMZ_NAME == input$FMZ)
} else {
(.)
}
} %>%
{
if (input$EFG_NAME != "ALL EFG") {
filter(., EFG_NAME == input$EFG_NAME)
} else {
(.)
}
} %>%
group_by(SEASON, TAXON_ID, COMMON_NAME) %>%
summarise(sumRA = sum(sumRA, na.rm = T), .groups = "drop")
rv$baslineText <- ifelse(length(input$BASELINE[1]:input$BASELINE[2]) == 1, as.character(input$BASELINE[1]), paste(input$BASELINE[1], "to", input$BASELINE[2]))
baselines <- sppSumm %>%
dplyr::filter(SEASON %in% input$BASELINE[1]:input$BASELINE[2]) %>%
group_by(TAXON_ID) %>%
summarise(baseline = mean(sumRA))
rv$sppSumm <- sppSumm %>%
left_join(baselines) %>%
left_join(TaxonList) %>%
mutate(deltaRA = sumRA / baseline) %>%
mutate(belowThresh = deltaRA < CombThreshold)
# Calculation of number of species declining below thresholds--------------------
# sort order for Conservation_Status columns
belowThrshSummNames <- c(
"Critically endangered", "Endangered", "Vulnerable",
"Near threatened", "Data deficient", "Least Concern", "Total"
)
# table of number below threshold for each SEASON by Conservation_Status and total
belowThreshSummLong <- rv$sppSumm %>%
group_by(SEASON, VIC_ADVISORY_STATUS) %>%
summarise(nBelowThresh = sum(belowThresh, na.rm = T)) %>%
# rbind(rv$sppSumm %>%
# group_by(SEASON) %>%
# summarise(nBelowThresh = sum(belowThresh,na.rm=T)) %>%
# mutate(VIC_ADVISORY_STATUS = "Total")) %>%
mutate(VIC_ADVISORY_STATUS = factor(VIC_ADVISORY_STATUS, levels = belowThrshSummNames)) %>%
rename(Conservation_Status = VIC_ADVISORY_STATUS)
belowThreshSumm <- belowThreshSummLong %>%
pivot_wider(
names_from = "Conservation_Status",
values_from = nBelowThresh, names_sort = TRUE
)
# filtering just the last season for total decreasing and biggest increases and decreases on summary---
finalSeason <- max(levels(belowThreshSumm$SEASON))
finalSeasonBelowThreshold <- belowThreshSumm %>%
filter(SEASON %in% finalSeason) %>%
select(-SEASON)
biggestIncreasers <- rv$sppSumm %>%
filter(SEASON %in% finalSeason) %>%
slice_max(order_by = deltaRA, n = 5) %>%
select(COMMON_NAME, deltaRA) %>%
filter(deltaRA > 1)
biggestDecreasers <- rv$sppSumm %>%
filter(SEASON %in% finalSeason) %>%
slice_min(order_by = deltaRA, n = 5) %>%
select(COMMON_NAME, deltaRA) %>%
filter(deltaRA < 1)
output$finalSeasonBelowThreshold <- renderTable(finalSeasonBelowThreshold)
output$finalSeasonIncreasers <- renderTable(biggestIncreasers)
output$finalSeasonDecreasers <- renderTable(biggestDecreasers)
rm(baselines)
rv$spChoices <- sort(unique(rv$sppSumm$COMMON_NAME))
# delta RA plot ----
deltaRAPlot <- rv$sppSumm %>%
filter(COMMON_NAME %in% input$raSpChoices) %>%
ggplot() +
geom_line(aes(x = SEASON, y = deltaRA, group = COMMON_NAME, color = COMMON_NAME)) +
theme(
axis.text.x = element_text(
angle = 90,
vjust = 0.5,
hjust = 1
),
text = element_text(size = 16)
)
if (length(input$raSpChoices>0)){
output$deltaRAPlot <- renderPlot(deltaRAPlot)
output$deltaRAPlotText<-renderText(NULL)
}else{
output$deltaRAPlot <- renderPlot(NULL)
output$deltaRAPlotText<-renderText("Select species to show trends")
}
# GMRA calculation ----
gma <-
rv$sppSumm %>%
# drop_na() %>%
group_by(SEASON) %>%
summarise(GMRA = geoMean(deltaRA)) %>%
ungroup()
rv$gma <- gma
if (nrow(rv$gma) > 0) {
output$gmaTitle <- renderText(paste(
"Geometric mean of species' abundances (GMRA)\nrelative to baseline of",
rv$baslineText,
"\n and count of species below threshold abundance\n"
))
# gmaPlot <-ggplot()+
# ggtitle(label = gmaTitle)+
# xlab("Fire Year")+
# #ylab("Geometric mean of species' relative abundances")+
#
# geom_line(data=rv$gma,aes(x=SEASON,y=GMRA, group = 1))+
# geom_col(data =belowThreshSummLong %>% filter(Conservation_Status!="Total"),aes(x=SEASON,y=nBelowThresh,fill=Conservation_Status))+
# theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),text = element_text(size=16))
# summary page plot of GMRA and number of species below threshold
#GMRA and count of number os species declining plot ----
gmaPlot <- plot_ly(data = belowThreshSummLong) %>%
add_bars(
data = belowThreshSummLong,
x ~ SEASON,
y = ~nBelowThresh,
color = ~Conservation_Status,
yaxis = "y"
) %>%
add_lines(
data = gma,
x = ~SEASON,
y = ~GMRA,
name = "GMRA",
type = "scatter",
yaxis = "y2"
) %>%
layout(
barmode = "stack",
yaxis = list(title = "Number species below threshold",rangemode="tozero"),
yaxis2 = list(overlaying = "y", side = "right", automargin = T, title = "GMRA"),
xaxis = list(title = "Fire Year")
)
output$gmaPlot <- renderPlotly({
gmaPlot
})
output$gmaMessage <- renderText(NULL)
} else {
output$gmaPlot <- renderPlotly({NULL})
output$gmaMessage <- renderText("There are no data to calculate a GMRA for this selection")
}
})
observeEvent(rv$sppSumm, {
updateSelectInput(session, "raSpChoices", choices = rv$spChoices)
})
}