Using Rscript, is there a decent way to suppress the non-script output? [duplicate]
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: methodsLoading 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 --vanillacmd_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: methodsLoading required package: gmpAttaching 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!