0

我想为 R 脚本创建警告/错误日志。
请看下面的例子:

setwd(tempdir())
zz <- file("all.Rout", open="wt")
    sink(zz, type="message")
    for (i in 1:30){
           log(i-50)
         }
    sink(type="message")
    close(zz)

我期待它会列出所有警告:
警告消息:
1:在日志中(i - 50):产生了 NaN
2:在日志中(i - 50):产生了 NaN
3:在日志中(i - 50):产生了 NaN

但是对于 1:30 中的循环 i,all.rout 文件中只有一行:
有 30 个警告(使用 warnings() 来查看它们)

知道如何解决吗?

我创建了基于另一个主题的代码:

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

4

1 回答 1

2

尝试options(warn=1)

来自?options

'warn': sets the handling of warning messages.  If 'warn' is
      negative all warnings are ignored.  If 'warn' is zero (the
      default) warnings are stored until the top-level function
      returns.  If 10 or fewer warnings were signalled they will be
      printed otherwise a message saying how many were signalled.
      An object called 'last.warning' is created and can be printed
      through the function 'warnings'.  If 'warn' is one, warnings
      are printed as they occur.  If 'warn' is two or larger all
      warnings are turned into errors.
于 2016-01-04T18:36:22.040 回答