This very nice workaround emerged in response to a question asked a little later here on SO. Note that you are required to install PhantomJS to get the following code to work.
## install 'webshot' package
library(devtools)
install_github("wch/webshot")
## load packages
library(leaflet)
library(htmlwidgets)
library(webshot)
## create map
m <- leaflet() %>% addTiles()
## save html to png
saveWidget(m, "temp.html", selfcontained = FALSE)
webshot("temp.html", file = "Rplot.png",
cliprect = "viewport")
And here's the resulting image.
Update:
Now that webshot has been officially released on CRAN and with the introduction of mapshot
in the mapview package, this manual workaround is no longer required. Now, the code simply goes like this:
library(mapview)
## 'leaflet' objects (image above)
m <- leaflet() %>% addTiles()
mapshot(m, file = "~/Rplot.png")
## 'mapview' objects (image below)
m2 <- mapview(breweries91)
mapshot(m2, file = "~/breweries.png")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…