MomX / Momocs

:dove: Morphometrics using R
http://momx.github.io/Momocs/
51 stars 18 forks source link

plot_PCA not working when plotting to file #214

Closed wol101 closed 3 years ago

wol101 commented 3 years ago

Momocs is great but I have come across an issue. Here is a simple test program using one of your examples:

library(Momocs)
bp <- bot %>% mutate(fake=sample(letters[1:5], 40, replace=TRUE)) %>%
efourier(6) %>% PCA
plot_PCA(bp)

pdf()
plot_PCA(bp)
dev.off()

Running in RStudio the first plot_PCA(bp) works fine and I get the plot in the plots window. However the pdf() to dev.off() bit throws an error:

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : 
  plot.new has not been called yet

If I replace plot_PCA with plot then it all works as expected, so it looks like plot_PCA is closing the graphics device when saving to pdf somewhere. The work around is to save the RStudio plot window to pdf which works OK but with a lot of plots it is easier to write them directly to files.

This is with a fresh install of R4.0.3 and Momocs from github, but I get the same error with R3.6.3 and the cran version of Momocs. I've tried it on Windows and Ubuntu.

vbonhomme commented 3 years ago

Does pdf("foo.pdf") help ?

Le jeu. 7 janv. 2021 19:01, Bill Sellers notifications@github.com a écrit :

Momocs is great but I have come across an issue. Here is a simple test program using one of your examples:

library(Momocs) bp <- bot %>% mutate(fake=sample(letters[1:5], 40, replace=TRUE)) %>% efourier(6) %>% PCA plot_PCA(bp)

pdf() plot_PCA(bp) dev.off()

Running in RStudio the first plot_PCA(bp) works fine and I get the plot in the plots window. However the pdf() to dev.off() bit throws an error:

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet

If I replace plot_PCA with plot then it all works as expected, so it looks like plot_PCA is closing the graphics device when saving to pdf somewhere. The work around is to save the RStudio plot window to pdf which works OK but with a lot of plots it is easier to write them directly to files.

This is with a fresh install of R4.0.3 and Momocs from github, but I get the same error with R3.6.3 and the cran version of Momocs. I've tried it on Windows and Ubuntu.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/MomX/Momocs/issues/214, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABECQFSPWLCY7ITNK7ZMZYTSYXZF5ANCNFSM4VZKFTCA .

wol101 commented 3 years ago

No, pdf("foo.pdf") gives the same error. It's a bit odd because I did manage to get it working on one machine briefly but then it stopped. However I was doing a whole bunch of other stuff and once I'd written the short example it seems to reliably give the error. There is a call list for the error too (in R4.0.3)

Calls: plot_PCA -> %>% -> layer_axes -> abline -> int_abline
vbonhomme commented 3 years ago

Thanks. Gonna have a look asap. Could you try with png() ? And with a couple of dev.off()before ?

Le jeu. 7 janv. 2021 20:23, Bill Sellers notifications@github.com a écrit :

No, pdf("foo.pdf") gives the same error. It's a bit odd because I did manage to get it working on one machine briefly but then it stopped. However I was doing a whole bunch of other stuff and once I'd written the short example it seems to reliably give the error. There is a call list for the error too (in R4.0.3)

Calls: plot_PCA -> %>% -> layer_axes -> abline -> int_abline

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/MomX/Momocs/issues/214#issuecomment-756326609, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABECQFWFX24ED4BXVVJ52S3SYYC25ANCNFSM4VZKFTCA .

wol101 commented 3 years ago

Thanks! Just tried with png and it gives the same error. Adding extra dev.off() lines makes no difference as far as I can tell.

wol101 commented 3 years ago

Even shorter demonstration using Rscript --vanilla so that there shouldn't be anything cluttering up my workspace and the pdf output is set by default:

 "c:\Program Files\R\R-4.0.3\bin\x64\Rscript.exe" --vanilla test.R

This test.R works and produces the expected Rplots.pdf file.

library(Momocs)
bp <- bot %>% mutate(fake=sample(letters[1:5], 40, replace=TRUE)) %>% efourier(6) %>% PCA
plot(bp)

This test.R fails with the plot.new has not been called yet error:

library(Momocs)
bp <- bot %>% mutate(fake=sample(letters[1:5], 40, replace=TRUE)) %>% efourier(6) %>% PCA
plot_PCA(bp)
vbonhomme commented 3 years ago

quite a strange error that buggled me a bit. apparently plotting within a pipe was not producing the plot and abline in layer_axes was trying to draw on nothing. It is now done in two steps and this works on my machine (this may be due to dependencies updates, don't really know).

as a side remark, plot(bp) uses the old plotting system, plot_PCA(bp) uses the new one (grindr attempt), they more or less look the same but are built on completely different schemes.

Le ven. 8 janv. 2021 à 09:39, Bill Sellers notifications@github.com a écrit :

Even shorter demonstration using Rscript --vanilla so that there shouldn't be anything cluttering up my workspace and the pdf output is set by default:

"c:\Program Files\R\R-4.0.3\bin\x64\Rscript.exe" --vanilla test.R

This test.R works and produces the expected Rplots.pdf file.

library(Momocs) bp <- bot %>% mutate(fake=sample(letters[1:5], 40, replace=TRUE)) %>% efourier(6) %>% PCA plot(bp)

This test.R fails with the plot.new has not been called yet error:

library(Momocs) bp <- bot %>% mutate(fake=sample(letters[1:5], 40, replace=TRUE)) %>% efourier(6) %>% PCA plot_PCA(bp)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/MomX/Momocs/issues/214#issuecomment-756626649, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABECQFQGHUQYAMQUAS5EOULSY3AE3ANCNFSM4VZKFTCA .

-- vincentbonhomme.fr http://www.vincentbonhomme.fr

vbonhomme commented 3 years ago

Could you try to reinstall with: devtools::install_github("MomX/Momocs") and tell me if it solves the problem please ?

wol101 commented 3 years ago

That's fixed it. I installed the version from github and it works as expected now. Thanks for fixing this so rapidly.

vbonhomme commented 3 years ago

Thanks for pointing it ;-)