-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
276 lines (237 loc) · 7.64 KB
/
server.R
File metadata and controls
276 lines (237 loc) · 7.64 KB
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
# Define server logic
server <- function(input, output, session) {
################################################################################################
# RELOAD
observeEvent(input$reload_btn, {
session$reload()
})
################################################################################################
layers <- callModule(reactiveLayersModule, id = "reactiveLayersModule")
# tab and module-level reactives
module <- reactive({
input$tabs
})
reactiveValsList <- list(
insertedTabs = reactiveVal(c()),
subunit_names = reactiveVal(NULL),
selected_subunits = reactiveValues(selected = character()),
mapCache = reactiveVal(0),
sppListCache = reactiveVal(NULL),
sppDisplay = reactiveVal(NULL),
sppSelectCache = reactiveVal(NULL),
bcrCache = reactiveVal(NULL),
bcrPred = reactiveVal(NULL),
inserted_ids = reactiveVal(character()),
data_ready = reactiveVal(FALSE),
pop_module_out = reactiveVal(NULL),
band = reactiveVal(NULL),
sppOnMap = reactiveVal(NULL)
)
######################## #
### GUIDANCE TEXT ####
######################## #
# UI for component guidance text
output$gtext_module <- renderUI({
req(input$tabs)
file <- file.path('Rmd', glue("gtext_{input$tabs}.Rmd"))
if (!file.exists(file)) return()
includeMarkdown(file)
})
# Control right Panel
output$rightPanel <- renderUI({
if (input$tabs == "data") {
tabsetPanel(
tabPanel("Download",
bandUI("explore_module"),
sppUI("explore_module"),
dwdUI("explore_module")
)
)
} else if(input$tabs == "popstats"){
tabsetPanel(
tabPanel("Download",
br(),
br(),
sppUI("explore_module"),
dwdUI("explore_module")
)
)
}
})
observe({
if (input$tabs == "data") {
shinyjs::show("explore_module-band")
shinyjs::show("explore_module-bandDef")
shinyjs::show("explore_module-dwdNMoutput")
shinyjs::show("explore_module-speciesboxes")
} else if (input$tabs == "popstats") {
shinyjs::hide("explore_module-band")
shinyjs::show("explore_module-speciesboxes")
shinyjs::show("explore_module-dwdNMoutput")
shinyjs::show("explore_module-bandDef")
}
})
# Help Component
# help_modules <- c("data", "dist")
#lapply(help_modules, function(module) {
# btn_id <- paste0(module, "Help")
# observeEvent(input[[btn_id]], updateTabsetPanel(session, "main", "Module Guidance"))
#})
######################## #
### MAPPING LOGIC ####
######################## #
# Initialize Leaflet map centered on Canada
output$myMap <- renderLeaflet({
leaflet() %>%
addMapPane(name = "ground", zIndex=380) %>%
addMapPane(name = "overlay", zIndex=420) %>%
#add listener
htmlwidgets::onRender("
function(el, x) {
var map = this;
map.on('baselayerchange', function(e) {
Shiny.setInputValue('active_raster', e.name, {priority: 'event'});
});
}
") %>%
addProviderTiles("CartoDB.Positron", group="baseMap") %>%
leafem::addMouseCoordinates() %>%
# Fit bounds to Canada's extent
fitBounds(lng1 = -141.0, lat1 = 42, lng2 = -52.0, lat2 = 70) %>%
addLayersControl(position = "topright",
options = layersControlOptions(collapsed = FALSE))
})
# Create map proxy for updates
myMap <- leafletProxy("myMap", session)
########################## #
########################## #
### ACCESS THE DATA ###
########################## #
########################## #
# Provide species UI
observeEvent(input$tabs, {
req(input$tabs == "data")
callModule(
exploreSERVER, "explore_module",
spp_list = spp_tbl,
layers = layers,
myMap = myMap,
reactiveVals = reactiveValsList # Pass the entire list
)
})
#build legend on selected species
#observeEvent(input$active_raster, {
observe({
req(input$active_raster, reactiveValsList$band())
selected <- input$active_raster
req(selected)
#browser()
r_bird <- reactiveValsList$sppSelectCache()[[selected]]
band_index <- as.numeric(reactiveValsList$band())
r <- r_bird[[band_index]]
req(r)
# Dynamically set legend title
legend_title <- switch(reactiveValsList$band(),
"1" = "Mean Density (males/ha)",
"Variation in density")
#pal <- colorNumeric("YlGnBu", values(r), na.color = "transparent")
#rng <- range(values(r), na.rm = TRUE)
#breaks <- seq(rng[1], rng[2], length.out = 6)
# Option 1
#vals <- values(r)
#vals <- vals[!is.na(vals)]
#
# Define your custom palette
#my_colors <- c(
# '#f9ffaf', '#edef5c', '#bbdf5f', '#61c074',
# '#34af7c', '#008c80', '#007a7c', '#255668'
#)
## Compute range and breaks
#rng <- range(vals, na.rm = TRUE)
#breaks <- seq(rng[1], rng[2], length.out = length(my_colors) + 1)
#leafletProxy("myMap") %>%
# clearControls() %>%
# addLegend(
# colors = my_colors,
# labels = sprintf("%.4f", breaks[-1]),
# title = legend_title,
# position = "bottomright",
# opacity = 1
# )
# Option2
r[r ==0] <- NA
vals <- values(r)
rng_trim <- quantile(vals, probs = c(0.0025, 0.9975), na.rm = TRUE)
my_colors <- c(
'#f9ffaf', '#edef5c', '#bbdf5f', '#61c074',
'#34af7c', '#008c80', '#007a7c', '#255668'
)
pal <- colorBin(
palette = my_colors,
domain = vals,
bins = seq(rng_trim[1], rng_trim[2], length.out = length(my_colors) + 1),
na.color = "transparent"
)
leafletProxy("myMap") %>%
clearControls() %>%
addLegend(
pal = pal,
values = vals,
title = legend_title,
position = "bottomright",
opacity = 1,
labFormat = labelFormat(
digits = 4
)
)
})
################################################################################################
# Observe on tabs
################################################################################################
observe({
req(input$`pop_module-popAnalysis`)
if (input$`pop_module-popAnalysis` == "popArea") {
# switch away if user tries to view Population table
updateTabsetPanel(session, "centerPanel", selected = "Species occurrence")
}else{
updateTabsetPanel(session, "centerPanel", selected = "Map View")
}
})
########################### #
########################### #
### POPULATION ESTIMATES ###
########################### #
########################### #
observeEvent(input$tabs, {
req(input$tabs == "popstats")
# Need to run getLayerNM first
if (!isTRUE(reactiveValsList$data_ready())) {
showModal(modalDialog(
title = "Data not ready",
"Please run the Model Access tool before using the Population Distribution tool",
easyClose = TRUE,
footer = modalButton("OK")
))
return(NULL) # stop here
}
callModule(
popSERVER, "pop_module",
layers = layers,
myMap = myMap,
reactiveVals = reactiveValsList # Pass the entire list
)
})
############################ #
############################ #
### PREDICTORS IMPORTANCE ###
############################ #
############################ #
observeEvent(input$tabs, {
req(input$tabs == "pred")
callModule(
predSERVER, "pred_module",
layers = layers,
reactiveVals = reactiveValsList # Pass the entire list
)
})
}