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

我已经找到了此解决方案如何在R中保存传单地图:How to save Leaflet in R map as png or jpg file?

但是它在Shiny中如何工作?我使示例代码保持简单,但请仔细考虑一下,就好像在用户希望将地图另存为pdf之前,通过leafletProxy()对地图进行了很多更改一样。

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

服务器

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 <- fluidPage(
     sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE),
     conditionalPanel(
       condition = "input.returnpdf == true",
       downloadLink('pdflink')
      )
     ),
     mainPanel(leafletOutput("map"))
)

最佳答案

我已经更新了以前的答案,以使其更加清晰,并说明了如何使用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"))


关于r - 将传单 map 保存在Shiny中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35384258/

10-12 23:18