我想捕获我的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'以避免意外加载保存的工作空间并避免启动画面)。 – 2014-09-20 12:55:13