Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further integration with knitr? #134

Closed
yihui opened this issue Aug 15, 2018 · 11 comments
Closed

Further integration with knitr? #134

yihui opened this issue Aug 15, 2018 · 11 comments

Comments

@yihui
Copy link
Contributor

yihui commented Aug 15, 2018

Finally I have a chance to play with this package and think about better integration with knitr: https://twitter.com/thomasp85/status/1021462758007672834 :)

I don't know what chunk options you need from knitr (it appears that gganimate already works amazingly well in R Markdown), but currently I wonder if the three chunk options fig.width, fig.height, and dev can be supported (you may also need to use dpi).

```{r, fig.width=4, fig.height=10}
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')
```
@thomasp85
Copy link
Owner

Some of the thoughts I’ve had regarding this is to be able to specify some of the arguments to animate as chink options so that you could define which renderer to use, the number of frames, framerate etc

This would allow you to not have to end with a call to animate() in order to costumize the animation settings

@thomasp85
Copy link
Owner

A big departure from the current animation support is that the number of frames in the animation is free to choose

@yihui
Copy link
Contributor Author

yihui commented Aug 16, 2018

For the knit_print.gganim method:

gganimate/R/gganim.R

Lines 23 to 26 in ef2e717

knit_print.gganim <- function(x, ...) {
anim <- animate(x)
knitr::knit_print(anim)
}

You are actually free to use any chunk options, e.g.,

knit_print.gganim <- function(x, options, ...) {
  anim <- do.call(animate, c(list(x, device = options$dev), options$gganimate.options)
  knitr::knit_print(anim)
}

Here the chunk option gganimate.options is just an example (you could use a different option name).

Then users can pass any arguments to animate, e.g.,

```{r, gganimate.options = list(renderer = ffmpeg_renderer())}
```

Supporting fig.width and fig.height can be a little bit tricky because I use inch as the unit for all graphical devices, e.g.,
https://github.com/yihui/knitr/blob/01407a72c8d3497336487029b21189ad13bee498/R/plot.R#L81 (if you want, I can export this function).

@thomasp85
Copy link
Owner

Ah - didn't know you could just create your own options. In that case I don't think there is anything more that knitr should do...

I think I'll just make my own device dimension solution as I don't currently support all devices in gganimate

What is your stance on namespacing options? Would you prefer to have one gganimate.options or is it ok to have all options as separate ones?

@thomasp85
Copy link
Owner

BTW, slightly tangential to this issue - have you had any experience with animations in pdf using the tikz and animate latex packages?

How would one potentially support this in knit_print

@yihui
Copy link
Contributor Author

yihui commented Aug 17, 2018

Good question on namespacing options. That has actually been a minor headache of mine, because knitr doesn't have any constraints on what options you can provide or use. So far I haven't seen bad consequences (like two package authors happen to use the same option for different purposes). I slightly prefer a single option gganimate.options = list() (if the name gganimate.options feels too long, gganimate also sounds okay to me).

Re: animations in PDF, yes, when fig.show='animate' and the output format is LaTeX, the LaTeX package animate will be used (the animation can only be viewed in Acrobat Reader). In your case, when knitr::is_latex_output(), I think you can just draw all plots. There is nothing else you need to do, since knitr should be able to handle the animation automatically. For R Markdown documents, \usepackage{animate} in the preamble will be needed (I should have automated this).

@thomasp85
Copy link
Owner

@yihui I'm toying with pdf output, and knitr_print.gganim() now just writes a bunch of pdf files - it does seem like knitr is still using ffmpeg for conversion and not a native latex solution, and the resulting pdf does not contain any animation...

If you have the time could you see if it is something in my setup that is not working - an example RMarkdown doc is below. You'll need the latest gganimate from GitHub

---
title: "PDF Animation Test"
output: pdf_document
header-includes:
   - \usepackage{animate}
---

`gganimate` now supports animations inside PDF documents. This feature is only 
viewable with Acrobat Reader, however. Remember to include 
`\usepackage{animate}` in the preamble and set `fig.show='animate'` in the chunk
options:

```{r, fig.show='animate'}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  transition_reveal(Month, Day)
```

yihui added a commit to yihui/knitr that referenced this issue Oct 8, 2018
@yihui
Copy link
Contributor Author

yihui commented Oct 8, 2018

Fixed in the dev version of knitr!

@thomasp85
Copy link
Owner

Yay - will test once I get a computer again

yihui added a commit to SiriChandanaGoruganti/knitr that referenced this issue Dec 9, 2018
@Frank-Sw
Copy link


title: "PDF Animation Test"
output: pdf_document
header-includes:

  • \usepackage{animate}

gganimate now supports animations inside PDF documents. This feature is only
viewable with Acrobat Reader, however. Remember to include
\usepackage{animate} in the preamble and set fig.show='animate' in the chunk
options:

library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  transition_reveal(Month, Day)

Does not work for me;

Always turn "PDF_Animation_Test_files/figure-latex/animation-%d.pdf: No such file or directory"

Could anyone of you guide me? Thanks.

@thomasp85
Copy link
Owner

have you installed the GitHub version of knitr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants