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

Possible bad interaction between glue and gganimate causing errors in animate() and anim_save() #422

Closed
jmcastagnetto opened this issue Dec 5, 2020 · 1 comment

Comments

@jmcastagnetto
Copy link

There seems to be a problem when using glue to interpolate values from a data.frame variable if we also want to make an animation with gganimate, perhaps is some parsing of "{variables}" in gganimate when generating the temporary frames. Found this problem when generating some animations recently

Here I am using an example using the Palmer Penguins dataset. First we generate two identical static plots, with and without using glue, both work

library(tidyverse)
library(gganimate)
library(palmerpenguins)
data(penguins)

p0 <- ggplot(
  penguins %>%
    mutate(
      sex = as.character(sex) %>% replace_na("unknown"),
      coolness = as.factor(sample(1:5, nrow(penguins), replace = TRUE))
    ) %>%
    filter(!is.na(flipper_length_mm)),
  aes(x = bill_length_mm, y = flipper_length_mm,
      group = species, color = coolness)
) +
  labs(
    title = "Palmer Penguines dataset - Species: {closest_state}",
    subtitle = paste0(
      "Island(s): ",
      origin = paste(unique(penguins$island), collapse = ", ")
    )
  ) +
  facet_wrap(~sex)

p1 <- p0 +
  labs(caption = "No glue") +
  geom_text(aes(label = paste0("[", coolness, "]")), show.legend = FALSE)

p2 <- p0 +
  labs(caption = "With glue") +
  geom_text(aes(label = glue::glue("[{coolness}]")), show.legend = FALSE)

Here is p1

p1

And p2 (identical)

p2

Now, when if we try to generate an animation, the one using p2 fails:

# This code works ---------------------------------------------------------
anim1 <- p1 +
  transition_states(
    states = species
  )

animate(
  anim1,
  width = 600,
  height = 300,
  nframes = 30,
  fps = 10
)

# This code generates an error --------------------------------------------
anim2 <- p2 +
  transition_states(
    states = species
  )

animate(
  anim2,
  width = 600,
  height = 300,
  nframes = 30,
  fps = 10
)

The last part above gives the following error on my system:

2020-12-05_14-48-03

For reference, here is a gif generated from anim1

anim_save(
  animation = anim1,
  filename = "penguins_animation.gif",
  width = 600,
  height = 300,
  nframes = 30,
  fps = 10
)

penguins_animation

And here is my sessionInfo()

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] palmerpenguins_0.1.0 gganimate_1.0.7      forcats_0.5.0       
 [4] stringr_1.4.0        dplyr_1.0.2          purrr_0.3.4         
 [7] readr_1.4.0          tidyr_1.1.2          tibble_3.0.4        
[10] ggplot2_3.3.2        tidyverse_1.3.0     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5        plyr_1.8.6        pillar_1.4.7      compiler_4.0.3   
 [5] cellranger_1.1.0  dbplyr_2.0.0      prettyunits_1.1.1 progress_1.2.2   
 [9] tools_4.0.3       digest_0.6.27     jsonlite_1.7.1    lubridate_1.7.9.2
[13] lifecycle_0.2.0   gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.9      
[17] reprex_0.3.0      cli_2.2.0         DBI_1.1.0         rstudioapi_0.13  
[21] yaml_2.2.1        haven_2.3.1       withr_2.3.0       xml2_1.3.2       
[25] httr_1.4.2        fs_1.5.0          generics_0.1.0    vctrs_0.3.5      
[29] rsthemes_0.1.0    hms_0.5.3         grid_4.0.3        tidyselect_1.1.0 
[33] glue_1.4.2        R6_2.5.0          gifski_0.8.6      fansi_0.4.1      
[37] readxl_1.3.1      farver_2.0.3      tweenr_1.0.1      modelr_0.1.8     
[41] magrittr_2.0.1    backports_1.2.0   scales_1.1.1      ellipsis_0.3.1   
[45] rvest_0.3.6       assertthat_0.2.1  colorspace_2.0-0  labeling_0.4.2   
[49] stringi_1.5.3     munsell_0.5.0     broom_0.7.2       crayon_1.3.4
@jmcastagnetto jmcastagnetto changed the title Possible bad interactiong between glue and gganimate causing errors in animate() and anim_save() Possible bad interaction between glue and gganimate causing errors in animate() and anim_save() Dec 5, 2020
@jmcastagnetto
Copy link
Author

jmcastagnetto commented Mar 8, 2021

Problem still persists as of 2021-03-07

> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] palmerpenguins_0.1.0 gganimate_1.0.7      forcats_0.5.1       
 [4] stringr_1.4.0        dplyr_1.0.5          purrr_0.3.4         
 [7] readr_1.4.0          tidyr_1.1.3          tibble_3.1.0        
[10] ggplot2_3.3.3        tidyverse_1.3.0     

loaded via a namespace (and not attached):
 [1] progress_1.2.2    tidyselect_1.1.0  haven_2.3.1       colorspace_2.0-0 
 [5] vctrs_0.3.6       generics_0.1.0    utf8_1.1.4        rlang_0.4.10     
 [9] pillar_1.5.1      glue_1.4.2        withr_2.4.1       DBI_1.1.1        
[13] tweenr_1.0.1      dbplyr_2.1.0      modelr_0.1.8      readxl_1.3.1     
[17] plyr_1.8.6        lifecycle_1.0.0   munsell_0.5.0     gtable_0.3.0     
[21] cellranger_1.1.0  rvest_0.3.6       labeling_0.4.2    fansi_0.4.2      
[25] gifski_0.8.7      broom_0.7.5       Rcpp_1.0.6        scales_1.1.1     
[29] backports_1.2.1   debugme_1.1.0     jsonlite_1.7.2    farver_2.1.0     
[33] fs_1.5.0          hms_1.0.0         stringi_1.5.3     grid_4.0.4       
[37] cli_2.3.1         tools_4.0.4       magrittr_2.0.1    crayon_1.4.1     
[41] pkgconfig_2.0.3   ellipsis_0.3.1    xml2_1.3.2        prettyunits_1.1.1
[45] reprex_1.0.0      lubridate_1.7.10  assertthat_0.2.1  httr_1.4.2       
[49] rstudioapi_0.13   R6_2.5.0          compiler_4.0.4   

In case you want to grab the sample code above, it is at: https://github.com/jmcastagnetto/misc-viz/blob/main/20201205-bad-interaction-glue-gganimate.R

VolodiaPG pushed a commit to VolodiaPG/gganimate that referenced this issue May 26, 2023
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

1 participant