如何将 R 警告消息重定向到 STDOUT?

发布于 2024-10-01 02:31:28 字数 218 浏览 3 评论 0原文

我正在使用网格引擎来运行 R 脚本。在此设置下,STDERR 受到认真对待,因此我想保持其干净,并且仅将真正/严重/致命的错误打印到 STDERR。

问题是我的 R 脚本生成各种 STDERR 消息,这些消息并不是真正严重的警告...例如, scan 似乎将其读取的项目数打印到 STDERR。

我可以(从 R 内部)将 STDERR 重定向到 STDOUT 吗?

I'm using a grid engine to run R scripts. The STDERR is taken seriously under this setup, so I would like to keep it clean and have only real/serious/fatal errors printed to STDERR.

The problem is my R script generate various STDERR messages which are not really serious warnings... for example, scan seems to print to STDERR the number of items it read.

Can I redirect (from within R) STDERR to STDOUT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

[旋木] 2024-10-08 02:31:28

查看sink()的帮助页面:

'sink' 将 R 输出转移到连接。如果“文件”是一个字符
字符串,将建立具有该名称的文件连接
转移的持续时间。

正常的 R 输出(到连接“stdout”)被转移
默认'类型=“输出”'。仅提示和(大多数)消息
继续出现在控制台上。消息发送到“stderr()”
(包括来自“消息”、“警告”和“停止”的内容)可以
被“sink(type = "message")”转移(见下文)。

Look at the help page for sink():

‘sink’ diverts R output to a connection. If ‘file’ is a character
string, a file connection with that name will be established for
the duration of the diversion.

Normal R output (to connection ‘stdout’) is diverted by the
default ‘type = "output"’. Only prompts and (most) messages
continue to appear on the console. Messages sent to ‘stderr()’
(including those from ‘message’, ‘warning’ and ‘stop’) can be
diverted by ‘sink(type = "message")’ (see below).

故笙诉离歌 2024-10-08 02:31:28

@Dirk 已经提供了答案,但我想补充一点,您可以使用 stdout() 来连接到 STDOUT。您可以在任何输出函数中使用它来直接输出。

@Dirk already provided the answer, but I would just add that you can use stdout() to get a connection to the STDOUT. You can use this in any output function to direct output there.

素染倾城色 2024-10-08 02:31:28

隔离错误的一种方法是使用 try catch,在主块中,所有内容都发送到 stdout,然后捕获代码引发的任何错误并将其发送到 stderr。

这只会处理代码中引发的错误,而不是记录错误消息,但可能是您的解决方案。

result = tryCatch({
    sink(stdout(), type = "message") # sink all messages, warnings, errors to stdout
    
    message('Starting! This message will go to stdout')
    warning('Warning! This message will go to stdout')
    error('Error! This message will ALSO go to stdout')
    
    #Call your code here, e.g.
    #...
    stop('An error is thrown', call.=FALSE)
    #...

}, error = function(e) {
    sink(NULL, type="message") # close the sink

    #Now sink closed, you can re-raise the error, and it will be directed to sterr:
    stop(paste0('Error: ', e), call.=FALSE)
})

One way to segregate out an error is to use try catch, in the main block everything is send to stdout, then any errors thrown by the code are caught and sent to stderr.

This will only handle errors that are thrown in the code, rather than logging of error messages, but might be a solution for you.

result = tryCatch({
    sink(stdout(), type = "message") # sink all messages, warnings, errors to stdout
    
    message('Starting! This message will go to stdout')
    warning('Warning! This message will go to stdout')
    error('Error! This message will ALSO go to stdout')
    
    #Call your code here, e.g.
    #...
    stop('An error is thrown', call.=FALSE)
    #...

}, error = function(e) {
    sink(NULL, type="message") # close the sink

    #Now sink closed, you can re-raise the error, and it will be directed to sterr:
    stop(paste0('Error: ', e), call.=FALSE)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文