I'm attempting to use tryCatch to skip instances of a function generating errors inside of a for loop so that the loop completes and generates output values in those instances where there are no errors. Here's a toy example whose output makes little sense to me:
list=list(10,20,"abc",30,40)
y=c()
for(x in list1){
skip_to_next = FALSE
temp=tryCatch(log(x), error = function(e) {skip_to_next = TRUE})
if(skip_to_next) {next}
y=c(y,temp)
}
If I execute this for loop, my output y is:
y
[1] 2.302585 2.995732 1.000000 3.401197 3.688879
I'm not sure where the 1.0 is coming from, log of a string throws an error and should just skip the third iteration entirely, i.e. return a 4-element vector with elements 1:2 and 4:5 of the above.
Evidently I'm not implementing tryCatch() or next correctly - why isn't "next" taking me to the next iteration as I thought it should?
I was referencing this previous stackoverflow discussion when writing my example: