Return a rendered leaflet map from leafletProxy()

2019-07-25 16:15发布

Is it possible to retrieve a leaflet map in Shiny after it's already been rendered?

Here is an example in code that shows how a map generated by leaflet() is different from one that is returned from leafletProxy() even though they would appear the exact same when rendered. Is there a function maybe different from leafletProxy() to get the actual htmlwidget object?

library(shiny)
library(leaflet)

m1 <- leaflet() %>% addTiles()

shinyApp(
  ui = fluidPage(
    textOutput("test"),
    br(),
    leafletOutput("mymap")
  ),
  server = function(input, output, session) {
    output$mymap <- renderLeaflet({
      leaflet() %>% addTiles()
    })
    output$test <- renderText({
      sprintf("Are the two maps the same?: %s", 
              identical(m1, leafletProxy("mymap")))
    })
  }
)

Reasoning: The reason that I would like the actual rendered object is that I have a Shiny application where the map is updated multiple times using leafletProxy() and then it needs to be downloaded to an HTML file. The problem is saveWidget, webshot, mapshot and other functions need the actual map object, not the proxy version in order to save. This question has been posted multiple times in a few different ways, but the only viable solution is to create two maps side-by-side: one that's updated via leafletProxy() and another that's built directly on top of the call to leaflet(). I would rather not maintain two different maps.

Potential Duplicate Questions

Disclaimer I have cross-posted this question as an issue to the manipulateWidget package since it seems like the most promising area where already rendered objects in Shiny can be retrieved. https://github.com/rte-antares-rpackage/manipulateWidget/issues/59

0条回答
登录 后发表回答