Clickable Custom (topo/geojson) polygons in leaflet?

498 views
Skip to first unread message

Nathan Robins

unread,
Sep 15, 2015, 11:55:52 PM9/15/15
to Shiny - Web Framework for R
Hi all

I am relatively new to Shiny (and to be honest my R is a bit rusty but coming back quickly) and I am trying to come up to speed with leaflet integrated in R. I think I have an okay grasp of how much of it works but I have been struggling for days with one major hurdle:

I have custom regions I want to integrate as clickable polygons that would then select what data to show in a side box. I have been able to bring in the GeoJSON data as polygons, using addGeoJson(), but I cannot figure out how to identify the individual regions or even reference the other attributes in the geoJson data once the data is added to a leaflet map.

Ideally I would like to color the polygons based on one of the attributes in the data, but I have likewise failed to figure that out.

Thanks for any guidance!

TL/DR:
How can I access data attributes for custom GeoJSON polygons and use them to manipulate the map or another shiny output object? 

Joe Cheng

unread,
Sep 16, 2015, 12:05:20 AM9/16/15
to Nathan Robins, Shiny - Web Framework for R
I think the doc page at http://rstudio.github.io/leaflet/json.html covers your options. Either use rgdal to convert to sp shapes, or modify the GeoJSON to contain properties and styles that you want. When you get a click event back (see http://rstudio.github.io/leaflet/shiny.html#inputsevents) on a GeoJSON object, you'll see a "properties" property that will contain whatever properties were on the feature. So if you add a unique identifier or name as a property on your GeoJSON shapes, then you'll have it accessible there.

--
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/f33cb4bd-f8a4-41a1-83b5-a8ba3e14de3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathan Robins

unread,
Sep 16, 2015, 11:32:10 AM9/16/15
to Shiny - Web Framework for R, nathan...@gmail.com
Thank you so much - I'd seen those before but something wasn't clicking as to how to access the properties associated with the json objects.

My other challenge is probably related to the styles - I want to add in a time slider that will cause the polygons to recolor based on some property in the data associated with times on the slider - any good references for guidance there?

Thanks again!

Joe Cheng

unread,
Sep 18, 2015, 2:51:25 PM9/18/15
to Nathan Robins, Shiny - Web Framework for R
Currently this is something you would do by moving the polygon drawing code into an observer with leafletProxy (leaving the initial map setup, setView, etc in a renderLeaflet). Read http://rstudio.github.io/leaflet/shiny.html carefully to learn more. It's a bit subtle but very powerful.

Your next question may be how to do all of this on the client side so a round-trip to the server is not required. That is currently not possible without writing a bunch of custom JavaScript, sorry.

Nathan Robins

unread,
Sep 21, 2015, 3:31:16 PM9/21/15
to Shiny - Web Framework for R, nathan...@gmail.com
Thanks again - I think my struggle is actually a few steps back from that even (that much makes sense) - I am having trouble figuring out how to style the individual GeoJSON polygons when they are drawn, I can only color all of them at once, I am missing the bit of knowledge required to style them individually.
Reply all
Reply to author
Forward
0 new messages