Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a gganimate graph in html using rmarkdown::render(), without generating unwanted output

I am trying to render gganimate() plots in html using an r-markdown document. I am able to create the html document (though the simple example below takes at least a minute) and the gganimate graphic successfully loads in the browser (firefox), however, I get a bunch of unwanted output in the browser.

The unwanted output looks like this:


Frame 1 (1%)

Frame 2 (2%)

Frame 3 (3%)

...

Frame 96 (96%)

Frame 97 (97%)

Frame 98 (98%)

Frame 99 (99%)

Frame 100 (100%)

Finalizing encoding... done!


Like I said, after this unwanted output, the animation is indeed displayed correctly.

I have tried messing around with the knitR code-chunk heading options. Mostly been looking at these options here: https://bookdown.org/yihui/rmarkdown/r-code.html

I have also tried the solutions suggested on this post: suppress console output in r markdown, but keep plot Specifically, I have tried wrapping the ggplot object in "invisible".

Just copy the code below into an Rmarkdown document, save this Rmarkdown document as "example.Rmd", then, in the R console run: rmarkdown::render("example.Rmd")

---
    title: "Testing gganimate with R Markdown"
    output: html_document
---

```{r message = FALSE}
library(ggplot2)
library(gganimate)
ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')
```
like image 415
axme100 Avatar asked Dec 20 '18 21:12

axme100


3 Answers

One workaround for this is to assign the animation to an object goo <- ggplot(... and write the animation to a file anim_save("goo.gif", goo) while suppressing results from the code chunk results = FALSE. Then render the gif in markdown immediately after the code chunk ![](goo.gif).

E.g.

---
title: "Testing gganimate with R Markdown"
output: html_document
---


```{r message = FALSE, warning = FALSE, results = FALSE}
library(ggplot2)
library(gganimate)
goo <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')

anim_save("goo.gif", goo)
```


![](goo.gif)
like image 148
Chris Holbrook Avatar answered Nov 18 '22 07:11

Chris Holbrook


I was running into this issue as well, so I decided to dive into the code to see if I could get rid of the messages altogether.

It turns out that the output comes from the gifski package that is used by default to generate the animation. gifski lets you control whether it outputs these messages with a progress flag. gganimate uses the output of the interactive() call to set this flag.

I'm not sure why none of the chunk options in rmarkdown suppress the messages (possibly it's because they are created in C code) but my workaround for this issue is instead of using the interactive R session connected to my editor to create the document, I use the following command to create the final version of my document from the command-line:

R -e "rmarkdown::render('document.Rmd',output_file='document.html')"

like image 1
martijn Avatar answered Nov 18 '22 06:11

martijn


EDIT: The example in the question now fails on my desktop (gifski 1.4.3-1, gganimate 1.0.7), though it still works on my laptop (gifski 1.4.3, gganimate 1.0.7). The desktop error is

File test_gganimate_files/figure-html/unnamed-chunk-1-1.gif not found in resource path
Error: pandoc document conversion failed with error 99
Execution halted

I tried downgrading with versions::install.versions("gifski", "1.4.3"), but the error remains. Sorry I don't have time to recreate my exact other package versions at the moment.


ORIGINAL:

This problem seems to be gone as of late 2020, using gifski 0.8.6, gganimate 1.0.7. Here my devtools::session_info() after knitting the example in the question:

devtools::session_info()
## - Session info ---------------------------------------------------------------
##  setting  value                       
##  version  R version 4.0.3 (2020-10-10)
##  os       Windows 10 x64              
##  system   x86_64, mingw32             
##  ui       RTerm                       
##  language (EN)                        
##  collate  English_United States.1252  
##  ctype    English_United States.1252  
##  tz       Europe/Berlin               
##  date     2020-12-02                  
## 
## - Packages -------------------------------------------------------------------
##  package     * version date       lib source        
##  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
##  callr         3.5.1   2020-10-13 [1] CRAN (R 4.0.3)
##  cli           2.2.0   2020-11-20 [1] CRAN (R 4.0.3)
##  colorspace    2.0-0   2020-11-11 [1] CRAN (R 4.0.3)
##  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
##  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
##  devtools      2.3.2   2020-09-18 [1] CRAN (R 4.0.3)
##  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
##  dplyr         1.0.2   2020-08-18 [1] CRAN (R 4.0.2)
##  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
##  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
##  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
##  farver        2.0.3   2020-01-16 [1] CRAN (R 4.0.0)
##  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
##  generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.3)
##  gganimate   * 1.0.7   2020-10-15 [1] CRAN (R 4.0.3)
##  ggplot2     * 3.3.2   2020-06-19 [1] CRAN (R 4.0.2)
##  gifski        0.8.6   2018-09-28 [1] CRAN (R 4.0.0)
##  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
##  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
##  hms           0.5.3   2020-01-08 [1] CRAN (R 4.0.0)
##  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.2)
##  knitr         1.30    2020-09-22 [1] CRAN (R 4.0.3)
##  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.0.3)
##  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.0)
##  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
##  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
##  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
##  pillar        1.4.7   2020-11-20 [1] CRAN (R 4.0.3)
##  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
##  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
##  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
##  plyr          1.8.6   2020-03-03 [1] CRAN (R 4.0.0)
##  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
##  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
##  progress      1.2.2   2019-05-16 [1] CRAN (R 4.0.0)
##  ps            1.4.0   2020-10-07 [1] CRAN (R 4.0.3)
##  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
##  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.3)
##  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.2)
##  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
##  rlang         0.4.9   2020-11-26 [1] CRAN (R 4.0.3)
##  rmarkdown     2.5     2020-10-21 [1] CRAN (R 4.0.3)
##  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.3)
##  scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.0)
##  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
##  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
##  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
##  testthat      3.0.0   2020-10-31 [1] CRAN (R 4.0.3)
##  tibble        3.0.4   2020-10-12 [1] CRAN (R 4.0.3)
##  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.0)
##  tweenr        1.0.1   2018-12-14 [1] CRAN (R 4.0.0)
##  usethis       1.6.3   2020-09-17 [1] CRAN (R 4.0.3)
##  vctrs         0.3.5   2020-11-17 [1] CRAN (R 4.0.3)
##  withr         2.3.0   2020-09-22 [1] CRAN (R 4.0.3)
##  xfun          0.19    2020-10-30 [1] CRAN (R 4.0.3)
##  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
## 
## [1] C:/R-library
## [2] C:/Program Files/R/R-4.0.3/library
like image 1
Jon Olav Vik Avatar answered Nov 18 '22 06:11

Jon Olav Vik