在命令行下运行R脚本时输出错误/警告日志(txt文件)

时间:2012-07-26 09:16:15

标签: r command-line error-handling

如果我在命令行下运行R脚本(实际上我是通过在VBA中调用运行它),如何将任何错误/警告消息输出到txt文件?

2 个答案:

答案 0 :(得分:34)

您可以使用sink()将消息和警告转移到文件中。诀窍是设置参数type="message"

以下是根据?sink的帮助改编的示例:

setwd(tempdir())

## capture messages and errors to a file.
zz <- file("all.Rout", open="wt")
sink(zz, type="message")

try(log("a"))

## reset message sink and close the file connection
sink(type="message")
close(zz)

## Display the log file
readLines("all.Rout")
[1] "Error in log(\"a\") : Non-numeric argument to mathematical function"

答案 1 :(得分:22)

要关闭与日志文件的连接,您必须使用sink(type="message")而不是sink(),然后使用close(zz)

相关问题