【问题标题】:Create warning log file when running R script运行 R 脚本时创建警告日志文件
【发布时间】:2016-01-04 18:32:24
【问题描述】:

我想为 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:在 log(i - 50) 中:产生了 NaN
2:在 log(i - 50) 中:产生了 NaN
3:在 log(i - 50) 中:产生了 NaN

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

知道如何解决吗?

我已经根据另一个主题创建了代码:

Output error/warning log (txt file) when running R script under command line

【问题讨论】:

    标签: r warnings sink


    【解决方案1】:

    试试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.
    

    【讨论】:

    • 完美!谢谢你:)
    猜你喜欢
    • 2012-07-24
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多