I'm working on a Shiny application where users can draw polygons on a Leaflet map, and I'm encountering an issue where the drawn polygons are not being captured or saved as expected. I'm using the leaflet, leaflet.extras, and shiny packages for this functionality.
Here's a simplified version of my Shiny app code:
#########################################################################
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
titlePanel("Draw Polygons on Map"),
leafletOutput("map"),
actionButton("save", "Save Drawn Areas"),
dataTableOutput("coordsTable")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = 2.3522, lat = 48.8566, zoom = 12) %>%
addDrawToolbar()
})
observeEvent(input$map_draw_created, {
shape <- input$map_draw_created
cat("Draw event triggered. Layer type:", shape$layerType, "\n")
print(shape)
})
}
shinyApp(ui = ui, server = server)
####################################################################################
The issue I'm facing is that when I draw polygons on the map and click the "Save" button, the app does not seem to capture or save the polygons. The console does not show any log messages from the observeEvent for map_draw_created, indicating that the event is not being triggered.
I have ensured that the leaflet, leaflet.extras, and shiny packages are up to date. Despite this, the app is not functioning as expected. I'm looking for insights or suggestions on what might be causing this issue and how to resolve it.
Has anyone else experienced a similar issue or can offer guidance on how to correctly capture and save drawn polygons in a Shiny app using Leaflet?
You want to use input$map_draw_new_feature
instead.