wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

crash/error in case of printing saved plot_grid() object across R release/devel #166

Closed singmann closed 4 years ago

singmann commented 4 years ago

It seems that cowplot::plot_grid() objects created and saved in current R 4.0.2 lead to an error when trying to open them in R devel. The error message is:

Error in grid.Call.graphics(C_setviewport, vp, TRUE) : 
  attempt to set index 26/22 in SET_VECTOR_ELT

One can see exactly this error message on Github actions checks using R devel on MacOS with my afex package: https://github.com/singmann/afex/runs/1017929374?check_suite_focus=true The vignette also load in such saved objects and consequently fail.

Conversely, if a cowplot::plot_grid() object is created and saved in R devel and opens in current/release, it occasionally crashes R (or RStudio). However, this problem depends a bit on the setup. It quite regularly happens in RStudio (on windows) but not in just R. And my Github actions checking the package also fail completely on all OS if it uses binaries created in R-devel.

As a reproducible example, the following is enough:

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()
p2 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()

p3 <- plot_grid(p1, p2, labels = c('A', 'B'), label_size = 12)
save(p1, p2, p3, file = "cowplot_current.rda")
save(p1, p2, p3, file = "cowplot_devel.rda")

load("cowplot_current.rda")
load("cowplot_devel.rda")
p3

The following zip contains both files from my (windows) system, where current was saved in R 4.0.2 and devel in R-devel. cowplot_issue.zip

clauswilke commented 4 years ago

This is almost certainly happening because the grid graphics engine has undergone substantial internal changes recently. There's nothing that can be done about this. In general, it's not a good idea to save plot objects under one R version and open them in another. There's no guarantee that this would ever works. It only works if the internal graphics engine code doesn't change.

singmann commented 4 years ago

I understand. I just found it weird that it did not happen for ggplot plots, but as soon as they are combined by cowplot::plot_grid. Anyway, I found a solution for my package. Thanks for the response.