rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
805 stars 509 forks source link

input$map_click fires when clicking on Circle shape #764

Open pmallot opened 2 years ago

pmallot commented 2 years ago

When clicking on a shape both a map_shape_clickand a map_click event are fired. I.e. the click is not consumed by the map_shape_click. I would expect the click event to be consumed by the map_shape_click. If there is no observer for this event then it might be beneficial to fire a map_click event instead. My use case was being able to add circles to the map by clicking on it and removing circles by clicking on them. While clicking on the circles to remove them works, the click also triggering on the map also meant a new circle was automatically created in the same spot. I circumvented this by comparing the positions, but that is not a clean solution.

Reprex:

library(shiny)
library(leaflet)

ui <- fluidPage(
    leafletOutput("map")
)

server <- function(input, output) {
    output$map <- renderLeaflet({
      leaflet() %>% addTiles() %>% addCircles(lat=37.233333, lng=-115.808333, label="clickMe")
    })

    observeEvent(input$map_shape_click, {
      print("Shape click")
    })

    observeEvent(input$map_click,  {
      print("Map click")
    })
}

shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:6301

Created on 2021-11-02 by the reprex package (v2.0.0)

daattali commented 3 weeks ago

Did you ever find a solution?

pmallot commented 3 weeks ago

No, the workaround did it's job well enough. Clicks on shapes are dealt with as intended and clicks on the map are discarded if input$map_shape_click was also triggered with the same lng/lat.

daattali commented 3 weeks ago

Using freezeReactiveValue(input, 'map_click') seems to work for me, but I imagine that only works if I can guarantee that the shape click will always fire prior to the map click.