【发布时间】:2014-09-20 12:30:46
【问题描述】:
我正在尝试捕获我的 R 脚本的完整控制台日志。我想要所有事情的时间顺序,在它们发生时打印警告。我试过这个:
options(warn = 1)
tmpSinkfileName <- tempfile()
sink(tmpSinkfileName, split = TRUE)
cat("Doing something\n")
warning("Hi here")
cat("Doing something else\n")
warning("Hi there")
sink()
console.out <- readChar(tmpSinkfileName, file.info(tmpSinkfileName)$size)
unlink(tmpSinkfileName)
cat(console.out)
# Doing something
# Doing something else
warnings()
# NULL
但不幸的是,console.out 中缺少警告。我怎样才能做到这一点?根据文档,options(warn = 1) 应该在警告发生时打印它们。很遗憾,他们没有被sink()捕获。
【问题讨论】:
-
R CMD BATCH script.R产生一个文件 script.Rout 和完整的会话;也许这与您的用例一致? (R CMD BATCH --vanilla --silent以避免意外加载保存的工作空间并避免启动画面)。
标签: r