動画出力の仕方

ここでは,RとRstudioによる動画出力の方法をまとめる.以下の例は,animationライブラリのpdfからの改変.

Rscriptでhtmlに出力


library(animation)

saveHTML({
  par(mar = c(4, 4, 0.5, 0.5))
  for (i in 1:20) {
    plot(runif(20), ylim = c(0, 1))
    ani.pause()
  }
}, img.name = "random", imgdir = "random", htmlfile = "random.html",
autobrowse = FALSE, title = "Demo of 20 uniform random numbers",
description = "description")

Rscriptでmp4に出力

まず,ffmpegをダウンロードしインストールする.


library(animation)

# windows
#oopts = ani.options(ffmpeg = "z:/R/ffmpeg.exe")
# mac
oopts = ani.options(ffmpeg = "ffmpeg")

saveVideo({
  par(mar = c(3, 3, 1, 0.5), mgp = c(2, 0.5, 0), tcl = -0.3, cex.axis = 0.8,
      cex.lab = 0.8, cex.main = 1)
  ani.options(interval = 0.05, nmax = 300)
  brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow")
}, video.name = "BM.mp4", other.opts = "-b 300k")  # higher bitrate, better quality
ani.options(oopts)

knitrでhtmlに出力.動画にはjavascript使用.

サンプル

knitrでhtmlに出力.動画にはffmpegでwebmを使用.

サンプル

2016年12月7日現在においての動きは以下.

レポートの提出には,knitrでjavascript+htmlに出力することを推奨する.