首页 文章

在R闪亮中保存传单 Map

提问于
浏览
3

我创建了一个应用程序,用户可以在其中修改传单 Map ,我想在pdf报告中使用此 Map . 我有1.安装包传单,webshot和htmlwidget 2.安装了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: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工作正常:

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中做出的更改 . 有人可以帮忙吗?

1 回答

  • 4

    这不是完美的,但这里有一个使用库html2canvas的解决方案 . 请注意归属,许可和版权 . 此外,这个 won't 在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); 
        }
      });
    });
    '      
        )
      )
    )
    

相关问题