11

Possible Duplicate:
R suppress startupMessages from dependency

I've read about using sink("NUL") / sink("/dev/null"), but neither of them has fixed the problem I'm having. Even if I wrap the library() commands in sink("NUL") and sink(), my call to Rscript outputs all manner of information I don't want to see:

Loading required package: Matrix
Loading required package: methods
Loading required package: lattice
Loaded glmnet 1.8

Loading required package: MASS
Loading required package: lme4

Attaching package: 'lme4'

The following object(s) are masked from 'package:stats':

    AIC, BIC

Loading required package: R2WinBUGS
Loading required package: coda

Attaching package: 'coda'

The following object(s) are masked from 'package:lme4':

    HPDinterval

Loading required package: abind
Loading required package: foreign

arm (Version 1.5-05, built: 2012-6-6)

Working directory is C:/Users/andrews/bootstraps/user/_branches/ER-PoC/Bootstraps/R


Attaching package: 'arm'

The following object(s) are masked from 'package:coda':

    traceplot

[1] "client=51"         "date='01-01-2011'"
[1] "01-01-2011"
[1] 51

The stuff at the end is the only output I actually want, and also the only output I seem able to suppress with sink() commands. It really seems like there should just be an argument to Rscript that suppresses this output (which doesn't even show up if I source my script in the console)... any input?

Community
  • 1
  • 1
Claire Sannier
  • 902
  • 2
  • 8
  • 19
  • 6
    perhaps `?suppressPackageStartupMessages` will help? – Chase Aug 07 '12 at 22:38
  • 3
    Just wanted to mention in regard to closing as duplicate that this question is asking about a script and is being marked as a duplicate of a question speaking in terms of a package. So the 'silent' namespace method by Joris wouldn't even be used, and `suppressMessages()` doesn't do a full suppression when used as the OP stated. So in neither way does the 'exact duplicate' help this user. – Thell Aug 08 '12 at 15:12
  • Thanks, @Thell. I had already read the identified post when I asked this question, so I'm glad you agree. – Claire Sannier Aug 08 '12 at 15:31

1 Answers1

9

Andrew, I ran into the same thing and suppressMessages() didn't remove all the extra output, but using sink() in the form of capture.output() wrapped around the suppressMessages() works.

$ rscript --vanilla -e 'library(Rmpfr)'
Loading required package: methods
Loading required package: gmp
---->8----
Loading C code of R package 'Rmpfr': GMP using 32 bits per limb
---->8----


$ rscript --vanilla -e 'suppressMessages( library(Rmpfr) )'
Loading C code of R package 'Rmpfr': GMP using 32 bits per limb


$ rscript --vanilla -e 'msg.out <- capture.output( suppressMessages( library(Rmpfr) ) )'

What is going on when loading the Rmpfr package is several well behaved startup messages written using the message connection along with a not so nice message using the output connection. Sure, you could create and manipulate a sink() on your own, but that is what capture.output() is already setup to do.

Perhaps setting a verbose arg to get a little more control would be helpful::

$ cat sample.R
#!/c/opt/R/R-2.15.0/bin/rscript --vanilla

cmd_args <- commandArgs( TRUE );

if( length( cmd_args ) > 0 ) {
  eval( parse( text = cmd_args[1] ) )
}

if( exists( "verbose" ) ) {
  library( Rmpfr )
} else {
  msg.trap <- capture.output( suppressMessages( library( Rmpfr ) ) )
}

print("Hello")

Which yields::

$ ./sample.R
[1] "Hello"


$ ./sample.R "verbose=TRUE"
Loading required package: methods
Loading required package: gmp

Attaching package: 'gmp'
---->8----
[1] "Hello"

Lots of stuff you could play around with there, but at least you can see how to totally suppress the msg output.

Hope it helps. Have fun!

Thell
  • 5,883
  • 31
  • 55
  • This is looking like it's going to be the solution. I should have added that my main script sources four others to begin with, and that the `library()` statements are all in these ancillary scripts. Even so, wrapping the `source()` calls in `capture.output(suppressMessages())` almost works... now, I just have a line of output `character(0)` for each of the `source()` calls before the desired output. Any idea what that might be? Thanks a lot either way! – Claire Sannier Aug 08 '12 at 15:17
  • At there it is: just using `suppressMessages()` without `capture.output()` solves the whole problem. Thanks again! – Claire Sannier Aug 08 '12 at 15:19
  • 1
    The reason you received a `character(0)` is that you didn't assign the captured output to anything so it was printed and in your particular case the whole message had already been suppressed. Be aware that `suppressMessages()` by itself will not always leave you with an empty character array; just as with the second example command above, where Rmpfr spits out an initialization message even after using `suppressMessages()`. – Thell Aug 08 '12 at 18:06