我读过关于使用sink("NUL") / sink("/dev/null")的文章,但它们都没有解决我遇到的问题。即使我将library()命令包装在sink("NUL")和sink()中,我对Rscript的调用也输出了我不想看到的所有信息:
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最后的内容是我真正想要的唯一输出,也是我似乎能够用sink()命令抑制的唯一输出。实际上,似乎应该有一个Rscript参数来抑制这个输出(如果我在控制台中source脚本,它甚至不会显示出来).有意见吗?
发布于 2012-08-08 05:24:16
安德鲁,我遇到了同样的情况,suppressMessages()并没有删除所有额外的输出,但是使用sink()形式的capture.output()包装了suppressMessages()工作。
$ 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) ) )'加载Rmpfr包时发生的事情是使用message连接编写的几条行为良好的启动消息,以及使用output连接编写的不太好的消息。当然,您可以自己创建和操作一个sink(),但这正是capture.output()已经要做的。
也许设置一个冗长的arg来获得更多的控制是有帮助的:
$ 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")产生的结果:
$ ./sample.R
[1] "Hello"
$ ./sample.R "verbose=TRUE"
Loading required package: methods
Loading required package: gmp
Attaching package: 'gmp'
---->8----
[1] "Hello"很多东西你可以在那里玩,但至少你可以看到如何完全抑制msg输出。
希望能帮上忙。玩得开心!
https://stackoverflow.com/questions/11855072
复制相似问题