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

标签 r command-line error-handling

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

最佳答案

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

关于r - 在命令行下运行R脚本时输出错误/警告日志(txt文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11666086/

相关文章:

javascript - 如何使项目符号逐渐出现在 RMarkdown 的 flexdashboard Storyboard中?

bash - 从 bash 中的输入文件生成输出文件名

command-line - 在 powershell 中传递命令行 $args,从函数到函数

php - 在类中调用register_shutdown_function

android - 如何从Android App内的logcat捕获错误?

sql - 名称为 “.”的表

R - Shiny 上的实时图表

r - 使用来自库 mouse() 的估算数据集来拟合 R 中的多级模型

linux - 终止 MySQLd session 以返回命令提示符

Java Hangman - 错误检查