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

Animation files not saved #93

Closed
neuwirthe opened this issue Jul 10, 2018 · 14 comments
Closed

Animation files not saved #93

neuwirthe opened this issue Jul 10, 2018 · 14 comments

Comments

@neuwirthe
Copy link

I am able to create animations in RStudio, but they are only displayed in the Viewer window.
I do not find any files containing the animation so I could use them outside of RStudio.
How can I "catch" the output file.
Also,
plot does work for my configuration (RStudio 1.2.805, R 3.5.1 MacOS 10.13.6)

Here is my example

library(ggplot2)
library(gapminder)
library(gganimate)

ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear') -> p
animate(p,device="png")

and here is what I see when I try plot:

plot(p)
Error in UseMethod("ggplot_gtable") :
no applicable method for 'ggplot_gtable' applied to an object of class "gganim_built"

@thomasp85
Copy link
Owner

The default is to return a magick object which can be saved with image_write()

The plot method is currently down due to some patchwork issues

@neuwirthe
Copy link
Author

Thank you. Perhaps that is worth mentioning in the README.

@gregrs-uk
Copy link

Thank you both for this, but the returned object doesn't seem to be a magick object so I'm unable to save your example plot using image_write:

library(ggplot2)
library(gganimate)

p <- 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')

library(magick)
#> Linking to ImageMagick 6.9.9.39
#> Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

image_write(p, 'test.gif')
#> Error: The 'image' argument is not a magick image object.

Created on 2018-07-22 by the reprex
package
(v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_GB.UTF-8                 
#>  tz       Europe/London               
#>  date     2018-07-22
#> Packages -----------------------------------------------------------------
#>  package     * version    date       source                               
#>  assertthat    0.2.0      2017-04-11 CRAN (R 3.5.0)                       
#>  backports     1.1.2      2017-12-13 CRAN (R 3.5.0)                       
#>  base        * 3.5.0      2018-04-24 local                                
#>  bindr         0.1.1      2018-03-13 CRAN (R 3.5.0)                       
#>  bindrcpp      0.2.2      2018-03-29 CRAN (R 3.5.0)                       
#>  class         7.3-14     2015-08-30 CRAN (R 3.5.0)                       
#>  classInt      0.2-3      2018-04-16 cran (@0.2-3)                        
#>  colorspace    1.3-2      2016-12-14 CRAN (R 3.5.0)                       
#>  compiler      3.5.0      2018-04-24 local                                
#>  crayon        1.3.4      2017-09-16 CRAN (R 3.5.0)                       
#>  datasets    * 3.5.0      2018-04-24 local                                
#>  DBI           1.0.0      2018-05-02 CRAN (R 3.5.0)                       
#>  devtools      1.13.6     2018-06-27 CRAN (R 3.5.0)                       
#>  digest        0.6.15     2018-01-28 CRAN (R 3.5.0)                       
#>  dplyr         0.7.6      2018-06-29 CRAN (R 3.5.1)                       
#>  e1071         1.6-8      2017-02-02 cran (@1.6-8)                        
#>  evaluate      0.11       2018-07-17 CRAN (R 3.5.0)                       
#>  farver        1.0        2018-07-21 Github (thomasp85/farver@d29b48c)    
#>  gdtools       0.1.7      2018-02-27 CRAN (R 3.5.0)                       
#>  gganimate   * 0.9.9.9999 2018-07-22 Github (thomasp85/gganimate@13a9a29) 
#>  ggplot2     * 3.0.0      2018-07-03 CRAN (R 3.5.0)                       
#>  glue          1.3.0      2018-07-17 CRAN (R 3.5.0)                       
#>  graphics    * 3.5.0      2018-04-24 local                                
#>  grDevices   * 3.5.0      2018-04-24 local                                
#>  grid          3.5.0      2018-04-24 local                                
#>  gtable        0.2.0      2016-02-26 CRAN (R 3.5.0)                       
#>  hms           0.4.2      2018-03-10 CRAN (R 3.5.0)                       
#>  htmltools     0.3.6      2017-04-28 CRAN (R 3.5.0)                       
#>  knitr         1.20       2018-02-20 CRAN (R 3.5.0)                       
#>  lazyeval      0.2.1      2017-10-29 CRAN (R 3.5.0)                       
#>  lpSolve       5.6.13     2015-09-19 cran (@5.6.13)                       
#>  magick      * 1.9        2018-05-11 cran (@1.9)                          
#>  magrittr      1.5        2014-11-22 CRAN (R 3.5.0)                       
#>  memoise       1.1.0      2017-04-21 CRAN (R 3.5.0)                       
#>  methods     * 3.5.0      2018-04-24 local                                
#>  munsell       0.5.0      2018-06-12 CRAN (R 3.5.0)                       
#>  patchwork     0.0.1      2018-07-21 Github (thomasp85/patchwork@7fb35b1) 
#>  pillar        1.3.0      2018-07-14 CRAN (R 3.5.0)                       
#>  pkgconfig     2.0.1      2017-03-21 CRAN (R 3.5.0)                       
#>  plyr          1.8.4      2016-06-08 CRAN (R 3.5.0)                       
#>  prettyunits   1.0.2      2015-07-13 cran (@1.0.2)                        
#>  progress      1.2.0      2018-06-14 cran (@1.2.0)                        
#>  purrr         0.2.5      2018-05-29 CRAN (R 3.5.0)                       
#>  R6            2.2.2      2017-06-17 CRAN (R 3.5.0)                       
#>  Rcpp          0.12.17    2018-05-18 CRAN (R 3.5.0)                       
#>  rlang         0.2.1      2018-05-30 CRAN (R 3.5.0)                       
#>  rmarkdown     1.10       2018-06-11 CRAN (R 3.5.0)                       
#>  rprojroot     1.3-2      2018-01-03 CRAN (R 3.5.0)                       
#>  scales        0.5.0      2017-08-24 CRAN (R 3.5.0)                       
#>  sf            0.6-3      2018-05-17 cran (@0.6-3)                        
#>  spData        0.2.9.0    2018-06-17 cran (@0.2.9.0)                      
#>  stats       * 3.5.0      2018-04-24 local                                
#>  stringi       1.2.4      2018-07-20 CRAN (R 3.5.0)                       
#>  stringr       1.3.1      2018-05-10 CRAN (R 3.5.0)                       
#>  svglite       1.2.1      2017-09-11 cran (@1.2.1)                        
#>  tibble        1.4.2      2018-01-22 CRAN (R 3.5.0)                       
#>  tidyselect    0.2.4      2018-02-26 CRAN (R 3.5.0)                       
#>  tools         3.5.0      2018-04-24 local                                
#>  transformr    0.1.0      2018-07-21 Github (thomasp85/transformr@f163c57)
#>  tweenr        0.1.5.9999 2018-07-21 Github (thomasp85/tweenr@4d4f8d1)    
#>  units         0.6-0      2018-06-09 cran (@0.6-0)                        
#>  utils       * 3.5.0      2018-04-24 local                                
#>  withr         2.1.2      2018-03-15 CRAN (R 3.5.0)                       
#>  yaml          2.1.19     2018-05-01 CRAN (R 3.5.0)

@gregrs-uk
Copy link

I had missed out the animate stage. For anyone visiting this issue, the following should work:

library(ggplot2)
library(gganimate)

p <- 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')

image <- animate(p)

library(magick)
#> Linking to ImageMagick 6.9.9.39
#> Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
#> Disabled features: fftw, ghostscript, x11
image_write(image, 'test.gif')

Created on 2018-07-22 by the reprex
package
(v0.2.0).

@gregrs-uk
Copy link

@thomasp85 Would you like me to add an example of saving an image file to the README?

@thomasp85
Copy link
Owner

I'll do it - I still haven't finalised how this should work out in the end, e.g. if an equivalent of ggsave() should be provided

@gregrs-uk
Copy link

Sure, no problem. Thanks.

@volha-tryputsen
Copy link

Hi all, I am working on embedding animated gif into html report (though rendering .Rmd) and ran into the same issues as @gregrs-uk when saving image into .gif

image_write(image, 'test.gif')
Error: The 'image' argument is not a magick image object.
@thomasp85 , has anything changes since a month ago?
Thank you!

sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] magick_1.9 bindrcpp_0.2 gganimate_0.9.9.9999 dplyr_0.7.4 purrr_0.2.4
[6] readr_1.1.1 tidyr_0.8.0 tibble_1.4.2 tidyverse_1.1.1 forcats_0.2.0
[11] ggridges_0.4.1 PMCMR_4.2 car_3.0-0 carData_3.0-1 knitr_1.17
[16] multcomp_1.4-8 TH.data_1.0-8 MASS_7.3-47 mvtnorm_1.0-6 rms_5.1-1
[21] SparseM_1.77 Hmisc_4.0-3 Formula_1.2-2 survival_2.41-3 lattice_0.20-35
[26] caTools_1.17.1 gridExtra_2.3 plotly_4.7.1 ggplot2_3.0.0 readxl_1.0.0
[31] janitor_0.3.1 glue_1.3.0

loaded via a namespace (and not attached):
[1] colorspace_1.3-2 class_7.3-14 rio_0.5.10 rprojroot_1.2 htmlTable_1.9
[6] base64enc_0.1-3 rstudioapi_0.7 farver_1.0 MatrixModels_0.4-1 lubridate_1.6.0
[11] xml2_1.1.1 codetools_0.2-15 splines_3.4.1 mnormt_1.5-5 jsonlite_1.5
[16] broom_0.4.2 cluster_2.0.6 png_0.1-7 shiny_1.0.5 compiler_3.4.1
[21] httr_1.3.1 backports_1.1.0 assertthat_0.2.0 Matrix_1.2-10 lazyeval_0.2.1
[26] tweenr_0.1.5.9999 acepack_1.4.1 htmltools_0.3.6 quantreg_5.34 prettyunits_1.0.2
[31] tools_3.4.1 gtable_0.2.0 reshape2_1.4.2 Rcpp_0.12.18 cellranger_1.1.0
[36] nlme_3.1-131 transformr_0.1.0 crosstalk_1.0.0 psych_1.7.5 stringr_1.3.0
[41] openxlsx_4.0.17 rvest_0.3.2 mime_0.5 lpSolve_5.6.13 devtools_1.13.4
[46] polspline_1.1.12 zoo_1.8-0 scales_1.0.0 hms_0.3 parallel_3.4.1
[51] sandwich_2.4-0 RColorBrewer_1.1-2 yaml_2.1.14 curl_3.1 memoise_1.1.0
[56] rpart_4.1-11 latticeExtra_0.6-28 stringi_1.1.7 highr_0.6 e1071_1.6-8
[61] gifski_0.8.3 checkmate_1.8.4 spData_0.2.9.3 rlang_0.2.2 pkgconfig_2.0.1
[66] bitops_1.0-6 evaluate_0.10.1 bindr_0.1 sf_0.6-3 labeling_0.3
[71] patchwork_0.0.1 htmlwidgets_1.2 plyr_1.8.4 magrittr_1.5 R6_2.2.2
[76] DBI_1.0.0 pillar_1.2.1 haven_1.1.0 foreign_0.8-69 withr_2.1.1
[81] units_0.6-0 abind_1.4-5 nnet_7.3-12 modelr_0.1.1 crayon_1.3.4
[86] rmarkdown_1.6 progress_1.2.0 grid_3.4.1 data.table_1.10.4 git2r_0.20.0
[91] classInt_0.2-3 digest_0.6.13 xtable_1.8-2 httpuv_1.3.5 munsell_0.5.0
[96] viridisLite_0.3.0 kableExtra_0.6.0

@thomasp85
Copy link
Owner

There shouldn’t be any need for additional steps when embedding animations with rmarkdown. Just treat it like a regular ggplot

@volha-tryputsen
Copy link

Thanks, Thomas, it worked 🥇
After killing half of the day trying to come up with complicated solution, turned out that the simplicity is the genius! My html report looks beautiful with the animated viz! Janssen R&D is getting such a makeover thanks to all the wonderful work you are doing!

@thomasp85
Copy link
Owner

Happy you got it to work😊

@ChrisBeeley
Copy link

Just for any confused Linux people out there, I get the

#> Error: The 'image' argument is not a magick image object. 

error message with Ubuntu 16.04 but not with 17.10, I presume because of an older version of ImageMagick. I'm guessing to get it running on 16.04 you'll have to find another repository or compile from source. I haven't done this myself so I can't be sure.

@thomasp85
Copy link
Owner

gganimate does not use ImageMagick in any way. It has changed default renderer to gifski which is why image_write no longer works and the returned object. anim_save() will always work no matter the renderer

@ChrisBeeley
Copy link

Aha. Apologies. I'll do that then. Thanks 😀

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

5 participants