gpt4 book ai didi

r - 如何将 R map 中的 Leaflet 保存为 png 或 jpg 文件?

转载 作者:行者123 更新时间:2023-12-03 08:25:50 32 4
gpt4 key购买 nike

我正在使用 Leaflet 包在 R 中创建 map 。它工作得很好。我可以通过简单的导出导出 R 中的 map ,但我需要从 R 中的脚本导出 map 。我的简单代码是:

png("test_png.png")
(m <- leaflet() %>% addTiles())
dev.off()

它可以工作,但是...输出 png 文件是白色的空白。

最佳答案

这个非常好的解决方法出现了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")

这是生成的图像。

map



更新:

现在, 网络截图 已在 CRAN 上正式发布并引入 mapshot map View 包,不再需要此手动解决方法。现在,代码就是这样:
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")

breweries

关于r - 如何将 R map 中的 Leaflet 保存为 png 或 jpg 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31336898/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com