[R] Automatic re-looping after error

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Sun Oct 12 14:44:27 CEST 2003


On 12-Oct-03 Andy Liaw wrote:
> It's in R-1.8.0, released October 8th.

On 09-Oct-03 Thomas Lumley wrote:
> On Thu, 9 Oct 2003 Ted.Harding at nessie.mcc.ac.uk wrote:
> 
>> All that's really needed to cope with the situation is for R to
>> drop that cycle of the loop, and resume with a new cycle.
>>
>> However, I'm wondering how to set this up. I've had a look at
>> try(), and I'm not at all sure that it does what I would want.
>> What I'd really like is something (inside the loop) on the lines of
>>
>>    on.error(maybe some parameters X) break
>>
>> where X might specify what sort of error or what function it comes
>> from. Would setting
>>
>>   options(error = break )
>>
> 
> I don't think so.  You may need to look at the new exception-handling
> code (start with help(tryCatch)).

I've now installed R-1.8.0 which does include 'tryCatch' and related
things, but from the look of it I'll have to study it a bit before I see
how it all works!

Meanwhile, thanks to others (Spencer Graves, Achim Zeileis, as well as
Thomas Lumley and Andy Liaw) for suggestions. Though I'd already looked
at "try", I thought I'd have another go. It turns out I'd been muddled
about testing the result of 'try' in the right way.

In fact, if 'myfun(...)' might fail in a loop, then

  result <- try(myfun(...));
  if(class(result)=="try-error") next ;

will have the effect of breaking out of the current cycle of the
loop and starting a new cycle. Otherwise 'result' will be a valid
returned value from 'myfun'. This is exactly what I had wanted.

Thanks to all for the help!
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 12-Oct-03                                       Time: 13:44:27
------------------------------ XFMail ------------------------------




More information about the R-help mailing list