加载软件包时禁用消息 [英] Disable messages upon loading a package

查看:93
本文介绍了加载软件包时禁用消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R(ROCR)中有一个需要在我的R环境中加载的软件包.装入包装后,将打印一组消息.通常,这很好,但是由于我的R脚本的输出正用于进一步分析,因此我想完全禁用所有这些输出.我怎么做?此外,我宁愿完全不必修改ROCR来执行此操作,这样该脚本的未来用户也不必这样做.

I have a package in R (ROCR) that I need to load in my R environment. Upon loading the package, a set of messages are printed. This is ordinarily fine, but since the output of my R script is being used for further analysis I want to completely disable all of this output. How do I do that? Furthermore, I'd prefer to do it without having to modify ROCR at all, so that future users of this script don't have to do that either.

到目前为止:

  • sink()在这里不起作用-将stdout和std err都重定向到/dev/null对我没有任何作用.
  • 不足为奇的是,options(warnings=-1)也不执行任何操作,因为这些警告本身并不是警告.
  • sink() doesn't work here - redirecting both stdout and std err to /dev/null does nothing for me.
  • Unsurprisingly, options(warnings=-1) does nothing either, since these are not warnings, per se, being printed.

有什么想法吗?

推荐答案

只需在library()调用周围使用suppressMessages():

edd@max:~$ R

R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)
[...]

R> suppressMessages(library(ROCR))
R>                                               # silently loaded
R> search() 
 [1] ".GlobalEnv"         "package:ROCR"         # it's really there      
 [3] "package:gplots"     "package:KernSmooth"
 [5] "package:grid"       "package:caTools"   
 [7] "package:bitops"     "package:gdata"     
 [9] "package:gtools"     "package:stats"     
[11] "package:graphics"   "package:grDevices" 
[13] "package:utils"      "package:datasets"  
[15] "package:methods"    "Autoloads"         
[17] "package:base"      
R> 

这篇关于加载软件包时禁用消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆