needRhelp
needRhelp

Reputation: 3148

Leaflet drawToolbar with leafletProxy in R Shiny

I like to add and remove a drawtoolbar when toggling a button. But it does not work. Maybe a bug related to https://github.com/bhaskarvk/leaflet.extras/issues/148 or is something wrong with my code?

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

ui <- fluidPage(
  bsButton("edit", " Edit", icon = icon("pencil"),
           style = "default", type = "toggle", value = FALSE),
  leafletOutput("map")
)

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

  output$map <- renderLeaflet({leaflet() %>% addTiles()})

  observe({
    req(isFALSE(input$edit))
    print(input$edit)
    leafletProxy("map") %>% removeDrawToolbar()
  })

  observe({
    req(isTRUE(input$edit))
    print(input$edit)
    leafletProxy("map") %>% addDrawToolbar()
  })
}

shinyApp(ui, server)

Upvotes: 2

Views: 454

Answers (1)

Carlos Xavier Bonilla
Carlos Xavier Bonilla

Reputation: 667

Looks like an outstanding issue with leafletProxy. As suggested in your link you can workaround this by replacing the two "removeFrom" methods with just "remove" in lfx-draw-bindings.js. On Windows you can find that here:

C:\Program Files\R\R-3.4.3\library\leaflet.extras\htmlwidgets\build\lfx-draw

This solution only works locally though, and won't carry out if you publish your app on shinyapps.io.

Upvotes: 2

Related Questions