Home > OS >  Trigger click event on polyline in Leaflet R Shiny
Trigger click event on polyline in Leaflet R Shiny

Time:08-10

I want to trigger a click event when a user clicks on a polyline in Leaflet, but I'm not sure on the exact syntax to have this occur, and I can't find a list of them anywhere. I know that for polygons it's <mapid>_shape_click and for markers it's <mapid>_marker_click, but I've tried line/polyline/path_click to get it to trigger, and no luck.

Reproducible example code, if needed:

library(shiny)
library(leaflet)
library(tidyverse)

ui <- fluidPage(


   fluidRow(
     column(
       width = 12,
       leafletOutput("map")
     )
   )
)


server <- function(input, output) {

    output$map <- renderLeaflet({
      leaflet() %>%
        addTiles() %>%
        setView(lng = -100, lat = 37, zoom = 5) %>%
        addPolylines(
          lng = c(-100, -105),
          lat = c(32, 42)
        )
    })
    
    observeEvent(input$map_path_click, {
      input$map_path_click %>% glimpse()
    })
}


shinyApp(ui = ui, server = server)

CodePudding user response:

You want to use shape

library(shiny)
library(leaflet)
library(tidyverse)

ui <- fluidPage(
    
    
    fluidRow(
        column(
            width = 12,
            leafletOutput("map")
        )
    )
)


server <- function(input, output) {
    
    output$map <- renderLeaflet({
        leaflet() %>%
            addTiles() %>%
            setView(lng = -100, lat = 37, zoom = 5) %>%
            addPolylines(
                lng = c(-100, -105),
                lat = c(32, 42)
            )
    })
    
    observeEvent(input$map_shape_click, {
        input$map_shape_click %>% glimpse()
    })
}


shinyApp(ui = ui, server = server)

enter image description here

  • Related