Using leafletProxy to make changes in layers and styles

472 views
Skip to first unread message

Fabio

unread,
Aug 11, 2017, 5:07:56 PM8/11/17
to Shiny - Web Framework for R
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

Joe Cheng

unread,
Aug 15, 2017, 8:19:22 AM8/15/17
to Fabio, Shiny - Web Framework for R
Yes, those both sound fine. Although depending on the opacity of your restyled/detailed polygons for both (1) and (2), you might not need to hide the original polygons at all--just let them be covered by the new polygon, since it'll be the same shape.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/17e4deae-6cfd-43a5-a0a3-109dc26872ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fabio

unread,
Aug 16, 2017, 10:56:15 AM8/16/17
to Shiny - Web Framework for R, fa...@canopyrss.tech
Thanks for your reply, Joe. I'm kind of forced to hide the original polygons because we display labels for both the original and the new, substitute polygons. Leaflet is displaying the labels of the polygons on the top by default. But I found that if you hover the mouse over a polygon limit, you can eventually highlight and activate the label of the polygon on the bottom... and that can't be easily undone.

Just to double-check... my understanding is that the only (easy) way of changing the polygon data being mapped to color is by adding those polygons again using leafletProxy and addPolygons. Is that correct?

Thanks again,
Fabio
Reply all
Reply to author
Forward
0 new messages