将R诊断消息发送到stdout而不是stderr [英] send R diagnostic messages to stdout instead stderr

查看:87
本文介绍了将R诊断消息发送到stdout而不是stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一个选项,使我可以将R诊断消息(由message()生成)重定向到stdout,而不是默认情况下的stderr.

Looking for an options which let me to redirect R diagnostic messages (produces by message()) to stdout, not stderr as it is by default.

message手动状态:

默认处理程序将消息发送到stderr()连接.

The default handler sends the message to the stderr() connection.

所以问题是如何更改此默认行为?仍然保留warning()stop()的重定向.

So the question is how can I change this default behavior? still leaving redirection of warning() and stop() intact.

已经尝试过接收器type='message',但它会重定向所有(消息,警告,错误).

Already tried sink type='message' but it redirects all (messages, warnings, errors).

如果有人愿意测试,这是示例脚本exec_test.R:

If anyone is willing to test, this is sample script exec_test.R:

print("using print")
cat("using cat\n")
message("using message")
warning("using warning")
stop("using stop")
q("no")

然后将通过以下方式执行:

which then will be executed by:

Rscript exec_test.R 1>> exec_test.Rout 2>> exec_test_error.Rout

我不需要使用2>&1,因为我的脚本会产生大量的message,很少出现真正的错误,因此我需要将这些日志存储在单独的文件中.

I don't what to use 2>&1 because my script produce tons of messages and very rarely the real errors so I need to store those logs in separate files.

推荐答案

虽然这不是最佳做法,但是您可以使用默认写入stdout()的版本覆盖message,对吗?

While this is very likely not a best practice, you could override message with a version that writes to stdout() by default, right?

message <- function (..., domain = NULL, appendLF = TRUE) 
{
    args <- list(...)
    cond <- if (length(args) == 1L && inherits(args[[1L]], "condition")) {
        if (nargs() > 1L) 
            warning("additional arguments ignored in message()")
        args[[1L]]
    }
    else {
        msg <- .makeMessage(..., domain = domain, appendLF = appendLF)
        call <- sys.call()
        simpleMessage(msg, call)
    }
    defaultHandler <- function(c) {
        cat(conditionMessage(c), file = stdout(), sep = "")
    }
    withRestarts({
        signalCondition(cond)
        defaultHandler(cond)
    }, muffleMessage = function() NULL)
    invisible()
}

这篇关于将R诊断消息发送到stdout而不是stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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