[R] Difficulties with for() {while(){}}

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 17 14:24:35 CET 2005


Ronaldo Reis-Jr. wrote:
> Hi,
> 
> I have the follow function:
> 
> function() {
> 
>   ## Init of function
>   ...
> 
>   for(i in test) {
>     ...
> 
>     while(j <= test2) {
>     ...
> 
>     }
>   }
> }
> 
> The problem is that sometimes, naturally, the while is not possible to be 
> resolved, and so the program abort.
> 
> In this case I need that program return to the init of function and run again.

Wrap the call to your function in try(), and if the result is a 
try-error, try it again.  E.g. if your function is named f, do something 
like this:

repeat {
   result <- try(f())
   if (!inherits(result, "try-error")) break
}

Duncan Murdoch

> 
> How I can make this? Abort the while, abort the for and run the function 
> again?
> 
> Thanks
> Ronaldo




More information about the R-help mailing list