我正在尝试捕获在我的for循环中可能发生的任何错误,这将一遍又一遍地重复我的函数(该函数本身很好,但是只想对其进行压力测试,以查看是否有任何意外的可能弹出)。我将在一夜之间运行for循环,并希望醒来并查看错误列表。
但是,我不确定如何将tryCatch与warnErrList结合使用以产生这样的效果...
stresstest <- for (i in 1:50000) tryCatch (
{
samplefunction(sampleargument)
}
))
errorslist <- warnErrList(stresstest)
答案 0 :(得分:1)
您可以将error=
参数设置为identity
。此后,如果您在warnErrList
上运行res
,则会在警告中显示错误摘要。 示例:
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