加载包时禁用消息

发布于 2024-12-24 16:14:34 字数 360 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽手叙旧 2024-12-31 16:14:34

只需在 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> 

Just use suppressMessages() around your library() call:

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> 
戴着白色围巾的女孩 2024-12-31 16:14:34

Dirk 的回答会抑制所有消息,并且不特定于加载包时生成的消息。

对所问问题的更准确的解决方案是:

suppressPackageStartupMessages(library("THE_PACKAGE_NAME"))

可以在此处找到更详细的解释

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:

suppressPackageStartupMessages(library("THE_PACKAGE_NAME"))

A bit more detailed explanation can be found here

左岸枫 2024-12-31 16:14:34

使用suppressPackageStartupMessages,请参阅MehradMahmoudian<的答案/em>.为了完整起见,在此处添加使用示例:

对于一个库,使用 suppressPackageStartupMessages(...),例如:

suppressPackageStartupMessages(library(ggplot2))

对于多个库,使用 suppressPackageStartupMessages({...}),例如:

suppressPackageStartupMessages({
    library(ggplot2)
    library(ggdendro)
})

另请参阅:
禁止包启动消息

Use suppressPackageStartupMessages, see the answer by MehradMahmoudian. For completeness, adding here examples of usage:

For one library, use suppressPackageStartupMessages(...), for example:

suppressPackageStartupMessages(library(ggplot2))

For multiple libraries, use suppressPackageStartupMessages({...}), for example:

suppressPackageStartupMessages({
    library(ggplot2)
    library(ggdendro)
})

SEE ALSO:
Suppress package startup messages

孤城病女 2024-12-31 16:14:34

库(ROCR,安静= TRUE)可能是一个更优雅的选择。

library(ROCR, quietly = TRUE) might be a more elegant option.

过期情话 2024-12-31 16:14:34

如果您使用 for 循环加载包,那么您必须像下面所示的那样静默整个循环,而不是在单独加载库时抑制消息。

requiredPackages = c('plyr','dplyr','data.table')
suppressMessages(
 for (p in requiredPackages) {
  if (!require(p, character.only = TRUE)){
   install.packages(p)
  }
  library(p, character.only = TRUE)
 }
)

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.

requiredPackages = c('plyr','dplyr','data.table')
suppressMessages(
 for (p in requiredPackages) {
  if (!require(p, character.only = TRUE)){
   install.packages(p)
  }
  library(p, character.only = TRUE)
 }
)
冷夜 2024-12-31 16:14:34

通过添加 quietly = T 如下所示将解决该问题:

suppressWarnings(suppressMessages(library("dplyr", quietly = T)))

如果有多个包,您可以使用 :

## specify the package names
PKGs <- c("affy","gcrma","readxl","ggplot2","lattice" )

并且它们使用 lapply,如下所示:

lapply(PKGs, library, character.only = TRUE ,quietly = T)

By adding quietly = T as shown below will solve the issue:

suppressWarnings(suppressMessages(library("dplyr", quietly = T)))

In case of multiple package you can use :

## specify the package names
PKGs <- c("affy","gcrma","readxl","ggplot2","lattice" )

and them use lapply as below:

lapply(PKGs, library, character.only = TRUE ,quietly = T)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文