leaflet icon indicating copy to clipboard operation
leaflet copied to clipboard

Signal when `leafletProxy` is done rendering on the client side

Open daattali opened this issue 1 year ago • 0 comments

With a plain leaflet map, we can use htmlwidgets::onRender() in order to perform an action when the map finishes rendering.

When using leafletProxy() to update a map, I don't think there's any way to know when the rendering of the update finished.

Here's an example shiny app, when you press the button it adds polygons. I would like to have some signal or callback when the rendering is complete. Even just triggering shiny's outputinvalidated javascript event would be a step in the right direction.

library(shiny)
library(leaflet)
library(sf)

polys <- st_sf(
  geometry = st_sfc(
    st_polygon(list(rbind(
      c(-101.744384765625, 39.32155002466662),
      c(-101.5521240234375, 39.330048552942415),
      c(-101.40380859375, 39.330048552942415),
      c(-101.33239746093749, 39.364032338047984),
      c(-101.041259765625, 39.36827914916011),
      c(-100.975341796875, 39.30454987014581),
      c(-101.0399169921875, 39.24501680713314),
      c(-101.243896484375, 39.16414104768742),
      c(-101.455078125, 39.11125631228113),
      c(-101.737060546875, 39.13006024213511),
      c(-101.77825927734375, 39.16839962520706),
      c(-101.734619140625, 39.254884981213734),
      c(-101.744384765625, 39.32155002466662)
    )))
  )
)

ui <- fluidPage(
  leafletOutput("map"),
  actionButton("go", "go")
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>%
      setView(-101, 39, 8)
  })
  
  observeEvent(input$go, {
    leafletProxy("map") %>%
      addPolygons(data = polys)
  })
}

shinyApp(ui, server)

daattali avatar Jul 27 '24 14:07 daattali