Is it possible to trigger click event on leaflet drawToolbar?

For some reason I would like to draw polygon using button but not the one from the Toolbar, just my own button. In short, I want to click 'Click me!' button and start drawing polygon instead of choosing this option from toolbar. I tried to use jquery to trigger 'click' action on toolbar but can't do this at all. I noticed that 'buttons' on toolbar are not really buttons but simple classes. Maybe that's why I can't 'click' on them programatically. I also tried to use observeEvent but it doesn't work. Do you have any idea how can I trigger this action?

Here's working example:

library(shiny)
library(leaflet)
library(leaflet.extras)
library(dplyr)
library(sf)

ui <- fluidPage(
    actionButton("btn", label = "Click me!"),
    leafletOutput("map")
)

server <- function(input, output, session) {
    
    coords <- quakes %>%
        sf::st_as_sf(coords = c("long","lat"), crs = 4326)

    output$map <- leaflet::renderLeaflet({
    leaflet::leaflet() %>% 
        leaflet::addTiles() %>% 
      leaflet::setView(172.972965,-35.377261, zoom = 4) %>%      
      leaflet::addCircleMarkers(
        data = coords,
        stroke = FALSE,
        radius = 6
        ) %>%         
      leaflet.extras::addDrawToolbar()
  })

}

shinyApp(ui, server)

and here's my jQuery code which should trigger 'draw polygon' option on toolbar but is not working:

$('#btn').click(function(){
  $('.leaflet-draw-draw-polygon').click();
});

This topic was automatically closed 54 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.