本文介绍了在命令行下运行R脚本时输出错误/警告日志(txt文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I run R script under command line (actually I run that from calling in VBA), how can I output any error/warning messages to a txt file?

推荐答案

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

You can use sink() to divert messages as well as warnings to a file. The trick is to set the argument type="message":

这是一个从帮助中修改的示例?sink

Here is an example adapted from the help for ?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"

这篇关于在命令行下运行R脚本时输出错误/警告日志(txt文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:56