leaflet icon indicating copy to clipboard operation
leaflet copied to clipboard

addPolygons() now responds to crosstalk events

Open cpsievert opened this issue 7 years ago • 17 comments

For example,

library(plotly)
library(leaflet)
library(sf)

nc <- system.file("shape/nc.shp", package = "sf") %>%
  st_read() %>% 
  st_transform(4326) %>%
  highlight_key()

map <- leaflet(nc) %>%
  addTiles() %>%
  addPolygons(
    opacity = 1,
    color = 'white',
    weight = .25,
    fillOpacity = .5,
    fillColor = 'blue',
    smoothFactor = 0
  )

p <- plot_ly(nc) %>% 
  add_markers(x = ~BIR74, y = ~SID79) %>%
  layout(dragmode = "lasso") %>%
  highlight("plotly_selected")

crosstalk::bscols(map, p)

leaf

TODO:

  • [ ] Should 2D brush events on polygons emit a crosstalk event? I'm leaning towards yes, but I'm not immediately sure how to implement. If not, the brush select icon/mode should be removed to avoid confusion.
  • [ ] Implement the same for addPolylines() etc?
  • [ ] Tests?

Any feedback/input is greatly appreciated :)

cpsievert avatar Feb 27 '17 22:02 cpsievert

Nice! So in theory this should work for all shapes like rectangles, circles, circleMarkers, polylines, right ? provided code for each of these shapes is changed accordingly.

bhaskarvk avatar Feb 28 '17 00:02 bhaskarvk

I was originally thinking polygon brushing would be really hard to implement, because it'd either have to work by 1) any intersection between the brushing rectangle and the polygon, or 2) complete coverage of the polygon by the brushing rectangle.

But a nice easier-to-implement alternative would be 3) the brushing rectangle includes the center-of-mass of the polygon (is that the right term for it?). I believe there are fast routines to do this in R?

jcheng5 avatar Feb 28 '17 07:02 jcheng5

I mean, obviously the correct term is "centroid" not center-of-mass, and obviously it's just the arithmetic mean of all of the points in the polygon. :)

jcheng5 avatar Feb 28 '17 07:02 jcheng5

Yea, that sounds reasonable, and maybe we could leverage sf::st_centroid()?

cpsievert avatar Feb 28 '17 16:02 cpsievert

For irregular polygons centroids can fall out of the polygon/s. You may also want to evaluate https://www.mapbox.com/blog/polygon-center/. It is a JS solution, so in theory it should be easy to integrate.

bhaskarvk avatar Feb 28 '17 16:02 bhaskarvk

The mapbox solution is really interesting. I can see how it would be much better than centroid for labeling but I'd want to try out both for brushing to see how they feel.

jcheng5 avatar Feb 28 '17 17:02 jcheng5

Yup agreed.

bhaskarvk avatar Feb 28 '17 17:02 bhaskarvk

We can decompose to triangles for finer elements, as illustrated in my sfdct package. License is a problem but in time we might get CGAL or another alternative.

mdsumner avatar Mar 13 '17 10:03 mdsumner

Also fwiw this is what I was going after here: https://twitter.com/mdsumner/status/830380049815597056

mdsumner avatar Mar 13 '17 10:03 mdsumner

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Oct 02 '19 15:10 CLAassistant

We could make very good use of this pull request. Is there anything we can help with so that it can be accepted?

krlmlr avatar Oct 19 '20 16:10 krlmlr

@krlmlr this PR only implements the ability for leaflet polygons to respond to crosstalk events. I'm pretty sure @jcheng5 hesitated to take this on initially because ideally, when this is added, you'd also have the ability to brush polygons to emit crosstalk events (which is what most of the comments are about). If you wanted to take a stab at implementing that, I'm pretty sure that'd increase the chances of this landing

cpsievert avatar Oct 19 '20 16:10 cpsievert

Yeah that would help a lot. Even “just” using the centroid algorithm.

jcheng5 avatar Oct 19 '20 16:10 jcheng5

FWIW, @timelyportfolio came up with this some time ago to enable this kind of thing. Maybe that could serve as a basis for implementation.

In action here: https://twitter.com/timelyportfolio/status/942519152731926529

tim-salabim avatar Oct 19 '20 17:10 tim-salabim

Responding to crosstalk events is good enough for our use case. I intend to respond to $map_shape_click events and update the selection manually via shared_data$selection() . I don't see the value of brushing polygons for our use case.

I think it would be useful to enable this for all other shape types, we could file an issue to implement brushing at some point if necessary. I haven't found a documentation of this limitation -- why we can crosstalk points but not polygons -- where can we document it?

krlmlr avatar Oct 19 '20 17:10 krlmlr

I'm also missing the ability to select/deselect individual objects in the crosstalk framework, this doesn't seem to work neither with leaflet nor with plotly. Has this been considered?

krlmlr avatar Oct 20 '20 11:10 krlmlr

Hello, I have found quite a few projects that would benefit from this. It works really well but it is not in the main rstudio/leaflet branch. Is there any reason there's a hangup in merging? I am happy to help if I can be pointed in the right direction.

sstoltzman-rclco avatar Nov 02 '21 14:11 sstoltzman-rclco