如何将 R 地图中的传单保存为 png 或 jpg 文件? [英] How to save Leaflet in R map as png or jpg file?

查看:24
本文介绍了如何将 R 地图中的传单保存为 png 或 jpg 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Leaflet 包在 R 中创建地图.它运行良好.我可以用简单的Export在R中导出地图,但是我需要从R中的脚本中导出地图.我的简单代码是:

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

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

解决方案

出现了这个非常好的解决方法

<小时>

更新:

现在 webshot 已在 CRAN 上正式发布,并且在 ma​​pview 包中引入了 mapshot,这种手动解决方法不再适用必需的.现在,代码就像这样:

图书馆(地图视图)## 'leaflet' 对象(上图)m <- 传单() %>% addTiles()mapshot(m, file = "~/Rplot.png")## 'mapview' 对象(下图)m2 <- mapview(breweries91)mapshot(m2, file = "~/breweries.png")

I'm using Leaflet package to create maps in R. It works perfectly. I can export maps in R with simply Export, but I need to export maps from script in R. My simple code is:

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

It works but... the output png file is white blank.

解决方案

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")

这篇关于如何将 R 地图中的传单保存为 png 或 jpg 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆