r - 在 RMarkdown 中使用 LaTeX 动画包

标签 r pdf animation knitr r-markdown

我想使用 LaTeX animate 在 PDF 中生成动画图形包裹。

代码

---
title: "test_animations"
author: "Colours"
date: "27/10/2017"
output: 
    pdf_document:
        includes:
            in_header: header_ani.tex

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
pacman::p_load(gganimate, gapminder, ggplot2)
```

## Test animations

```{r sample_ani, fig.show='animate', message=FALSE, warning=FALSE}
p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
  geom_point() +
  geom_point(aes(frame = year), color = "red") +
  scale_x_log10()
gganimate(p2, saver = "gif")
```
header_ani.tex
\usepackage{animate}

问题

Warning: Ignoring unknown aesthetics: frame

Quitting from lines 20-25 (second_animation.Rmd) Error: Could not find ffmpeg command. You should either change the animation.fun hook option or install ffmpeg with libvpx enabled. Execution halted



笔记

为什么要引用 .根据knitr documentation :

When the chunk option fig.show='animate' and there are multiple plots produced from a code chunk, all plots will be combined to an animation. For LaTeX output, the LaTeX package animate is used to create animations in PDF. For HTML/Markdown output, by default FFmpeg is used to create a WebM video. Note you have to enable the libvpx support when installing FFmpeg. Linux and Windows users can just follow the download links on the FFmpeg website (libvpx has been enabled in the binaries). For OS X users, you can install FFmpeg via Homebrew



应该用于转换为html。是不是因为 RMarkdown 的管道?

enter image description here

(RStudio: RMarkdown)

强制使用 沿线的某个地方?



是否可以使用 animate打包在 RMarkdown 文档中并避免 因此获得的 PDF 具有以下组件,其中包含 animate 包提供的控制。

chart with controls

(不是我想要生成的图表,而是显示动画图形应该如何嵌入到 PDF 中,取自上面引用的动画包文档。)

最佳答案

借鉴类似的答案 (Plot animation in knitr rmarkdown)和一个 LaTeX-related discussion我想出了以下解决方案:

---
title: "test_animations"
author: "Colours"
date: "27/10/2017"
classoption: landscape
output: 
    pdf_document:
        keep_tex: true
        includes:
            in_header: header_ani.tex

---

```{r setup, include=FALSE}
Vectorize(require)(package = c("knitr"),
               character.only = TRUE)
opts_chunk$set(echo = FALSE,
               cache = TRUE)
pacman::p_load(gganimate, gapminder, ggplot2)
```

## Test animations

```{r sample_ani, message=TRUE, warning=TRUE, echo=TRUE, }
p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
  geom_point() +
  geom_point(aes(frame = year), color = "red") +
  scale_x_log10()
gganimate(p2, filename = "animation2.gif", title_frame = TRUE) -> amimate
```
<!-- Notes on the answer: https://tex.stackexchange.com/a/240387/123504 -->

```{r convert_shele, echo=TRUE, message=TRUE, warning=TRUE, paged.print=FALSE}
# Extra options for resize can be added
system(command = "convert -coalesce animation2.gif something.png")
```


  \animategraphics[loop,controls,width=\linewidth]{12}{something-}{0}{12}

# Latex code generating animation

Figures 0 - 12 should reflect frames

~~~
  \animategraphics[loop,controls,width=\linewidth]{12}{something-}{0}{12}
~~~

头文件是有问题的。代码生成带有所需控件的动画图形。

预览

带有可用控件的生成动画的快照。

preview of generated animation

笔记
  • 该代码使用外部 convert命令。我认为这不是最佳的,因为它涉及来回转换图形。
  • 作为上述结果,{12}{something-}{0}{12} 中的值必须使用反射(reflect)帧速率的第一个图形手动设置(降低会减慢动画速度)something-对应于通过 convert 生成的文件名为 something-0.png ... something-n.png{0}{12}对应文件编号。
  • 关于r - 在 RMarkdown 中使用 LaTeX 动画包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46978055/

    相关文章:

    r:按多列分组并计数

    ios - 每个循环后都有延迟的按钮图像阵列动画

    r - 如果值存在于 r 的其他列中,如何创建列标记?

    java - 在java中读取.csv文件: missing few rows

    ios - 如何使用 CGPDF 获取 PDF 目标

    r - knitr pandoc : "cannot produce pdf output with pdf writer"

    Android Gauge动画问题

    javascript - 从上到下隐藏方 block

    R数据帧排列函数

    html - IE <object> z-index 问题。 wmode透明不起作用