Interacting with Leaflet labels

How can I make use of leaflet's addLabelOnlyMarkers(), labelOptions() argument interactive = TRUE? I do not have experience integrating JS with R.

If I can't, I have 2 roundabout options.

  1. Use the Shiny Leaflet input$MAPID_click event to find the closest possible POINT, and use that to drive interaction.
  2. Make the label into a marker? Then just use addMarkers() instead of addLabelOnlyMarkers(), and input$MAPID_marker_click to drive interaction.

Hi ,

this should get you going:


points <- data.frame(lng = rnorm(40) * 2 + 13, 
                     lat = rnorm(40) + 48,
                     label = letters[1:40]

ui <- fluidPage(

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

    output$itsmeadam <- renderLeaflet({
        leaflet() %>%
            addTiles() %>% 
            addLabelOnlyMarkers(data = points, 
                                lng = ~lng, 
                                lat = ~lat, 
                                label = ~label, 
                                labelOptions = labelOptions(noHide = T, 
                                                            direction = 'top', 
                                                            textOnly = T,
                                                            interactive = TRUE)
    observeEvent(input$itsmeadam_marker_click, {
        click <- input$itsmeadam_marker_click
        text<-paste("Lattitude ", click$lat, "Longtitude ", click$lng)
        proxy <- leafletProxy("itsmeadam")
        proxy %>% 
            clearPopups() %>%
            addPopups(click$lng, click$lat, text)

shinyApp(ui, server)
1 Like

Oh dear! I assumed input$MAPID_marker_click would only be for addMarkers().

What if I have both addMarkers() and addLabelOnlyMarkers()? How do I signal that I have clicked one instead of the other?

They would be layered one over another - so it would not make a real difference - i would not use both. But if you want to have the markers use the addMarkers() option label and it will show up when you hover over.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.