如何在 R 中打印到 stderr?

发布于 2024-07-27 05:29:09 字数 85 浏览 4 评论 0原文

如何在 R 中打印到 stderr

这对于用 Rscript 编写的脚本特别有用。

How do you print to stderr in R?

This would especially useful for scripts written in Rscript.

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

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

发布评论

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

评论(5

晨曦÷微暖 2024-08-03 05:29:10

实际上以下内容对我有用:

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

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

Actually the following works for me:

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

write("prints to stdout", stdout())
千寻… 2024-08-03 05:29:10

这是一个更灵活的版本,用于在 Rscript 中进行调试/详细使用。 它不仅按照您的要求打印到 stderr,而且还允许您传递可变数量的参数、类型等,就像 printf 那样。

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

现在您可以执行以下操作:

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

等等。

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 you can do things like:

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

etc.

秉烛思 2024-08-03 05:29:10
message('for writing diagnostic info to standard error')

消息用于生成“简单”诊断消息,这些消息既不是警告也不是错误,但仍然表示为条件。 与警告和错误不同,最后的换行符被视为消息的一部分,并且是可选的。 默认处理程序将消息发送到 stderr() 连接。

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.

注定孤独终老 2024-08-03 05:29:10

与已接受的答案的建议相反,使用 write() 函数,这将是对该函数的不当使用,因为它被设计用于将数据写入文件而不是消息。 来自 write() 文档,我们有:

数据(通常是矩阵)x被写入文件file。 如果 x 是二维矩阵,您需要转置它以使文件中的列与内部表示中的列相同。

此外,请注意,write() 为列的数据输出提供了方便的包装器。

write
# function (x, file = "data", ncolumns = if (is.character(x)) 1 else 5, 
#     append = FALSE, sep = " ") 
# cat(x, file = file, sep = c(rep.int(sep, ncolumns - 1), "\n"), 
#     append = append)

也就是说,我建议使用 cat () 以及相应的 file = ... 参数中的条件处理程序 stderr()stdout()

因此,要向标准 error 写入消息,应该使用:

cat("a message that goes to standard error", file = stderr())

或者:

message("also sent to standard error")

对于标准 out,只需直接使用 cat() 即可默认情况下设置写入stdout()

cat("displays in standard out by default")

Contrary to the accepted answer's suggestion to use the write() function, this would be an inappropriate use of the function as it is designed to be used for writing data to a file instead of messages. From the write() documentation, we have:

The data (usually a matrix) x are written to file file. If x is a two-dimensional matrix you need to transpose it to get the columns in file the same as those in the internal representation.

Moreover, note that write() provides a convenience wrapper for data output of columns.

write
# function (x, file = "data", ncolumns = if (is.character(x)) 1 else 5, 
#     append = FALSE, sep = " ") 
# cat(x, file = file, sep = c(rep.int(sep, ncolumns - 1), "\n"), 
#     append = append)

That said, I would recommend using cat() alongside of the appropriate condition handler stderr() or stdout() in file = ... parameter.

Thus, to write a message to standard error, one should use:

cat("a message that goes to standard error", file = stderr())

Or:

message("also sent to standard error")

For standard out, just use cat() directly as it is setup to write to stdout() by default.

cat("displays in standard out by default")
咽泪装欢 2024-08-03 05:29:10

是否可以配置打印
打印到 stderr 的函数?

来自 Ripley 本人

不,但是标准输出的位置是
由sink()控制,所以你可以
达到同样的效果。 内部R
不知道输出来自什么
print() (这不仅仅是一个
功能但数百个方法)。

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).

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