In this example I have some polygons and I click on the centroids (variable centroides) to get values from a raster (variable s).

server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles('Esri.WorldImagery') %>%
addMarkers(data = points()) %>%
addPolygons(data=sps_example) #This is my shapefile, only to show the
observeEvent(input$mymap_marker_click, {
p <- input$mymap_marker_click
dss <- data.frame(lon=p$lng, lat=p$lat)
coordinates(dss) <- c("lon", "lat")
proj4string(dss) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0") 
e_v<-extract(s, dss_t)
output$plot <- renderPlot({
plot(as.numeric(e_v), type="l")

Then I use the coordinates which I got with input$mymap_marker_click to extract the values from the raster inr the next steps.

But I was wondering if is there a way to get this coordinates from any pixel, without depending on the markers (like the centroids in this case). Just by clicking on the pixel in the map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.