首页 文章

无法使用savewidget / webshot在传单中快照Openstreetmap

提问于
浏览
0

我需要使用传单中的openstreet map来创建用户定义的 Map 的快照 . 我正在使用saveWidget保存一个html文件,然后使用webshot来捕捉该文件 . 它与Esri.WorldStreetMap和其他人完美配合 . 但是,我无法使用Openstreetmap . 以下是一个最小的问题:

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 回答

  • 1

    只需稍加修改,您的代码就可以在我的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)
    

    这就是我得到的:

    enter image description here

相关问题