在命令行下运行 R 脚本时输出错误/警告日志(txt 文件) [英] Output error/warning log (txt file) when running R script under command line

查看:9
本文介绍了在命令行下运行 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 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆