[R] catching errors in a loop

Henrik Bengtsson hb at maths.lth.se
Sun Jul 31 10:53:48 CEST 2005


I think you forgot to catch "error":s in your code, i.e. tryCatch({...}) 
will be the same as if you did {...}.  You want to use a construct like

for (ii in 1:10) {
   tryCatch({
     # Put the code that may generate an error here.
   }, error = function(ex) {
     # here 'ex' is an instance of 'simpleError'
     print(as.character(ex))
     print(ex$message)
   })
}

See ?tryCatch.  There are some additional examples at
http://www.maths.lth.se/help/R/ExceptionHandlingInR/.

A note: if you're not sure how it works, don't catch warnings.  If you 
catch warnings, the function that generates the warning will be 
interrupted, i.e. the warning will in effect be treated an an error 
(that normally interrupts the code).

/Henrik B

Spencer Graves wrote:
> 	  Unfortunately, I don't know how to get any more information from nls. 
>   My approach to this kind of problem is to write my own function to 
> compute the sum of squares and then use "optim(..., hessian=TRUE)". 
> This is less likely to choke, because "optim" will continue with a 
> singular hessian, while "nls" may not.  As I recall, when "optim" 
> stopped without giving me an answer, the function I asked "optim" to 
> either stopped itself or returned NAs to "optim".  Often a little work 
> with something like Taylor's theorem produced a decent asymptotic 
> approximation for numbers close to the singularities, and I was able to 
> get through those cases.  Sometimes, the hessian would be singular, but 
> "optim" still gave an answer.  In that case, I knew that the optimum was 
> not unique.
> 
> 	  Alternatively, you could continue with 'try' in the loop you 
> described and then explore later the cases where "nls" bombed.
> 
> 	  If you still have time for this and want to try more, I encourage you 
> to try something else like the above and send us another question if you 
> get stuck again.  Also I encourage you to read the posting guide! 
> "http://www.R-project.org/posting-guide.html", especially the part about 
> submitting a simple, self-contained example showing what you tried and 
> why it didn't do what you wanted.  If someone can copy a few lines from 
> your email into R and try something different, it will typically 
> increase the chances that you will get a useful reply.
> 
> 	  spencer graves	
> 
> Anders Bjørgesæter wrote:
> 
> 
>>Hello
>>
>>I can't figure out how to handle errors in R. I have a loop, e.g.
>>
>>for (i in 2:n) {
>>.
>>fit <- nls(model), start=list…
>>if any type of error occur write i to a text file
>>.
>>}
>>
>>I putted “try” around the nls-expression and this let me run through the 
>>loop without R stopping (which I want because each loop takes some time so 
>>I do not want it to stop), but I also want to capture the variable when an 
>>error occur.
>>
>>Appreciate any help
>>
>>/Anders
>>- - - - - - - - - - - - - - - - - - -
>>I tried to use:
>>**“options(error=write(variable.names(matrix[i]), 
>>file="..\\error.txt",append = TRUE))”, hoping this made R write to the text 
>>file every time an error occurred (but this made R write all i’s in the 
>>loop to the text file).
>>**tryCatch(<- nls(model), start=list…), finally =write(…) also writes to a 
>>text file but not necessary when there is an error.
>>**“if (Parameter>x errorM=9 else errorM =0” works but I want to capture any 
>>type of error.
>>- - - - - - - - - - - - - - - - -
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list