Reputation: 304
I'm working on a project in which I would like create and save WebGLHeatmap images from the leaflet.extras in an automated way. Unfortunately, while the following code:
map = leaflet(df) %>%
addWebGLHeatmap(lng=~df$longitude, lat=~df$latitude, opacity= 1 , intensity = .5 ,size = 1000, gradientTexture='deep-sea', data=df)%>%
addTiles(urlTemplate = map, attribution=attr)
Produces the desired image:
When I attempt to save the image in code (which I will eventually need to do) with the following code:
saveWidget(map, "tmp.html", selfcontained = F)
webshot("temp.html", file = "new_orleans.png", cliprect = "viewport")
I'm left with the following image instead:
I opened up an issue on the webshot github page, whose owner gave me this response. I also know a similar issue was addressed on SO last year, but there did not seem to be any resolution to it.
Since I have only seen examples of saving leaflet.extras maps with either webshot or mapshot (which gives me the exact same issue), I was wondering if anyone who has encountered this issue has been successful in resolving it through some sort of workaround, or with another R package that I am yet unfamiliar with.
Upvotes: 0
Views: 364
Reputation: 7469
This is a bit of a hack, with a call to system
but I think that you can make this work with RSelenium and your screenshot software that comes with your OS:
library(leaflet.extras)
library(leaflet)
library(RSelenium)
## addWebGLHeatmap
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addWebGLHeatmap(lng = ~long, lat = ~lat, size = 60000)
quakeplot <- leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addWebGLHeatmap(lng = ~long, lat = ~lat, size = 60000)
quakeplot
htmlwidgets::saveWidget(quakeplot, "quake.html")
rD <- rsDriver(browser="firefox")
remDr <- rD$client
remDr$navigate(paste0("file://", getwd(), "/quake.html"))
Sys.sleep(5)
system("gnome-screenshot -f test2.png")
remDr$close()
I saved the file to my current working directory then used the getwd()
to show that file in my browser. The Sys.sleep()
call is because it seemed like the leaflet tiles took forever to load (which might be part of your original issue).
Final result:
Not perfect but still pretty good!
Upvotes: 1