Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addPolygons() now responds to crosstalk events #391

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cpsievert
Copy link
Contributor

@cpsievert cpsievert commented Feb 27, 2017

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 :)

@bhaskarvk
Copy link
Collaborator

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.

@jcheng5
Copy link
Member

jcheng5 commented Feb 28, 2017

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
Copy link
Member

jcheng5 commented Feb 28, 2017

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. :)

@cpsievert
Copy link
Contributor Author

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

@bhaskarvk
Copy link
Collaborator

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.

@jcheng5
Copy link
Member

jcheng5 commented Feb 28, 2017

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.

@bhaskarvk
Copy link
Collaborator

Yup agreed.

@mdsumner
Copy link

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
Copy link

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

@schloerke schloerke mentioned this pull request Feb 12, 2018
29 tasks
@CLAassistant
Copy link

CLAassistant commented Oct 2, 2019

CLA assistant check
All committers have signed the CLA.

@krlmlr
Copy link

krlmlr commented Oct 19, 2020

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

@cpsievert
Copy link
Contributor Author

cpsievert commented Oct 19, 2020

@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

@jcheng5
Copy link
Member

jcheng5 commented Oct 19, 2020

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

@tim-salabim
Copy link
Contributor

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

@krlmlr
Copy link

krlmlr commented Oct 19, 2020

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
Copy link

krlmlr commented Oct 20, 2020

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?

@sstoltzman-rclco
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants