4
votes

I'm playing with leaflet in shiny app, and I would like to get information about the marker I'm clicking on. At this point I have a nice map with marker, but how can I return information about the marker I click on?

my server.R

library(shiny)
library(leaflet)
library(rgdal)



setwd("~/github/shiny_stuff/banyuls_map/")
####load data converted in geojson
capteurs<-readOGR("./data/capteurs.geojson", "OGRGeoJSON")

shinyServer(function(input, output, session) {
  map <- createLeafletMap(session, "map")
  session$onFlushed(once=TRUE, function() {
    map$addMarker(capteurs@coords[,2], 
               capteurs@coords[,1]
                  )
  })

})

my ui.R

library(shiny)
library(leaflet)



shinyUI(fluidPage(
  titlePanel("title panel"),

  leafletMap(
    "map", "100%", 400,
    initialTileLayer = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
    initialTileLayerAttribution = HTML('Maps by <a href="http://www.mapbox.com/">Mapbox</a>'),
    options=list(
      center = c(42.4797, 3.1181),
      zoom = 12
    )
  ),



))

enter image description here

2

2 Answers

4
votes

I believe you want to have a look at:

input$map_marker_click

The example bquast references uses input$map_shape_click but this is based on underlying GeoJSON data which is contained within the leafletMap you have loaded.

You can use the information returned by map_marker_click to query your data and insert it into a popup.

2
votes

The easiest way would be to look at what is done in a leaflet map example such as the one here:

http://glimmer.rstudio.com/jcheng/leaflet-demo/

you can find the code for this example here:

https://github.com/jcheng5/leaflet-shiny/blob/master/inst/examples/population/server.R

the object is called map$showPopup and can be found on line 118.