Hi,
I'm making my first shiny app using leaflet and would like to get some advice on how to use the leafletProxy function correctly/efficiently to make changes in the map. In summary, I'm loading a large SpatialPolygonsDataFrame (a few thousand polygons) the first time I call leaflet in the server. Based on user inputs, the app should (1) apply a different style to a specific group of polygons (e.g. to countries of a particular continent), or (2) hide a specific polygon (e.g., the limit of a country) and display a more detailed SPDF with multiple polygons in its place (e.g., the states of that country).
For (1), my plan is to break the initial SPDF by groups (e.g. continents) and addPolygons() with a loop, defining a different name for each group using the "group" parameter. For changing the style of the polygons of a given group (e.g. change the fill color based on an attribute in its data frame), I could hide the original group using hideGroup() and add that same group with addPolygons(), but using the modified style. When that group is deselected by the user, I can clear the modified version with clearGroup() and show the original version again using showGroup(). Is this a good approach? Or is there a better/faster way of changing the style of a particular group of polygons?
For (2), the idea is the same. But in addition to specifying a "group" when I first add the polygons, looks like I'd need to specify a "layerId" for each polygon in that group in order to be able to remove any single polygon later using removeShape(). When that option for "showing the states of the country" is deselected, I could remove the new SPDF with clearGroup() and get the original polygon (the country limit) back in using addPolygons, specifying the original "group". I wonder if there is an equivalent of showGroup/hideGroup for showing/hiding a specific layerID. If so, I'm guessing that would be faster than removing/adding polygons every time.
Sorry for the long post and for not providing a reproducible example. I guess this is more of a conceptual question for understanding the best way of dealing with multiple layers in the context of leaflet/shiny... but I'd be happy to come up with a simple example if needed.
Many thanks,
Fabio