Get info from leaflet map widget object

512 views
Skip to first unread message

ArjunaCap

unread,
Mar 25, 2015, 5:26:58 PM3/25/15
to shiny-...@googlegroups.com
How can I obtain the map bounds from an application map currently in view?

Below is my (failing) MRE; it is supposed to display a table (at right) of cities currently in the map view (at left).

Incidentally, I attempted to use the code from Mr Cheng's very fine Superzip example, but I think this leaflet is the later htmlwidget version:

Thank you

library(shiny)
 library
(shinydashboard)
 library
(dplyr)
 library
(leaflet)
 
 ui
<- dashboardPage(
   dashboardHeader
(),
   dashboardSidebar
(),
   dashboardBody
(fluidRow(
                   box
(width = 6, status = "info",
                     title
= "Property Map",
                     leafletOutput
("myMap")),
                   
                   box
(width = 6, status = "info",
                       title
= "Properties",
                       tableOutput
("propsInView"))
                     
   
)
 
))
 
   
  my_data
<- data.frame(city = c("LA", "Chi", "Dallas", "NYC"), lat = c(34.1, 41.79, 32.82, 40.7), lng = c(-118,-87, -96.7, -74))
   
   server
<- function(input, output) {
     
     
## A reactive function to update as the user adjusts the map
     
# It was copied from the Superzip example
     
# It doesn't work
     propsInBounds
<- reactive({
       
if (is.null(input$map_bounds))
         
return(my_data[FALSE,])
       bounds
<- input$map_bounds
       latRng
<- range(bounds$north, bounds$south)
       lngRng
<- range(bounds$east, bounds$west)
       subset
(my_data,
              lat
>= latRng[1] & lat <= latRng[2] &
                lng
>= lngRng[1] & lng <= lngRng[2])
     
})
       
     
## The map ##
     output$myMap
<- renderLeaflet({
       leaflet
(my_data) %>% addTiles() %>% addCircleMarkers()
     
})
     
     
##The cities in view ##
     output$propsInView
<- renderTable({
       propsInBounds
() %>% select(city)
     
})
     
}
   
 shinyApp
(ui, server)


Joe Cheng

unread,
Mar 26, 2015, 11:03:20 AM3/26/15
to ArjunaCap, shiny-...@googlegroups.com

Instead of input$map_bounds, try input$myMap_bounds.

--
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/221861f0-378d-4644-9a73-bd49b2aee4ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Cawthon

unread,
Mar 26, 2015, 11:10:18 AM3/26/15
to Joe Cheng, shiny-...@googlegroups.com
That almost worked, and your timing was excellent as I was scouring the old shiny-leaflet package in github.

So I additionally needed to add id = 'myMap' when calling leaflet, as in:

 output$myMap <- renderLeaflet({
       leaflet
(my_data, id = 'myMap') %>% addTiles() %>% addCircleMarkers()
     
})

Thank you, Joe Cheng.
-- 

Michael Cawthon
Chief Investment Officer
Green Street Energy LLC
mcaw...@greenstenergy.com
p: 479-442-1407

Yihui Xie

unread,
Mar 26, 2015, 10:02:13 PM3/26/15
to Michael Cawthon, Joe Cheng, shiny-discuss
I think we have removed the "id" argument from the leaflet() function
in the new leaflet package. If you render a map in shiny, the id
prefix will be the output id, e.g. leafletOutput(outputId), then
input$outputId_bounds, input$outputId_something, ...

Regards,
Yihui

Joe Cheng

unread,
Mar 27, 2015, 1:12:23 AM3/27/15
to ArjunaCap, shiny-...@googlegroups.com

That shouldn't be needed, if it doesn't work without it it's a bug. The id on leafletOutput should be enough.

Carlos Sánchez

unread,
Mar 27, 2015, 11:52:36 AM3/27/15
to shiny-...@googlegroups.com, mcaw...@greenstenergy.com
Hi,

I don't think the new version of Leaflet is catching the events correctly. I have simplified Michael's code to just the Leaflet map:

library(shiny)
library
(shinydashboard)
library
(dplyr)
library
(leaflet)

ui
<- dashboardPage(
  dashboardHeader
(),
  dashboardSidebar
(),
  dashboardBody
(fluidRow(
    box
(width = 6, status = "info",
        title
= "Property Map",
        leafletOutput
("myMap"))

 
)

 
))

server
<- function(input, output) {

 
 
## The map ##

  output$myMap
<- renderLeaflet({
    leaflet
(my_data) %>% addTiles() %>% addCircleMarkers()
   
 
})

 
  observe
({
   
print(input$myMap_bounds)
   
print(input$myMap_shape_click)
   
})
}

shinyApp
(ui, server)

input$myMap_bounds works and prints out the limits of the map.

However, if I click on any of the circles nothing happens. I have also tried adding a marker but I could not get the event information for that either.

Carlos
Reply all
Reply to author
Forward
0 new messages