send R diagnostic messages to stdout instead stderr

前端 未结 4 1593
醉梦人生
醉梦人生 2020-12-31 23:13

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

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 23:41

    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()
    }
    

提交回复
热议问题