Closed jeroenclaes closed 6 years ago
I believe the documentation is misleading in that we can not add more than one element with the same layerId
. If you were to retrieve the leaflet.js layer afterwards, it would only contain the last group that was added.
I believe you're looking for just the group
argument. If this already uniquely defines the polygon group, there is no need for the layerId
argument.
Leaflet.R should have a warning when a multiple polygons are supplied with a single layer id (vectors are allowed).
The minimal example below displays the behavior you've experienced and also using only the group argument.
Please let me know if I've missed your original Issue intent.
- Barret
library(leaflet)
states <- geojsonio::geojson_read("https://rstudio.github.io/leaflet/json/us-states.geojson", what = "sp")
class(states)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
# works
leaflet(states) %>%
addTiles() %>%
addPolygons()
# displays last polygon only
leaflet(states) %>%
addTiles() %>%
addPolygons(layerId = "myLayer")
# works
leaflet(states) %>%
addTiles() %>%
addPolygons(group = "myGroup")
Created on 2018-08-07 by the reprex package (v0.2.0).
Oh, now I see thanks for the clarification!
I'm using the github versions of both leaflet and leaflet.extras
I have the following leaflet code:
Based on the area selected with the drawing of leaflet.draw, my code selects polygons /point geometries from different shapefiles (propietary data, cannot share), represented in R as SF data.frames. I can see that the code selects multiple rows (as it should), because the selections have 10+ rows, but only the first row of each data.frame is showing up on the map when I use a leaflet Proxy (and for-loop, for the polygons) to add them to the map:
I've found a similar issue on stackoverflow (https://stackoverflow.com/questions/51275767/only-the-first-polygon-is-showing-up-in-leaflet-2-0-1-choropleth-plot), where the suggested solution is to add "dashArray = NULL" to the addPolygons call, but this does not solve the problem.
In my tests, the issue goes away when I omit the layerId argument. This is the case even for simpler leaflet maps, such as