Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LaTeX animate package in RMarkdown

I would like to generate an animated graphic in PDF using the LaTeX animate package.

Code

---
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}

Problem

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

Notes

Why the reference to ffmpeg. According to the 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

ffmpeg should be used in conversion to html. Is it because of the RMarkdown's pipeline?

enter image description here

(RStudio: RMarkdown)

that forces use of ffmpeg somewhere along the line?

Question

Is it possible to make use of the animate package in a RMarkdown document and avoid ffmpeg so the obtained PDF has the following component with conrtols offred by the animate package.

chart with controls

(Not the chart I want to generate but shows how the animated graphic should be embedded in the PDF, taken from the animate package documentation referenced above.)

like image 818
Konrad Avatar asked Oct 27 '17 15:10

Konrad


1 Answers

Drawing on a similar answer (Plot animation in knitr rmarkdown) and a LaTeX-related discussion I have came up with the following solution:

---
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}
~~~

The header file is as in question. The code generates and animated graph with the required controls.

Preview

Snapshot of generated animation with the controls available.

preview of generated animation

Notes

  • The code makes use of the external convert command. I reckon that this is not optimal as it involves converting the graph back and forth.
  • As a consequence of above, the values in {12}{something-}{0}{12} have to be set manually with the first figure reflecting frame rate (decreasing will slow down the animation) something- corresponds to files generated via convert that are named something-0.png ... something-n.png and {0}{12} corresponds to the file numbers.
like image 79
Konrad Avatar answered Sep 30 '22 19:09

Konrad