trafficonese / leaflet.extras

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras/
GNU General Public License v3.0
214 stars 74 forks source link

addSearchFeatures() issue in shiny - popup not working #104

Closed cegbuna closed 5 months ago

cegbuna commented 7 years ago

After searching for a marker using the addSearchFeature() function in a leaflet map in shiny, the popup argument in the addCircleMarker() function doens't fire. This isn't an issue in rstudio. Curious to know if you can replicate and look into the issue. I used the data and code from your tutorial page below to replicate issue

Thank you

http://rpubs.com/bhaskarvk/leaflet-search

library(shiny)
library(leaflet.extras)

cities <- read.csv(textConnection("
City,Lat,Long,Pop
Boston,42.3601,-71.0589,645966
Hartford,41.7627,-72.6743,125017
New York City,40.7127,-74.0059,8406000
Philadelphia,39.9500,-75.1667,1553000
Pittsburgh,40.4397,-79.9764,305841
Providence,41.8236,-71.4222,177994
"))

leaf_search <- leaflet(cities) %>% 
  addProviderTiles(providers$OpenStreetMap) %>%
  addCircleMarkers(lng = ~Long, lat = ~Lat, weight = 1, fillOpacity=0.5,
                   radius = ~sqrt(Pop)/50 , popup = ~City, label=~City, group ='cities') %>%
  addResetMapButton() %>%
  addSearchFeatures(
    targetGroups = 'cities',
    options = searchFeaturesOptions(
      zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
      autoCollapse = TRUE, hideMarkerOnCollapse = TRUE )) %>%
  addControl(
    "<P><B>Hint!</B> Search for ...<br/><ul><li>New York</li><li>Boston</li><li>Hartford</li><li>Philadelphia</li><li>Pittsburgh</li><li>Providence</li></ul></P>",
    position='bottomright')

ui <- fluidPage(
  leafletOutput("map1", width = "100%", height = 500)
)

server <- function(input, output, session) {
   output$map1 <- renderLeaflet({
    leaf_search
  })
}

shinyApp(ui, server)
bhaskarvk commented 7 years ago

Thanks for reporting , I'll check it out.

bhaskarvk commented 7 years ago

btw did you use the CRAN version or the github version. I had made some fixes to Shiny part of search in the GH version.

cegbuna commented 7 years ago

I used the github version 0.2.9002 of the ‘leaflet.extras’ package

cegbuna commented 6 years ago

Hi bhaskarvk, Have you gotten a chance to look into this issue? Thanks

richardlent commented 6 years ago

I've had a similar problem with addSearchFeature(). After searching for a marker in a leaflet map inside of a Shiny dashboard, the leaflet click event for the found marker, and all other markers, no longer works.

daattali commented 6 years ago

@bhaskarvk I'm reporting a similar issue. After performing a search, the "marker_click" event does not get fired anymore. Reproducible example:

library(shiny)
library(leaflet)
library(leaflet.extras)

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

server <- function(input, output, session) {
  data <- data.frame(lat = 20:22, lng = 30:32, name = c("first", "second", "third"))

  output$map <- renderLeaflet({
    leaflet(data) %>%
      addTiles() %>%
      addMarkers(lng = ~lng, lat = ~lat, label = ~name, group = "names") %>%
      addSearchFeatures(targetGroups = "names")
  })

  observe({
    input$map_marker_click
    cat('clicked\n')
  })
}

shinyApp(ui, server)
daattali commented 6 years ago

@bhaskarvk I noticed that I actually get an error in the JS console when hovering/clicking on a marker only after doing a search

image

daattali commented 6 years ago

@schloerke tagging for visibility of an old issue

schloerke commented 6 years ago

Somehow the layer feature is being added to the value object. The layer object is very recursive.

schloerke commented 6 years ago

@daattali Found it.

leaflet-search is poisoning the concept of the latlng object of the marker by adding the layer attribute. I think it will be safer to have leaflet.R fix this issue than to rely on 3rd party packages behaving properly.

Adds the .layer property (any the many other lines like it): https://github.com/stefanocudini/leaflet-search/blob/eee9e4825a4e474961b7d48049d4621785a9fabb/src/leaflet-search.js#L539

leaflet-search is causing me more issues than any other package. :-/

Will comment when I have made a patch

daattali commented 6 years ago

Thanks @schloerke , I can confirm my sample code now works for me

daattali commented 6 years ago

But another bug is now happening! I'll report it in the PR

daattali commented 6 years ago

Works for me :) Thanks, I hope JS error report helped!

tobiasdirksen commented 5 years ago

Has this been fixed in leaflet?