Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is webshot not working with leaflets in R shiny?

Tags:

r

leaflet

shiny

First off webshot isn't working in this context webshot("google.com") for webshot("www.google.com") I get :

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

so This isn't working for the leaftlet code

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

)

I get a 404 error when I run this on rshiny

like image 272
James Hennessy Avatar asked Oct 29 '22 16:10

James Hennessy


1 Answers

It is working. I think you missed passing the file argument to webshot in the downloadHandler.

I saved the leaflet-map in a reactive and then you can call it in the renderLeaflet and in the downloadHandler.

The following example should work:

## 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)
like image 57
SeGa Avatar answered Nov 15 '22 06:11

SeGa