forgive me if I missed the right way to do this here, but I can't seem to make progress. Skipping error in for-loop helped a lot, as did some of the other answers related to tryCatch, but I'm still struggling. using tryCatch() in R to assign error values in loop didn't work for me, or I'm missing something.
I am running a for loop with tryCatch, but if I get an error, I would like to record it as a row in the resulting matrix. I can't seem to get the error function output up one level to the loop to be recorded. Here is a simple version of what I'm trying:
collectem <- function(eList){
tmpList <- NULL
for (e in eList){
tryCatch({
tmpVar <- c("foo", e)
if (e==3) stop("BLAH!")
}, error=function(d){c("No",d) -> tmpVar})
tmpList <- rbind(tmpList, tmpVar)
}
return(tmpList)
}
Call:
x <- collectem(1:10)
Which results in:
> x
[,1] [,2]
tmpVar "foo" "1"
tmpVar "foo" "2"
tmpVar "foo" "3"
tmpVar "foo" "4"
tmpVar "foo" "5"
tmpVar "foo" "6"
tmpVar "foo" "7"
tmpVar "foo" "8"
tmpVar "foo" "9"
tmpVar "foo" "10"
But I'm looking for this:
x
[,1] [,2]
tmpVar "foo" "1"
tmpVar "foo" "2"
tmpVar "No" "BLAH!"
tmpVar "foo" "4"
tmpVar "foo" "5"
tmpVar "foo" "6"
tmpVar "foo" "7"
tmpVar "foo" "8"
tmpVar "foo" "9"
tmpVar "foo" "10"
Thanks!!