加载包时禁用消息
我有一个 R (ROCR) 包,需要将其加载到我的 R 环境中。加载包后,会打印一组消息。这通常很好,但由于我的 R 脚本的输出用于进一步分析,我想完全禁用所有这些输出。我该怎么做?此外,我更愿意在根本不需要修改 ROCR 的情况下执行此操作,以便该脚本的未来用户也不必这样做。
到目前为止:
sink()
在这里不起作用 - 将 stdout 和 std err 重定向到/dev/null
对我没有任何作用。- 不出所料,
options(warnings=-1)
也不执行任何操作,因为这些本身并不是被打印的警告。
有什么想法吗?
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.
So far:
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.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需在
library()
调用周围使用suppressMessages()
即可:Just use
suppressMessages()
around yourlibrary()
call:Dirk 的回答会抑制所有消息,并且不特定于加载包时生成的消息。
对所问问题的更准确的解决方案是:
可以在此处找到更详细的解释
Dirk's answer suppresses all messages and is not specific to messages that is generated while loading packages.
The more accurate solution to the asked question is:
A bit more detailed explanation can be found here
使用
suppressPackageStartupMessages
,请参阅MehradMahmoudian<的答案/em>.为了完整起见,在此处添加使用示例:对于一个库,使用
suppressPackageStartupMessages(...)
,例如:对于多个库,使用
suppressPackageStartupMessages({...})
,例如:另请参阅:
禁止包启动消息
Use
suppressPackageStartupMessages
, see the answer by MehradMahmoudian. For completeness, adding here examples of usage:For one library, use
suppressPackageStartupMessages(...)
, for example:For multiple libraries, use
suppressPackageStartupMessages({...})
, for example:SEE ALSO:
Suppress package startup messages
库(ROCR,安静= TRUE)可能是一个更优雅的选择。
library(ROCR, quietly = TRUE)
might be a more elegant option.如果您使用 for 循环加载包,那么您必须像下面所示的那样静默整个循环,而不是在单独加载库时抑制消息。
If you load packages with a for loop, then you have to silent the complete loop like I show below instead of suppressing the message when loading the library individually.
通过添加
quietly = T
如下所示将解决该问题:如果有多个包,您可以使用 :
并且它们使用 lapply,如下所示:
By adding
quietly = T
as shown below will solve the issue:In case of multiple package you can use :
and them use lapply as below: