我使用Leaflet包在R中创建地图。它完美无缺。我可以使用简单导出在R中导出地图,但我需要从R中的脚本导出地图。我的简单代码是:
png("test_png.png")
(m <- leaflet() %>% addTiles())
dev.off()
它可以工作但是......输出png文件是白色空白。
答案 0 :(得分:46)
这个非常好的解决方法出现in response to a question稍后会问SO。请注意,您需要安装PhantomJS才能使以下代码生效。
## 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")
这是图片的结果。
现在已经在CRAN上正式发布了 webshot ,并且在 mapview 包中引入了mapshot
,现在不再需要此手动解决方法。现在,代码就是这样:
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")