Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all layerIds or groups from leaflet

Tags:

r

leaflet

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?

like image 475
Rorschach Avatar asked Oct 29 '25 20:10

Rorschach


1 Answers

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)})])
like image 60
Vlad Avatar answered Oct 31 '25 12:10

Vlad