将生成错误的调用包装在tryor中tryCatch。
在伪代码中:
out <- vector(mode = "list", length = 10)
for (i in seq_along(out)) {
res <- try(...function call here...)
## res contains the actual error msg in object of class "try-error"
while(inherits(res, "try-error")) {
res <- try(...function call here...)
}
out[[i]] <- res
}
或使用repeat
out <- vector(mode = "list", length = 10)
for (i in seq_along(out)) {
repreat {
res <- try(...function call here...)
if(!inherits(res, "try-error"))
break
}
out[[i]] <- res
}
您需要安排out为所需的长度或类型的对象来保存结果,您认为合适,以及是否需要记录函数是否失败(如上面的示例使用NA)取决于您的内容是做。如果没有明确的例子,那是你能得到的最好的。
阅读?try并查看?tryCatch,后者是一种更通用的捕获错误的机制。try本身是使用tryCatch例如实现的......