How can I suppress plot creation when calling a function in R?

I use a function in R (specifically limma::plotMDS ) that creates a graph and also returns a useful value. I want to get the return value without creating a plot. Is there an easy way to call a function, but suppress the plot that it creates?

+6
source share
1 answer

You can wrap a function call as follows:

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

Call example:

 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)) ) 
+3
source

Source: https://habr.com/ru/post/959286/