与this问题有关。我想构建一个简单的lapply函数,如果发生错误,它将输出NULL。
我的第一个想法是做这样的事
lapply_with_error <- function(X,FUN,...){
lapply(X,tryCatch({FUN},error=function(e) NULL))
}
tmpfun <- function(x){
if (x==9){
stop("There is something strange in the neiborhood")
} else {
paste0("This is number", x)
}
}
tmp <- lapply_with_error(1:10,tmpfun )但是tryCatch并没有捕捉到它所显示的错误。有什么想法吗?
发布于 2016-10-17 15:22:42
您需要为lapply提供一个函数:
lapply_with_error <- function(X,FUN,...){
lapply(X, function(x, ...) tryCatch(FUN(x, ...),
error=function(e) NULL))
}https://stackoverflow.com/questions/40089885
复制相似问题