Click on leaflet marker and get info

Hello,
I am making a Shiny/leaflet map whereby when the user clicks on a circle on a leaflet-r map, I would like some attributes of that circle to be displayed below the map. I got as far as the map and the circles, but the info upon clicking in NULL.

# Click on circle and get info

library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("mymap"),
  fluidRow(verbatimTextOutput("map_marker_click"))
)

server <- function(input, output, session) {

  # Create tree geometries
  tree_1g <- st_point(c(-79.2918671415814, 43.6760766531298))
  tree_2g <- st_point(c(-79.4883669334101, 43.6653747165064))
  tree_3g <- st_point(c(-79.2964680812039, 43.7134458013647))
                                        
  # Create sfc object with multiple sfg objects
  points_sfc <- st_sfc(tree_1g, tree_2g, tree_3g, crs = 4326)
  
  # Create tree attributes
  data <- data.frame (
    layerId = c("001", "002", "003"),
    address = c(10, 20, 30),
    street = c("first", "second", "third"),
    tname = c("oak", "elm", "birch"),
    latitude = c(43.6760766531298, 43.6653747165064, 43.7134458013647),
    longitude = c(-79.2918671415814, -79.4883669334101, -79.2964680812039)  
)
  
  tree_data <- st_sf(data, geometry = points_sfc)
  
  output$mymap <- renderLeaflet({
    leaflet(data = tree_data) %>%
      addProviderTiles(providers$Stamen.Watercolor) %>%
      
      # Centre the map in the middle of Toronto
      setView(lng = -79.384293, 
              lat = 43.685, 
              zoom = 11) %>% 
      
      addCircleMarkers( ~ longitude, ~ latitude)
  })
  
  observeEvent(input$mymap_marker_click, { 
    p <- input$map_marker_click
    print(p)
  })
  

}

shinyApp(ui, server)

Hi! Welcome!

I think you've just got a typo in your event observer code. Try this:

  observeEvent(input$mymap_marker_click, { 
    p <- input$mymap_marker_click  # typo was on this line
    print(p)
  })

Also, I had to add library(sf) at the top of your app in order to make it run. Frequently restarting your R session can help with catching that sort of thing :smile:

2 Likes

Thank-you jcblum. You are correct. Now I can get lat, lng and .nonce. I am not sure what .nonce is. How do I obtain an attribute of this circle, its "street" for example.

Update: Got things working for now.

Glad you got it working--FYI, the way we envision you doing this is by including the layerId in your addCircleMarkers call (in this case, layerId = ~layerId), and that ID will be passed back in input$map_marker_click$id. See http://rstudio.github.io/leaflet/shiny.html#inputsevents

2 Likes

Hi @ixodid. How did you obtain the attributes of the markers on click?

By observing the input$map_marker_click event; it will return both the x and y coordinates of a marker click, plus - and that can be extremely helpful - the unique layer_id of the marker, if one was assigned during the addMarkers() call.

For an example consider this post: Shiny: Using multiple exclusive reactiveValues for filtering dataset

It is built on top of the North Carolina shapefile, and can be run out of the box.