bbest / ohi-shiny2

simple OHI Shiny app, post #shinydevcon
0 stars 0 forks source link

leaflet: addTopoJSON() or addPolygons(), not both #1

Open bbest opened 8 years ago

bbest commented 8 years ago

Hi @FrissAnalytics, @jules32, @jafflerbach,

Good news! Herman's aster htmlwidget is now working interactively on hover with ohi-shiny2. Check it out at https://bdbest.shinyapps.io/ohi-shiny2/.

image

The not so great news is that it takes a few seconds to load or change layers. There is some structural inefficiencies from http://rstudio.github.io/leaflet/ not yet being a fully flexible implementation of http://leafletjs.com/. Ideally, the TopoJSON is loaded once, and individual polygons are simply recolored upon choosing a new layer from the OHI dropdowns, or highlighted upon hover, akin to the leaflet chloropleth example which implements a highlightFeature() function.

Still, the current functionality implemented by Herman works pretty well. You can try out his original locally with:

# install widget
devtools::install_github(
  'FrissAnalytics/ohi-aster', subdir='asterHTMLwidget')

# run shiny app
shiny::runGitHub(
  'FrissAnalytics/ohi-aster', subdir='OHI_SHINY3')

which looks like this:

image

Herman modeled this implementation after https://jcheng.shinyapps.io/choropleth3/, which looks like the original chloropleth:

image

Note the showcase mode allows you to see the code, using a "legacy" version of rstudio/leaflet and essentially drawing a new highlight polygon on mouseover and mouseout (ie hover) events.

Herman did something similar, with the rgns loaded as a SpatialPolygonsDataFrame and using addPolygons(). The interface is limited with input$map1_shape_mouseover$id usually being NULL on mouseover. The addTopoJSON() gives a much richer access to all input$map1_topojson_mouseover$properties (ie attributes of the original shape), but seems slow to load (should run profvis to figure out where exactly this happens).

Preferably some structural changes will be made to https://github.com/rstudio/leaflet for more flexibly enabling on-the-fly styling of polygons, whether for a new OHI layer, or on hover events. There's a (currently open) pull request Added removeFeatureGeoJSON, addFeatureGeoJSON, and styleFeatureGeoJSON by ideamotor that does something like this, but not as efficiently as possible.

We might also consider prepopulating the rgns with a popup per layer, similar to https://rpubs.com/jcheng/us-states-2, since the rstudio/leaflet popup feature works with addPolygons() for any type of shapefile read in and doesn't need seperate calls.

image

Feel free to edit this comment or add a new one to further document this issue.

More links:

jules32 commented 8 years ago

Hi @bbest,

This is so awesome that you've been able to get @FrissAnalytics's flower plot with the OHI map! I will have a look at the code tomorrow and see if I have any ideas.

Cheers, Julie

bbest commented 8 years ago

More on above from Herman @FrissAnalytics


On Fri, Feb 12, 2016 at 3:05 PM: Hi,

The highlightFeature/resetHighlight were exactly the things I tried, which didn’t work well. Then again, it looks pretty similar to what is described in the original leaflet site (i.e. the non R version), so I’m not sure why I didn’t get the performance I was hoping for.

I’m convinced there should be an alternative that is not that hard to implement, which has the speed of your original implementation.

If I have time this weekend I’ll make a HTMLWidget binding of the original choropleth example (on the leaflet.js site). That way you would have the look and speed of what you want. The obvious drawback would be that each feature you need must be implemented individually. That, however, is less work than you would think, especially if you know how to send messages from and to the server.

This article (https://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/) is the basis of many of the custom bindings that I have made myself.

If you get stuck or need advice, just send me an email and I see what I can do!

Have a great weekend, cheers H

bbest commented 8 years ago

If we end up doing anything with updating this functionality I would hope that it comes in the form of a pull request to update the core functionality of the rstudio/leaflet package, rather than "reinventing this wheel", so the OHI shiny app doesn't become obsolete with future updates.