Re: [R] catching errors in a loop

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Fri 29 Jul 2005 - 00:16:10 EST

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.

Right idea:

   fit <- try(nls(model, .....))
   if(inherits(fit, "try-error"))

        write(i, file="hello.txt")

Uwe Ligges

>
> 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@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



R-help@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 Received on Fri Jul 29 00:39:02 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:34:07 EST