[R] tryCatch - Continuing for/next loop after error

Costas risk2009 at ath.forthnet.gr
Thu Mar 10 09:51:15 CET 2011


Dear all,

I am not sure I understand fully the functionality of "tryCatch" and 
"try" commands and how these are used to continue a for/next loop if an 
error occurs within the loop.

Can somebody point me to material (or share some code) with more 
extensive examples than the ones in the help/FAQ pages?

Do explain my problem in more detail:

for (i in 100:1000)
{
      ## do some other stuff

     dataset<-head(data,i)
     tryCatch(estimatemodel(dataset))

}

My for/next loop reads in data (increasing the dataset by one point at 
every loop run) and then estimates a model. When the problem is 
computationally singular, the loop exits. I want to continue the loop 
and register an "error" estimation value for that step. However when I 
use use the try tryCatch(estimatemodel(data)) (where estimatemodel() is 
a wrapper function calling the model estimation and optimization 
routines), the problem still persists.

Is this the correct way to use tryCatch (or try) or should these go 
inside the actual code bits (i.e., in a more low level fashion) that 
conduct the optimization and model estimation?

Apologies if this is not clear enough.

Best,
Costas



More information about the R-help mailing list