I would like if there is a simple solution to filter a SpatialPolygonDataFrame in R. Imagine I have many polygons but I only want to select some of them to plot them using leaflet
My data comes from data.gouv.fr
Here is my gist for loading the data into R.
If I want to draw only one polygon in leaflet
, I filter the @data
part of my SpatialPolygonDataFrame, get the id
> ign_shape_iris@data %>%
+ filter(DCOMIRIS == "606120301")
DEPCOM NOM_COM IRIS DCOMIRIS NOM_IRIS TYP_IRIS id
1 60612 Senlis 0301 606120301 Vald'Aunette-Gateliere H 790
Then I plot only the polygon I want :
leaflet(ign_shape_iris@polygons[[790]]) %>%
addTiles() %>%
addPolygons()
I'm sure there is a better solution.
Try treating it as a usual data.frame, and think subset instead of filter:
ign_shape_iris[ign_shape_iris$DCOMIRIS == "606120301",]
or
subset(ign_shape_iris, DCOMIRIS == "606120301")