我应该如何将tryCatch与warnErrList一起使用?
我试图捕获for循环中可能出现的任何错误,这将一次又一次地重复我的函数(函数本身很好,但只想对它进行压力测试,看看是否会出现任何意外情况)。我将在一夜之间运行for循环,并希望醒来看到错误列表 然而,我不确定我应该如何使用tryCatch和warnErrList来产生这样的效果我应该如何将tryCatch与warnErrList一起使用?,r,R,我试图捕获for循环中可能出现的任何错误,这将一次又一次地重复我的函数(函数本身很好,但只想对它进行压力测试,看看是否会出现任何意外情况)。我将在一夜之间运行for循环,并希望醒来看到错误列表 然而,我不确定我应该如何使用tryCatch和warnErrList来产生这样的效果 stresstest <- for (i in 1:50000) tryCatch ( { samplefunction(sampleargument) } )) errorslist <- w
stresstest <- for (i in 1:50000) tryCatch (
{
samplefunction(sampleargument)
}
))
errorslist <- warnErrList(stresstest)
stresstest您可以将error=
参数设置为identity
。此后在res
ult上运行warnErrList
时,错误摘要将显示在警告中。例如:
x
x <- list(7, "a", 2, "c")
res <- sapply(x, function(x)
tryCatch({
log(x)
}, error=identity
))
warnErrList(res)
# [[1]]
# [1] 1.94591
#
# [[2]]
# NULL
#
# [[3]]
# [1] 0.6931472
#
# [[4]]
# NULL
#
# attr(,"warningMsg")
# [1] "2 times caught the same error in log(x): non-numeric argument to mathematical function"
# Warning message:
# 2 times caught the same error in log(x): non-numeric argument to mathematical function