Skip to content

Commit 4733928

Browse files
committed
use custom layer filters for rehab map
allows for the layer selection to control the popup used for the towns as well as the presented raster and centre icons.
1 parent 52cb829 commit 4733928

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

server.R

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,13 @@ function(input, output, session) {
259259
options = leafletOptions(pane = "maplabels"),
260260
group = "map labels") %>%
261261
hideGroup(names(rehab_tiers)) %>%
262+
hideGroup(paste0("centres_", names(rehab_tiers))) %>%
263+
hideGroup(paste0("towns_", names(rehab_tiers))) %>%
262264
addCircleMarkers(
263-
lng=df_locations$x, lat=df_locations$y,
265+
lng=df_rehab_map_locations$x, lat=df_rehab_map_locations$y,
264266
radius=2, fillOpacity=0,
265-
popup=df_locations$popup_rehab,
266-
group="Towns",
267+
popup=df_rehab_map_locations$popup_none,
268+
group="towns_None",
267269
options=leafletOptions(pane="markers")
268270
) %>%
269271
addLegendNumeric(
@@ -274,26 +276,47 @@ function(input, output, session) {
274276
bins=10,
275277
value=c(-0.01, 0:20, 20.1),
276278
htmltools::tagList(tags$div("Time to care (hours)"), tags$br())
277-
) %>%
278-
addLayersControl(
279-
position = "topright",
280-
baseGroups = c("None", names(rehab_tiers)),
281-
overlayGroups = c("Towns"),
282-
options = layersControlOptions(collapsed = FALSE))%>%
283-
htmlwidgets::onRender("
284-
function() {
285-
$('.leaflet-control-layers-list').prepend('<label style=\"text-align:center\">Markers</label>');
286-
}
287-
")
279+
)
288280
rvs$map_rehab
289281
})
290282

283+
observeEvent(list(input$rehab_layer_selection, input$rehab_towns_checkbox), {
284+
rehab_base_groups <- c("None", names(rehab_tiers))
285+
rehab_towns_groups <- paste0("towns_", rehab_base_groups)
286+
287+
hide_base_groups <- rehab_base_groups[rehab_base_groups != input$rehab_layer_selection]
288+
show_base_group <- input$rehab_layer_selection
289+
290+
hide_centre_groups <- paste0("centres_", hide_base_groups)
291+
show_centre_group <- paste0("centres_", show_base_group)
292+
293+
if(is.null(input$rehab_towns_checkbox)) {
294+
show_towns_group <- c()
295+
hide_towns_groups <- rehab_towns_groups
296+
} else {
297+
show_towns_group <- rehab_towns_groups[which(rehab_base_groups == show_base_group)]
298+
hide_towns_groups <- rehab_towns_groups[rehab_towns_groups != show_towns_group]
299+
}
300+
301+
show_ids <- c(show_base_group, show_centre_group, show_towns_group)
302+
hide_ids <- c(hide_base_groups, hide_centre_groups, hide_towns_groups)
303+
304+
leafletProxy("map_rehab") %>%
305+
showGroup(show_ids) %>%
306+
hideGroup(hide_ids)
307+
})
308+
291309
observeEvent(rvs$to_load_rehab, {
292310
req(rvs$map_rehab)
293311
if(is.null(isolate(rvs$map_rehab)) | isolate(rvs$map_rehab_complete))return()
294312
for(group_name in names(rehab_tiers)){
295313
new_layer <- readRDS(file.path(layers_dir, glue::glue("{rehab_tiers[[group_name]]$file}.rds")))
296314
centres_group <- df_centres[df_centres$centre_name %in% rehab_tiers[[group_name]]$centres, ]
315+
316+
popup_col <- paste0("popup_",tolower(group_name))
317+
popup_col <- str_replace_all(popup_col, " ", "_")
318+
town_popups <- df_rehab_map_locations %>% pull(!!{popup_col})
319+
297320
leafletProxy("map_rehab") %>%
298321
addRasterImage(
299322
data=new_layer,
@@ -306,7 +329,14 @@ function(input, output, session) {
306329
lng=centres_group$x, lat=centres_group$y,
307330
icon=tier_icons[group_name],
308331
popup=centres_group$popup,
309-
group=group_name,
332+
group=paste0("centres_", group_name),
333+
options=leafletOptions(pane="markers")
334+
) %>%
335+
addCircleMarkers(
336+
lng=df_rehab_map_locations$x, lat=df_rehab_map_locations$y,
337+
radius=2, fillOpacity=0,
338+
popup=town_popups,
339+
group=paste0("towns_", group_name),
310340
options=leafletOptions(pane="markers")
311341
)
312342
}

0 commit comments

Comments
 (0)