malucalle / selbal

selbal: selection of balances for microbial signatures
32 stars 15 forks source link

selbal.cv creates a blank plot file Rplots.pdf when run non-interactively #24

Open mmp3 opened 3 years ago

mmp3 commented 3 years ago

Consider an R script file foo.R that calls selbal.cv.

For example, we follow the selbal vignette:

foo.R:

  library(selbal)
  # Define x, y and z
  x <- HIV[,1:60]
  y <- HIV[,62]
  z <- data.frame(MSM = HIV[,61])

# Run selbal.cv function (with the default values for zero.rep and opt.cri)
  CV.BAL.dic <- selbal.cv(x = x, y = y, n.fold = 5, n.iter = 10,
                          covar = z, logit.acc = "AUC")

And then we execute this in an R console:

~$ R
> source("foo.R")

The command completes successfully, but it creates a file Rplots.pdf in the working directory. This file is a blank page.

However, if I execute selbal.cv interactively, i.e. I physically type in or copy-and-paste the selbal.cv command into the console, then it does not create a file called Rplots.pdf:

~$ R
> library(selbal)
> x <- HIV[,1:60]
> y <- HIV[,62]
> z <- data.frame(MSM = HIV[,61])
> CV.BAL.dic <- selbal.cv(x = x, y = y, n.fold = 5, n.iter = 10, covar = z, logit.acc = "AUC")

(does not create a file named Rplots.pdf)

UVic-omics commented 3 years ago

Hi @mmp3!

To save a plot as a PDF you can do the following (to save the global balance for example)

1) Run the code 2) Run the following comands:


# Modify the properties of your plot in order to visualize it properly (width, height, . . .) and open a PDF document
pdf("MyPlot.pdf")
# Represent the plot
grid.draw(CV.BAL.dic$global.plot)
# Close and save theh plot
dev.off()

At the end, you will get the PDF document with the representation at the directory you are working at.

mmp3 commented 3 years ago

Hi @UVic-omics Thank you for the suggestion.

The problem is not with individual plots (global.plot, accuracy.nvar, etc.). I save those to files afterwards as you indicated.

Instead, the problem is with the initial call to selbal.cv itself. It should not be plotting anything, but it apparently does. It prints a blank page.

malucalle commented 3 years ago

Hi @mmp3, I executed the R script file foo.R and no blank page pdf was created