How can I suppress the creation of a plot while calling a function in R?

Solution 1:

You can wrap the function call like this :

plotMDS.invisible <- function(...){
    ff <- tempfile()
    png(filename=ff)
    res <- plotMDS(...)
    dev.off()
    unlink(ff)
    res
}

An example of call :

x <- matrix(rnorm(1000*6,sd=0.5),1000,6)
rownames(x) <- paste("Gene",1:1000)
x[1:50,4:6] <- x[1:50,4:6] + 2
# without labels, indexes of samples are plotted.
mds <- plotMDS.invisible(x,  col=c(rep("black",3), rep("red",3)) )