我正在使用Leaflet包在R中创建地图,它工作得很好。我可以使用简单的导出导出R中的地图,但我需要从R中的脚本导出地图。我的简单代码是:
png("test_png.png")
(m <- leaflet() %>% addTiles())
dev.off()
它起作用了但是..。输出的png文件为空白。
发布于 2016-01-08 16:13:45
这个非常好的变通方法出现了,in response to a question稍后在这里问道。请注意,您需要安装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")
这是生成的图像。
更新:
现在,webshot已在CRAN上正式发布,并在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")
https://stackoverflow.com/questions/31336898
复制相似问题