Leaflet Zoom Observer

289 views
Skip to first unread message

Charles Youds

unread,
Jun 9, 2016, 2:42:24 PM6/9/16
to Shiny - Web Framework for R
Hey guys! I'm writing an RShiny Leaflet app (using MLS real-estate data), but I'm having trouble detecting a zoom event on the map. Here's what I have so far:

myMap <- reactive({
    return(
      leaflet(mls) %>%
        addControlFullScreen() %>%
        addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>%
        addDrawToolbar(polyline = FALSE, marker = FALSE) %>%
        addCircleMarkers(
          group = "marker",
          label =  ~ mls$address,
          lat = ~ geo.latitude,
          lng = ~ geo.longitude,
          stroke = FALSE,
          fillOpacity = 0.5,
          color = "coral",
          radius = 5
        ) %>%
        addSearchMarker("marker", position = "topleft", propertyName = "label") %>%
        addControlGPS()
    )
 })

  output$mymap <- renderLeaflet({
    myMap()
  })
  
  # A reactive expression that returns the set of mls locations that are in bounds right now
  mlsInBounds <- reactive({
    if (is.null(input$mymap_bounds))
      return(mls[FALSE, ])
    bounds <- input$mymap_bounds
    latRng <- range(bounds$north, bounds$south)
    lngRng <- range(bounds$east, bounds$west)
    subset(
      mls,
      geo.latitude >= latRng[1] & geo.latitude <= latRng[2] &
      geo.longitude >= lngRng[1] & geo.longitude <= lngRng[2]
    )
  })
  
  output$scatterMlsMap <- renderPlot({
    # If no listings are in view, don't plot
    if (nrow(mlsInBounds()) == 0)
      return(NULL)
    print(
      xyplot(
        floor.area.grand.total ~ list.price,
        data = mlsInBounds(),
        xlim = range(mls$floor.area.grand.total),
        ylim = range(mls$list.price)
      )
    )
  })

This is the relevant code. When I call

observeEvent(input$mymap_bounds,{ print("bounds changed") })

it only gets called when the map is opened. The goal is to have a scatterplot that gets generated based on the MLS listings that are within bounds.

Joe Cheng

unread,
Jun 9, 2016, 3:05:00 PM6/9/16
to Charles Youds, Shiny - Web Framework for R
That should work. Do you see any errors in your browser's JavaScript console?
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/adaa02f9-6dc8-45c3-90a2-7f018c7e809f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charles Youds

unread,
Jun 9, 2016, 3:15:29 PM6/9/16
to Shiny - Web Framework for R, charle...@gmail.com

On Thursday, June 9, 2016 at 12:05:00 PM UTC-7, Joe Cheng [RStudio] wrote:
That should work. Do you see any errors in your browser's JavaScript console?

Sadly no errors! It prints my output: "bounds changed" twice, and that's it. :(

Joe Cheng

unread,
Jun 9, 2016, 5:41:16 PM6/9/16
to Charles Youds, Shiny - Web Framework for R
Hmm, I don't know. The following works. Can you try narrowing it to a minimal reproducible example (as well as indicating the branch you used to install leaflet)? Thanks...

library(shiny)
library(leaflet)

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

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>% addTiles()
  })
  
  observeEvent(input$mymap_bounds, {
    print("Bounds changed")
  })
}

shinyApp(ui, server)

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Charles Youds

unread,
Jun 9, 2016, 7:20:18 PM6/9/16
to Shiny - Web Framework for R, charle...@gmail.com
I should have done that earlier. I apologize.

I ran the code snippet you posted as its own separate file, and zooming still didn't change the bounds.

Tried installing a fresh version of Leaflet, and that fixed it. Previously, I was using ("ssoulier/leaflet") and I guess it's out of date. Thank you very much! :D
Reply all
Reply to author
Forward
0 new messages