可能重复:
R从依赖项中禁止startupMessages
我读过关于使用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
Run Code Online (Sandbox Code Playgroud)
最后的东西是我真正想要的唯一输出,也是我似乎能用sink()
命令抑制的唯一输出.看起来应该只有一个参数来Rscript
抑制这个输出(如果我source
在控制台中我的脚本,它甚至都没有出现)...任何输入?
安德鲁,我遇到了同样的事情并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) ) )'
Run Code Online (Sandbox Code Playgroud)
加载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")
Run Code Online (Sandbox Code Playgroud)
产量::
$ ./sample.R
[1] "Hello"
$ ./sample.R "verbose=TRUE"
Loading required package: methods
Loading required package: gmp
Attaching package: 'gmp'
---->8----
[1] "Hello"
Run Code Online (Sandbox Code Playgroud)
你可以在那里玩很多东西,但至少你可以看到如何完全抑制msg输出.
希望能帮助到你.玩得开心!