How do you print to stderr in R?

狂风中的少年 提交于 2019-11-27 13:25:24

Actually the following works for me:

write("prints to stderr", stderr())

write("prints to stdout", stdout())

Here's a more flexible version for debugging/verbose use in Rscript. Not only it prints to stderr as you ask, but it also allows you to pass variable number of arguments, types etc, like printf does.

v <- function(...) cat(sprintf(...), sep='', file=stderr())

Now one can do things like:

v("name: %s  age: %d\n", name, age)

etc.

Is it possible to configure the print function to print to stderr?

From Ripley himself:

No, but where standard output goes is controlled by sink(), so you can achieve the same effect. R internally has no idea what output comes from print() (which is not just one function but hundreds of methods).

message('for writing diagnostic info to standard error')

message is used for generating ‘simple’ diagnostic messages which are neither warnings nor errors, but nevertheless represented as conditions. Unlike warnings and errors, a final newline is regarded as part of the message, and is optional. The default handler sends the message to the stderr() connection.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!