在R Shiny中保存传单地图 [英] Saving leaflet maps in R shiny

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

问题描述

我创建了一个应用,用户可以在其中修改传单地图,我希望在pdf报告中使用此地图.我有 1.已安装的软件包Leaflet,Webshot和htmlwidget 2.安装了PhantomJS

I have created an app where the user can modify a leaflet map and i would like to use this map in a pdf report. I have 1. installed packages leaflet, webshot and htmlwidget 2. installed PhantomJS

下面是代码的简化版本

server.R:

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

    shinyServer(function(input, output, session) {

      output$amap <- renderLeaflet({
      leaflet() %>%
        addProviderTiles("Stamen.Toner",
                     options = providerTileOptions(noWrap = TRUE,      reuseTiles=TRUE))
  })

  observe({
    leafletProxy("amap") %>%
    clearShapes() %>%
    addCircles(lng = c(22,-2), lat = c(42,65))
  })



  observeEvent(input$saveButton,{
    themap<- leafletProxy("amap")
    saveWidget(themap, file="temp.html", selfcontained = F) 
    webshot("temp.html", file = "Rplot.png",
          cliprect = "viewport")

  })
})

ui.R:

fluidPage(
  leafletOutput("amap", height="600px", width="600px"),
  br(),
  actionButton("saveButton", "Save")
  )

我收到此错误消息:

警告:system.file中的错误:"package"的长度必须为1 堆栈跟踪(从最里面开始): 73:system.file 72:readLines 71:粘贴 70:加载 69:yaml :: yaml.load_file 68:getDependency 67:widget_dependencies 66:htmltools :: attachDependencies 65:toHTML 64:保存窗口小部件 63:observeEventHandler [C:\ R files \ test/server.R#24] 1:闪亮的:: runApp

Warning: Error in system.file: 'package' must be of length 1 Stack trace (innermost first): 73: system.file 72: readLines 71: paste 70: yaml.load 69: yaml::yaml.load_file 68: getDependency 67: widget_dependencies 66: htmltools::attachDependencies 65: toHTML 64: saveWidget 63: observeEventHandler [C:\R files\test/server.R#24] 1: shiny::runApp

激活保存按钮时. 当我这样定义保存按钮时,savewidget可以正常工作:

when the save button is activated. savewidget works fine when i define the save button like this:

  observeEvent(input$saveButton,{
    themap<-leaflet() %>%
      addProviderTiles("Stamen.Toner",
                       options = providerTileOptions(noWrap = TRUE, reuseTiles=TRUE))

    saveWidget(themap, file="temp.html", selfcontained = F) 
    webshot("temp.html", file = "Rplot.png",
          cliprect = "viewport")

  })

但是我真的希望用户在网络快照中进行更改.有人可以帮忙吗?

But i really want the changes that the user makes in the webshot. Can anyone help?

推荐答案

这不是完美的方法,但是这里使用库

This is not perfect, but here a solution using the library html2canvas. Please be careful of attribution, license, and copyright. Also, this won't work in RStudio Viewer, but there are ways to get it to work.

library(leaflet)
library(htmltools)

lf <- leaflet() %>%
  addProviderTiles(
    "Stamen.Toner",
    options = providerTileOptions(
      noWrap = TRUE,
      reuseTiles=TRUE
    )
  )


#  add the mapbox leaflet-image library
#   https://github.com/mapbox/leaflet-image
#lf$dependencies[[length(lf$dependencies)+1]] <- htmlDependency(
#  name = "leaflet-image",
#  version = "0.0.4",
#  src = c(href = "http://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.4/"),
#  script = "leaflet-image.js"
#)

lf$dependencies[[length(lf$dependencies)+1]] <- htmlDependency(
  name = "html2canvas",
  version = "0.5.0",
  src = c(href="https://cdn.rawgit.com/niklasvh/html2canvas/master/dist/"),
  script = "html2canvas.min.js"
)



browsable(
  tagList(
    tags$button("snapshot",id="snap"),
    lf,
    tags$script(
'
document.getElementById("snap").addEventListener("click", function() {
  var lf = document.querySelectorAll(".leaflet");
  html2canvas(lf, {
    useCORS: true,
    onrendered: function(canvas) {
      var url = canvas.toDataURL("image/png");
      var downloadLink = document.createElement("a");
      downloadLink.href = url;
      downloadLink.download = "map.png"

      document.body.appendChild(downloadLink);
      downloadLink.click();
      document.body.removeChild(downloadLink); 
    }
  });
});
'      
    )
  )
)

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

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