【问题标题】:Saving leaflet maps in R shiny在 R 中保存传单地图
【发布时间】:2016-07-29 11:22:41
【问题描述】:

我创建了一个应用程序,用户可以在其中修改传单地图,我想在 pdf 报告中使用此地图。我有 1.安装包leaflet、webshot和htmlwidget 2.安装PhantomJS

下面是简化版的代码

服务器.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 中的错误:“包”的长度必须为 1 堆栈跟踪(最内层优先): 73:系统文件 72:读取线 71:粘贴 70:yaml.load 69: yaml::yaml.load_file 68:获取依赖关系 67:小部件_依赖项 66: htmltools::attachDependencies 65:转HTML 64:保存小部件 63:observeEventHandler [C:\R 文件\test/server.R#24] 1:闪亮::runApp

当保存按钮被激活时。 当我像这样定义保存按钮时,savewidget 工作正常:

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

  })

但我真的想要用户在 webshot 中所做的更改。有人可以帮忙吗?

【问题讨论】:

  • 您希望用户能够保存文件吗?我认为这在 JavaScript 方面会更好地处理。我会试着为你做一个例子。
  • 好吧,我出色的解决方案陷入了跨域问题。我会试着想其他的处理方式。

标签: r shiny leaflet


【解决方案1】:

这并不完美,但这里是使用库 html2canvas 的解决方案。请注意出处、许可和版权。此外,这不会在 RStudio Viewer 中工作,但有一些方法可以让它工作。

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); 
    }
  });
});
'      
    )
  )
)

【讨论】:

  • 非常感谢您的快速回复。我还没有让它在我闪亮的应用程序中工作。我在 javascript 方面不太强......本周晚些时候将不得不回到它。感谢您迄今为止所做的工作
猜你喜欢
  • 2017-07-28
  • 2016-05-24
  • 2017-10-30
  • 1970-01-01
  • 2016-10-23
  • 2017-10-02
  • 2015-09-28
  • 2017-06-19
相关资源
最近更新 更多