R Shiny with Leaflet - Remove shape (rectangle) drawn with leaflet.extras

Viewed 129

I'm using the addDrawToolbar function in the leaflet.extras package to draw a rectangle in a leaflet map in a Shiny app. After the rectangle is drawn, I can't seem to remove it.

I've tried using removeDrawToolbar as shown in the code below, but that doesn't seem to work.

Run the app and draw a rectangle using the black rectangle icon on the map toolbar. Clicking Apply should remove the rectangle. I know the Apply click is observed because the print statement executes in the console.

How do I remove the rectangle?

library(shiny)
library(leaflet)
library(leaflet.extras)

data <- data.frame(lat = c(43.836, 43.866), lng = c(-103.622, -103.532))

ui <- fluidPage(
    leafletOutput("map1"),
    actionButton(inputId = "apply_button", 
                 label = "Apply")
)

server <- function(input, output) {
    values <- reactiveValues(data = data)
    
    output$map1 <- renderLeaflet({
        leaflet() %>% addTiles() %>% 
            addMarkers(data = values$data) %>% 
            addDrawToolbar(
                targetGroup = "markers",
                polylineOptions = FALSE,
                polygonOptions = FALSE,
                circleOptions = FALSE,
                markerOptions = FALSE,
                circleMarkerOptions = FALSE
            )
    }) 
    
    observeEvent(input$apply_button, {
        print("apply button pressed")
        leafletProxy("map1") %>%
            removeDrawToolbar(clearFeatures = TRUE)
    })
}

shinyApp(ui, server)
0 Answers
Related