为什么webshot不能使用R闪亮的传单?

Jam*_*ssy 5 r leaflet shiny

首先关闭webshot在这个背景下没有工作webshot("google.com")webshot("www.google.com")我得到:

        env: node\r: No such file or directory
        Error in webshot("google.com") : webshot.js returned failure value: 127
Run Code Online (Sandbox Code Playgroud)

所以这不适用于传单代码

    staters <<-    
   readOGR(dsn="cb_2015_us_county_20m",layer="cb_2015_us_county_20m")
  getMap<-function()({

leaflet(staters) %>%
  addPolygons( stroke = T, fillOpacity =.7, smoothFactor = 0, color = "black",
               weight = .5, fill = T, fillColor = "red"
  )
output$downloadMap <- downloadHandler(
filename = function() { paste(input$chooseStates, '.png', sep='') },
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))

  saveWidget(getMap(), "temp.html", selfcontained = FALSE)
  webshot("temp.html", file = "filename.png", cliprect = "viewport")
}
Run Code Online (Sandbox Code Playgroud)

)

我在rshiny上运行时遇到404错误

SeG*_*eGa 2

这是工作。我认为您错过了将file参数传递webshotdownloadHandler.

我将传单地图保存在 a 中,reactive然后您可以在renderLeaflet和 中调用它downloadHandler

以下示例应该有效:

## install 'webshot' package
library(devtools)
# install_github("wch/webshot")
## load packages
# install_phantomjs(version = "2.1.1",
#                   baseURL = "https://github.com/wch/webshot/releases/download/v0.3.1/")
library(leaflet)
library(htmlwidgets)
library(webshot)
library(shiny)

ui <- fluidPage(
  leafletOutput("map"),
  downloadLink("downloadMap", "Download")
)

server <- function(input,output) {
  mapReact <- reactive({
    leaflet() %>% 
      addTiles('http://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png') %>% 
      addCircles(12.5,42,radius=500) %>% addMarkers(12,42,popup="Rome")
  })

  output$map <- renderLeaflet({
    mapReact()
  })

  output$downloadMap <- downloadHandler(
    filename = paste("LeafletMap", '.png', sep=''),
    content = function(file) {
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      saveWidget(mapReact(), "temp.html", selfcontained = FALSE)
      webshot("temp.html", file = file, cliprect = "viewport")

    })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)