mercredi 3 juillet 2019

Cannot create PDF by ggplot2 within if-statements

Consider a minimal example: generating an empty plot with ggplot2 and put it into a PDF file. Typically people would do

pdf()
ggplot()
dev.off()

and it works as expected. However, when you wrap these statements into an if-statement, the resulting PDF file becomes corrupted.

if (TRUE) {
    pdf()
    ggplot()
    dev.off()
}

enter image description here

This issue has been reproduced on both Windows 10 and macOS Mojave. I'm using the latest version of R

$ R --version
R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin18.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

Aucun commentaire:

Enregistrer un commentaire