leafgl icon indicating copy to clipboard operation
leafgl copied to clipboard

screenshooting a map with leafgl layer doesnt work

Open trafficonese opened this issue 3 years ago • 4 comments

I dont think this is really a leafgl error and more a bug of the underlying JS-libraries (webshot, html2canvas, FileSaver?) but taking a screenshot of a map with leafgl data doesnt display the layer in the resulting image.

I tried several approaches to take a screenshot; mapview::mapshot, shinyscreenshot::screenshot and leaflet.extras2::addEasyprint.

Here's a shiny app which tries all those methods:

library(shiny)  
library(sf)
library(leaflet)
library(leafgl)
library(leaflet.extras2)
library(shinyscreenshot)
library(mapview)

storms = st_as_sf(atlStorms2005)
cols = heat.colors(nrow(storms))

ui <- fluidPage(
  actionButton("go", "Take a screenshot with `shinyscreenshot`"),
  actionButton("mapshot", "Take a screenshot with `mapview`"),
  leafletOutput("map"))

m <- leaflet()  %>% 
  addTiles() %>%
  addEasyprint(options = easyprintOptions(exportOnly = TRUE)) %>%
  addGlPolylines(data = storms, color = cols, popup = TRUE, opacity = 1)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    m
  })
  observeEvent(input$go, {
    screenshot()
  })
  observeEvent(input$mapshot, {
    mapshot(m, file = paste0(getwd(), "/map.png"))
  })
}
shinyApp(ui, server)

trafficonese avatar Dec 14 '20 17:12 trafficonese

Did you also try https://github.com/rstudio/webshot2?

tim-salabim avatar Dec 14 '20 18:12 tim-salabim

If we expose the option preserveDrawingBuffer and set it to TRUE the screenshot works with addEasyprint and screenshot, but somehow mapshot doesnt work.

No, I didnt try webshot2 yet.

trafficonese avatar Dec 14 '20 18:12 trafficonese

Indeed, webshot2 also works, even with preserveDrawingBuffer = FALSE.

trafficonese avatar Dec 14 '20 18:12 trafficonese

That's good news I guess. I've been waiting for RStudio to push webshot2 to CRAN and rewrite mapshot accordingly which currently uses webshot. Until then, a fix using preseveDrawingBuffer seems like a good move. I'll have a look at changes needed for mapshot over the Christmas break. Thanks for bringing this up

tim-salabim avatar Dec 14 '20 19:12 tim-salabim