Здесь шаблон с использованием try
collectem <- function(eList){
#browser()
tmpList <- NULL
for (e in eList){
flag <- try(if (e==3) stop("BLAH!"),silent = TRUE)
if(!is.null(flag) && class(flag)=="try-error"){
#tmpVar <- c("No","BLAH!")
d <- gsub('.*\\: (.*)\n','\\1',flag)
tmpVar <- c("No",d)
} else {tmpVar <- c("foo", e)}
tmpList <- rbind(tmpList, tmpVar)
}
return(tmpList)
}
Когда мы нажмем е = 3 flag
будет
flag
[1] "Error in try(if (e == 3) stop(\"BLAH!\"), silent = TRUE) : BLAH!\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in doTryCatch(return(expr), name, parentenv, handler): BLAH!>
Таким образом, мы можем извлечь что угодно после :
и до \n
как сообщение об ошибке, используя gsub
и группировку. Вот что мы получим
gsub('.*\\: (.*)\n','\\1',flag)
[1] "BLAH!"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in doTryCatch(return(expr), name, parentenv, handler): BLAH!>