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
- Saving LeafletProxy results of user-input in Shiny R
- Saving leaflet maps in R shiny
- https://github.com/r-spatial/mapview/issues/77
- https://github.com/ramnathv/htmlwidgets/issues/271
- https://community.rstudio.com/t/solved-error-when-using-mapshot-with-shiny-leaflet/6765
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