【发布时间】:2011-04-30 12:47:07
【问题描述】:
op <- options(warn=0) #although doesn't work for any value of warn
assign("last.warning", NULL, envir = baseenv())
thisDoesntWork<- function() {
warning("HEY, this is definitely a warning!")
cat(paste("number of warnings:",length(warnings())))
}
>thisDoesntWork()
Warning in thisDoesntWork() : HEY, this is definitely a warning!
number of warnings: 0
警告数应为 1 而不是 0 -
如果在函数中调用,warnings() 似乎什么也不返回。为什么?如何解决此问题以检查函数中是否出现警告并将其打印出来?
我不想使用tryCatch,因为那样我会丢失函数返回的值(它可能仍然返回有效值,即使它产生了警告)。
【问题讨论】:
标签: r error-handling