输出错误/警告日志(txt文件)根据命令行运行v脚本时(Output error/warning l

2019-06-17 13:14发布

如果我下的命令行中运行[R脚本(其实我是运行在VBA调用),我怎么能输出的任何错误/警告消息到一个txt文件?

Answer 1:

您可以使用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"


Answer 2:

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



文章来源: Output error/warning log (txt file) when running R script under command line