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?
Asked
Active
Viewed 4,095 times
15

Ryan C. Thompson
- 40,856
- 28
- 97
- 159
-
4You might try putting `pdf("NULL")` and `dev.off` around your plot function/object. – Jota Dec 03 '13 at 22:53
-
Won't that create a PDF file called "NULL"? – Ryan C. Thompson Dec 04 '13 at 08:06
-
1It will. Sorry about that. Try calling `suppressWarnings(windows("NULL"))` before your call to your plot function/object. But note that this solution is Windows-specific. – Jota Dec 04 '13 at 15:23
-
@Jota `pdf(file = NULL)` should work, without the quotes. – Dec 22 '20 at 15:17
1 Answers
10
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)) )

agstudy
- 119,832
- 17
- 199
- 261
-
3It seems unnecessary to create a temp file. Could I use /dev/null instead? – Ryan C. Thompson Dec 04 '13 at 08:30