从R中的一系列传单地图创建gif [英] Create a gif from a series of Leaflet maps in R

查看:80
本文介绍了从R中的一系列传单地图创建gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将leaflet R Studio绘图转换为图像文件的自动化方法.

I am looking for an automated method to convert leaflet R Studio plots into image files.

好像将leaflet小部件导出为HTML一样简单(将传单输出保存为html ).但是我找不到关于如何将leaflet小部件生成的图像另存为图像的任何答案或文档.可以在R Studio中手动执行此操作似乎很奇怪,但是R Studio中没有可以调用的功能来执行相同的操作.

Seems like exporting a leaflet widget to HTML is straightforward (Saving leaflet output as html). However I cannot find any answers or docs about how to save the image produced by a leaflet widget as an image. It seems strange that I can do this manually in R Studio but that there isn't some function within R Studio that can be called to do the same thing.

我已经尝试了常见的可疑物,但有以下几种变化:

I've tried the usual suspects, variations on the following:

png("test_png.png")
map
dev.off()

但是这些都只是打印白色或打印甚至无法打开的文件.如果我正确理解了 Git讨论,似乎leaflet中的内容不可用,但是用户所希望的.

But these all just print white or print a file that can't even be opened. IF I understand this Git discussion correctly, seems like something in leaflet is not available but is desired by users.

与此同时,R Studio显然有一种方法可以将该图像渲染为图像文件,使我按下按钮即可. 是否可以自动执行此操作?如何将在R Studio中绘制的图像导出到图像文件?我需要图像文件,并且需要此文件是程序性的,因为我想从几百张地图中制作一个gif.

In the meantime, R Studio clearly has a way to render this image into an image file, making me press a button to do it. Is there a way to automate this? How can I export the images plotted in R Studio to image files? I need image files and I need this to be programmatic because I want to make a gif out of a few hundred maps.

或者,我也欢迎提供解决方法的建议.我可以尝试以下方法:

Alternately, I'd welcome suggestions for a workaround. I might try this: Python - render HTML content to GIF image but if someone has alternatibve suggestions, please share.

推荐答案

我一直在尝试结合

I've been trying to do this with a combination of the webshot package and saveWidget from htmltools, although it's pretty slow. For a few hundred maps, it's probably not too bad if you're only doing it here and there. But, for real-time application it is too slow.

此工作流程需要两个外部应用程序. webshot截取网页的屏幕截图,并要求您首先安装PhantomJS(它很小且很容易).我还使用 ImageMagick (需要从命令行访问)来创建.gif文件,但我确定您可以使用许多其他程序来制作gif文件.

There are two external applications you need for this workflow. webshot takes screenshots of webpages and requires you to install PhantomJS first (it's tiny and easy). I also use ImageMagick (and needs to be accessible from the command line) to create the .gif files, but I'm sure there many other programs you could use to make gifs.

这个想法只是在循环中创建地图,使用saveWidget将它们保存到一个临时html文件中,然后使用webshot将其变成png(慢速).然后,一旦有了所有的png,请使用ImageMagick将它们转换为gif(快速).

The idea is just to create the maps in a loop, save them to a temporary html file with saveWidget and use webshot to turn it into a png (slow). Then, once you have all the pngs, use ImageMagick to convert them to a gif (fast).

这里是一个示例,我也加载了ggmap,但只是为了获得一个放大位置.

Here is an example, I also load ggmap, but only to get a location to zoom in on.

library(webshot)
library(leaflet)
library(htmlwidgets)
library(ggmap)

loc <- geocode('mt everest')  # zoom in everest
zooms <- seq(2,14,3)          # some zoom levels to animate

## Make the maps, this will make some pngs called 'Rplot%02d.png'
## in your current directory
for (i in seq_along(zooms)) {
    m <- leaflet(data=loc) %>%
      addProviderTiles('Esri.WorldImagery') %>%
      setView(lng=loc$lon, lat=loc$lat, zoom=zooms[i])
    if (i==1)
        m <- m %>% addPopups(popup="Going to see Mt Everest")
    if (i==length(zooms))
       m <- m %>%
          addCircleMarkers(radius=90, opacity = 0.5) %>%
          addPopups(popup = 'Mt Everest')

    ## This is the png creation part
    saveWidget(m, 'temp.html', selfcontained = FALSE)
    webshot('temp.html', file=sprintf('Rplot%02d.png', i),
            cliprect = 'viewport')
}

然后,它只是将png转换为gif.我是在Windows上执行此操作的,因此在Mac/Linux上,命令可能会稍有不同(我认为只是单引号而不是双引号或其他内容).这些命令来自命令行/shell,但是您也可以使用system/system2从R调用或尝试使用具有ImageMagick某些包装函数的animation程序包.要制作没有任何幻想的gif只是简单的convert *.png animation.gif.我使用了稍长的代码来使png变小/增加一些延迟/并使序列进出.

Then, it is just converting pngs to gif. I did this on a Windows, so command might be slightly different on a mac/linux (I think just single quotes instead of double quotes or something). These commands are from a command line/shell, but you could also use system/system2 to call from R or try the animation package that has some wrapper functions for ImageMagick. To make a simle gif with nothing fancy is simply, convert *.png animation.gif. I used a slightly longer code to make the pngs smaller/add some delays/and have the sequence go in and out.

convert Rplot%02d.png[1-5] -duplicate 1,-2-1, -resize "%50" gif:- | convert - -set delay "%[fx:(t==0||t==4)?240:40]" -quiet -layers OptimizePlus -loop 0 cycle.gif

这篇关于从R中的一系列传单地图创建gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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