将传单地图保存在Shiny中 [英] Save leaflet map in Shiny

查看:60
本文介绍了将传单地图保存在Shiny中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a leaflet map in a Shiny application. Now I need a download button, so that the user can download the currently shown map including all markers, polygons etc. as a pdf file.

我发现了此解决方案如何在R中保存传单地图:

I have found this solution how to save a leaflet map in R: How to save Leaflet in R map as png or jpg file?

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

But how does it work in Shiny? I kept the example code simple, but think of it, as if there were a lot of changes to the map via leafletProxy() before the user wants to save the map as a pdf.

这是我的尝试,但是不起作用.

This is my try, but it's not working.

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"))
)

推荐答案

我更新了之前的答案,以使其更加清晰并说明如何使用软件包 mapview 中的 mapshot >. 此外,遵循下面的Jake问题,我注意到可能有必要指定一个指向图块的链接(在 addTiles 之内),或者可能会以灰色背景下载地图.

I have updated my previous answer to make it more clear and illustrate how to use mapshot from package mapview. Moreover, following Jake's question below, I noticed that it might be necessary to specify a link to a tile (within addTiles), or the map might be downloaded with a grey background.

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

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

)

这篇关于将传单地图保存在Shiny中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆