Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

I am using a function in R (specifically limma::plotMDS) that produces a plot and also returns a useful value. I want to get the returned value without producing the plot. Is there an easy way to call the function but suppress the plot that it creates?

like image 300
Ryan C. Thompson Avatar asked Dec 03 '13 22:12

Ryan C. Thompson


1 Answers

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)) )
like image 89
agstudy Avatar answered Oct 10 '22 02:10

agstudy