x <- tryCatch({
warning("oops")
}, warning=function(w) {
## do something about the warning, maybe return 'NA'
message("handling warning: ", conditionMessage(w))
NA
})
结果就是
handling warning: oops
> x
[1] NA
tryCatch 之后继续执行; 您可以通过将警告转换为错误来决定结束
x <- tryCatch({
warning("oops")
}, warning=function(w) {
stop("converted from warning: ", conditionMessage(w))
})