Is there a function hidden somewhere to retrieve all the the layerIds or Groups currently available on a leaflet map? I'm pretty sure this would be fairly easy using the javascript interface, but I haven't found a solution using the R leaflet API.
The context is a shiny app where layers can be added, successively. I was debating maintaining a reactiveValues and update it with each group/layerId when appropriate, but it ends up being a bunch of extra coding/observers. Is there a function or simple way to get the groups or layerIds, or even better both?
If you have a map
m <- leaflet() %>% addTiles() %>%
addPopups(-122.327298, 47.597131, content, layerId = "my layer id",
options = popupOptions(closeButton = TRUE)
)
Then you can get the layerId from
m$x$calls[[2]]$args[[4]]
[1] "my layer id"
For a more general map m with multiple layerIds and groups introduced with multiple methods (such as addMarkers, addCircles, etc) try using
l <- lapply(lapply(m$x$calls, function(x) x[[2]]), function(x) x[4][[1]])
layerIds <- unlist(l[sapply(l, function(x) {class(x) != "list" & !is.null(x)})])
g <- lapply(lapply(m$x$calls, function(x) x[[2]]), function(x) x[5][[1]])
groupIds <- unlist(g[sapply(g, function(x) {class(x) != "list" & !is.null(x)})])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With