I have a fairly simple leaflet project in RStudio


#load data/layers
m = leaflet() %>% {add polygons....}
saveWidget(m, file="HTML_File.html", selfcontained = F)

The resulting HTML page displays nicely, but I would want to respond to a click event by adding a pop-up that provides the lat / lon of the point clicked, as well as the attribute of the top-most layer.

I have no idea how to go about it. I noticed that the leaflet.js script that gets exported has code to handle double clicks. I tried to hack that code to provide an alert, along the lines of

alert("Lat, Lon : " + e.latlng.lat + ", " + e.latlng.lng)   

but clearly I don't know what I'm doing.

  • 1
    I agree pajonk's in that Shiny is the best solution for your issue, but if you're happy (to start with) with displaying coordinates on hover, the mapview package offers a function for this called addMouseCoordinates.
    – Jul
    Jan 15, 2018 at 4:30

2 Answers 2


Looks like you need more powerful tool to handle your problem. The solution in R may be using Shiny - basics in this link: https://rstudio.github.io/leaflet/shiny.html

input$MAPID_click is an event that is sent when the map background or basemap is clicked. The value is a list with lat and lng.


Since I struggled to find this answer, in your server block, you need something like this:

    click = input$map1_click
    text<-paste("Latitude: ", click$lat, ", Longtitude: ", click$lng)
    text2<-paste("You've selected point ", text)
    map1_proxy = leafletProxy("map1") %>%
        clearPopups() %>%
        addPopups(click$lng, click$lat, text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.