从 R 中的一系列 Leaflet 地图创建一个 gif

Create a gif from a series of Leaflet maps in R

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

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

我已经尝试了通常的嫌疑人,以下变体:

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

但是这些都只是打印白或者打印一个连打开都打不开的文件。如果我正确理解这个 Git discussion,似乎 leaflet 中的某些内容不可用,但用户需要。

与此同时,R Studio 显然有办法将此图像渲染成图像文件,让我按下按钮即可完成。 有没有办法自动执行此操作?如何将 R Studio 中绘制的图像导出到图像文件? 我需要图像文件,我需要它是程序化的,因为我想从几百张地图中制作 gif

或者,我欢迎提出解决方法的建议。 我可以试试这个:Python - render HTML content to GIF image 但如果有人有其他建议,请分享。

我一直在尝试将 webshot 包和 htmltools 中的 saveWidget 组合起来,尽管它很慢。对于几百张地图,如果你只是在这里和那里做,那可能还不错。但是,对于实时应用来说,它太慢了。

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

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

举个例子,我也加载了ggmap,但只是为了获取一个位置放大。

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 上的命令可能略有不同(我认为只是单引号而不是双引号或其他)。这些命令来自命令 line/shell,但您也可以使用 system/system2 从 R 调用或尝试 animation 包,它具有一些 ImageMagick 的包装函数。制作一个没有花哨的简单 gif 很简单,convert *.png animation.gif。我使用稍长的代码来制作 png smaller/add 一些 delays/and 序列进出。

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

我有 table 3 列:纬度、经度、日(376 天)。

过程是:创建地图 -> 将地图另存为 HTML -> 将地图另存为 PNG -> 导入图片 -> 绘制(使用 plot + ggimage)

这一切的过程,会在一个循环中

library(leaflet)
library(animation)
library(png)
library(htmlwidgets)
library(webshot)
library(ggmap)

saveGIF({
for (i in 1:376) {
  map  = leaflet() %>%
    addTiles() %>%
    setView(lng = lon_lat[1,2], lat = lon_lat[1,1], zoom = 5)%>%
    addMarkers(lng = lon_lat[lon_lat$day == i,2],lat = lon_lat[lon_lat$day == i,1])

  saveWidget(map, 'temp.html', selfcontained = FALSE) ## save the html
  webshot('temp.html', file=sprintf('Rplot%02d.png', 1),cliprect = 'viewport') ## save as png
  img = readPNG("Rplot01.png") ### read the png
  plot(ggimage(img)) ###reading png file
}
})

您可以创建一系列 PNG 文件,正如 jenesaisquoi(第一个答案)所回答的那样。然后使用带有 magick 包的以下代码使用 png 文件创建 gif。

library(magick)

    png.files <- sprintf("Rplot%02d.png", 1:20) #Mention the number of files to read
GIF.convert <- function(x, output = "animation.gif")#Create a function to read, animate and convert the files to gif
        {
        image_read(x) %>%
        image_animate(fps = 1) %>%
        image_write(output)
        }

        GIF.convert(png.files)

您不需要在 PC 上安装 ImageMagick 软件。

代码Link:Animation.R