在Shiny中保存leaflet地图

17

我已经在Shiny应用程序中创建了一个leaflet地图。现在我需要一个下载按钮,这样用户就可以将当前显示的地图(包括所有标记、多边形等)作为pdf文件下载。

我发现了如何在R中保存leaflet地图的解决方案:How to save Leaflet in R map as png or jpg file?

但是在Shiny中该怎么做呢?我保持示例代码简单,但请考虑一下,在用户希望将地图保存为pdf之前,通过leafletProxy()进行了大量更改。

这是我的尝试,但它没有起作用。

server.R

library(shiny)
library(leaflet)
library(devtools)
install_github("wch/webshot") # first install phantomjs.exe in your directory

library(htmlwidgets)
library(webshot)

server <- function(input, output){

  output$map <- renderLeaflet({
    leaflet() %>% addTiles()
  })

 observe({
    if(input$returnpdf == TRUE){
      m <- leafletProxy("map")
      saveWidget(m, "temp.html", selfcontained = FALSE)
      webshot("temp.html", file = "plot.pdf", cliprect = "viewport")
    }
  })

  output$pdflink <- downloadHandler(
    filename <- "map.pdf",
    content <- function(file) {
      file.copy("plot.pdf", file)
    }
  )
}

ui.R

ui <- fluidPage(
     sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE), 
     conditionalPanel(
       condition = "input.returnpdf == true",
       downloadLink('pdflink')
      ) 
     ), 
     mainPanel(leafletOutput("map"))
)
1个回答

10

我已经更新了之前的答案,使其更加清晰,并说明了如何使用来自mapview包的mapshot。 此外,根据Jake在下面的问题,我注意到可能需要指定一个瓦片链接(在addTiles中),否则地图可能会下载一个灰色的背景。

服务器

server = function(input, output){

    mymap <- reactive({
      # here I have specified a tile from openstreetmap
      leaflet() %>% addTiles('http://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png')
    })

    output$map <- renderLeaflet({
      mymap()
    })

    # function with all the features that we want to add to the map
    myfun <- function(map){
      addCircles(map,12.5,42,radius=500) %>% addMarkers(12,42,popup="Rome")
    }

    observe({
      leafletProxy("map") %>% myfun()
    })

    # map that will be downloaded
    mapdown <- reactive({
      # we need to specify coordinates (and zoom level) that we are currently viewing
      bounds <- input$map_bounds
      latRng <- range(bounds$north, bounds$south)
      lngRng <- range(bounds$east, bounds$west)
      mymap() %>% myfun() %>% setView(lng = (lngRng[1]+lngRng[2])/2, lat = (latRng[1]+latRng[2])/2, zoom = input$map_zoom)
    })

    output$map_down <- downloadHandler(
      filename = 'mymap.pdf',

      content = function(file) {
        # temporarily switch to the temp dir, in case you do not have write
        # permission to the current working directory
        owd <- setwd(tempdir())
        on.exit(setwd(owd))

        # using saveWidget and webshot (old)
        saveWidget(mapdown(), "temp.html", selfcontained = FALSE)
        webshot("temp.html", file = file, cliprect = "viewport")

        # using mapshot we can substitute the above two lines of code
        # mapshot(mapdown(), file = file, cliprect = "viewport")
      }
    )
  }

用户界面

ui <- fluidPage(
     sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE), 
     conditionalPanel(
       condition = "input.returnpdf == true",
       downloadButton('map_down')
      ) 
     ), 
     mainPanel(leafletOutput("map"))

)

:这是一个只包含右括号的HTML段落。

有什么想法为什么绘图会下载灰色背景,而不是地图背景(特别是使用第二个代码块中的方法)? - Jake
一个可能的原因是它没有加载地图的瓦片。顺便说一下,现在可以使用 mapshot 来大幅简化下载,而不是使用 webshot。基本上,您不再需要使用 Rmarkdown 或 saveWidget。我会发布一个更新的解决方案。 - Davide
嗨,杰克,它下载时带有灰色背景,因为您需要在_addTiles_中指定链接(请参见我上面编辑的示例)。我猜默认瓷砖不支持下载,但您可以尝试指定默认瓷砖的链接,看看是否有效。希望能帮到你。 - Davide
谢谢您的回复。我按照您上面的示例确实让它工作了,但我真的想能够将其与离线地图瓦片一起使用。我已经在本地计算机上下载了一些瓦片(我不会连接到互联网),并正在提供这些瓦片。当我使用这些本地瓦片按照您的示例操作时,我的下载仍然显示灰色背景。 - Jake
如果使用leaflet.extras包中的addDrawToolbar绘制线条和标记,那该怎么办呢?我无法保存带有它们的地图,于是我在Stack Overflow上提出了一个问题: https://dev59.com/XFQJ5IYBdhLWcg3wiWab - BRCN
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接