0

I have the need to make a snapshot of a userdefined map using openstreet map in leaflet. I am using saveWidget to save a html file and then webshot to take a snap of that file. It works perfectly with Esri.WorldStreetMap and others. However, I cannot make it work with Openstreetmap. Below is a minimal exaple:

library(shiny)
library(leaflet)
library(webshot)
library(htmlwidgets)

ui <- fluidPage(
  actionButton("button", "An action button")
)

server <- function(input, output, session) {
  observeEvent(input$button,
               {
                 themap<-leaflet() %>%
                   addProviderTiles("Openstreetmap.Mapnik")%>%
                   setView(lng=174.768, lat=-36.852,zoom=14)%>% 
                   addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
                 saveWidget(themap, 'temp.html', selfcontained = T)    
                 webshot('temp.html', file = "map.png",cliprect = viewport")
               })

}

shinyApp(ui, server)

1 Answers1

1

With minor modifications your code works on my R 3.4.2:

ui <- fluidPage(
  actionButton("button", "An action button")
)
server <- function(input, output, session) {
  observeEvent(input$button,
               {
                 themap <- leaflet() %>%
                   addProviderTiles("OpenStreetMap.Mapnik") %>%
                   setView(lng=174.768, lat=-36.852,zoom=14) %>% 
                   addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
                 saveWidget(themap, 'temp.html', selfcontained = T)    
                 webshot('temp.html', file = "map.png",cliprect = "viewport")
               })

}
shinyApp(ui, server)

This is what I get:

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • The Problem persists under R 3.4.2 ( i was using 3.4.1. before). I have the same problem on my local version and on the Rstudio server and shiny server. I don't see the minor modifications to the code that you mentioned. – Mette Lægdsmand Nov 01 '17 at 08:37
  • thanks. spelling error when posting. Unfortunately i still experience the problem. – Mette Lægdsmand Nov 01 '17 at 11:36
  • R version 3.4.2 (2017-09-28) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 Matrix products: default locale: [1] LC_COLLATE=Danish_Denmark.1252 LC_CTYPE=Danish_Denmark.1252 LC_MONETARY=Danish_Denmark.1252 LC_NUMERIC=C [5] LC_TIME=Danish_Denmark.1252 .... – Mette Lægdsmand Nov 01 '17 at 12:16
  • attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] htmlwidgets_0.9 webshot_0.4.2 leaflet_1.1.0 shiny_1.0.5 loaded via a namespace (and not attached): [1] Rcpp_0.12.13 digest_0.6.12 mime_0.5 R6_2.2.2 xtable_1.8-2 jsonlite_1.5 magrittr_1.5 tools_3.4.2 [9] crosstalk_1.0.0 httpuv_1.3.5 yaml_2.1.14 rsconnect_0.8.5 compiler_3.4.2 htmltools_0.3.6 – Mette Lægdsmand Nov 01 '17 at 12:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157997/discussion-between-mette-laegdsmand-and-marco-sandri). – Mette Lægdsmand Nov 01 '17 at 12:55
  • If anyone experience the same and find a solution plaese let me know. – Mette Lægdsmand Nov 01 '17 at 12:59
  • 1
    1. the modified code above worked for me. 2. there are two spelling error in your code: Openstreetmap -> OpenStreetmap, viewport" - "viewport". How can you have spelling errors? How that code work for yourself? 3. why do you need the shiny part for example? It makes no sense. The script inside the {} part can be run independently. – dracodoc Jan 11 '18 at 15:15
  • @dracodoc Hi. I totally agree with your comment. I do not understand why Mette said that my code did not work. If my modified code works for you, could you upvote it ? Thank you ! – Marco Sandri Jan 11 '18 at 15:27
  • @dracodoc Thank you! – Marco Sandri Jan 12 '18 at 16:06