Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to silence the output from this R package?

Tags:

r

I'm playing a bit with the LowRankQP() package in R, and even setting verbose=FALSE still produces a lot of outputs (see example below).

The outputs are coming from the compiled part of the code. Is there a way (a wrapping function?) in R to make a call to LowRankQP() absolutely silent (i.e. not print anything on the screen) without modifying the underlying compiled code (neither of the email addresses associated with this package is still active)?

library(LowRankQP)

Vmat <- matrix(0,6,6)
diag(Vmat) <- c(1, 1,1,0,0,0)
dvec <- c(0,-5,0,0,0,0)
Amat <- matrix(c(-4,-3,0,-1,0,0,2,1,0,0,-1,0,0,-2,1,0,0,-1),6,3)
bvec <- c(-8,2,0)
uvec <- c(100,100,100,100,100,100)

aa<-LowRankQP(Vmat,dvec,t(Amat),bvec,uvec,method="CHOL")

# LowRankQP CONVERGED IN 15 ITERATIONS
# 
#     Primal Feasibility    =   2.5719308e-16
#     Dual Feasibility      =   7.1949984e-16
#     Complementarity Value =   3.3066705e-11
#     Duality Gap           =   3.3065273e-11
#     Termination Condition =   9.7802929e-12

It's the part that starts with "LowRankQP CONVERGED IN 15 ITERATIONS" that i want away with..

Ubuntu 11.04, R version 2.12.1 and LowRankQP() 1.0.1.

like image 375
user189035 Avatar asked May 30 '11 14:05

user189035


1 Answers

sink(file=NULL) does NOT work, since it closes the last sink, nothing more.

sink(file=NULL) Warning message: In sink(file = NULL) : no sink to remove

What does work though is:

f = file()
sink(file=f) ## silence upcoming output using anonymous file connection
... your code here ...
sink() ## undo silencing
close(f)

Using an anonymous file has the advantage of being platform-agnostic, i.e. you don't have to come up with a temporary file name.

Example:

f = file()
sink(file=f)
example(glm)
sink()
close(f)

I've used sink() successfully for other functions (e.g. normalmixEM2comp {mixtools}).

(edit: the first version of this post did not use an explicit file handle, and thus gave a warning -- or even an error if you call the above snippet often enough). This is now fixed by using close(f).

like image 106
cbielow Avatar answered Oct 07 '22 05:10

cbielow