Re: [R] Automatic re-looping after error
Ted == Ted Harding [EMAIL PROTECTED] on Sun, 12 Oct 2003 13:44:27 +0100 (BST) writes: . Ted I've now installed R-1.8.0 which does include Ted 'tryCatch' and related things, but from the look of it Ted I'll have to study it a bit before I see how it all Ted works! Ted Meanwhile, thanks to others (Spencer Graves, Achim Ted Zeileis, as well as Thomas Lumley and Andy Liaw) for Ted suggestions. Though I'd already looked at try, I Ted thought I'd have another go. It turns out I'd been Ted muddled about testing the result of 'try' in the right Ted way. Ted In fact, if 'myfun(...)' might fail in a loop, then Ted result - try(myfun(...)); Ted if(class(result)==try-error) next ; Ted will have the effect of breaking out of the current Ted cycle of the loop and starting a new cycle. Otherwise Ted 'result' will be a valid returned value from Ted 'myfun'. This is exactly what I had wanted. very good. slightly more recommendable code is result - try(myfun(...)) if(inherits(result, try-error)) next - using inherits(obj, cls) is more robust than class(obj) == cls because it also works when `obj' has more than one class (e.g. a `glm' object) or when `obj' has no class {i.e. when `methods' is not attached or in older R versions}. - no trailing ; Martin Maechler [EMAIL PROTECTED] http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
On 13-Oct-03 Martin Maechler wrote: slightly more recommendable code is result - try(myfun(...)) if(inherits(result, try-error)) next - using inherits(obj, cls) is more robust than class(obj) == cls because it also works when `obj' has more than one class (e.g. a `glm' object) or when `obj' has no class {i.e. when `methods' is not attached or in older R versions}. Thanks! (I had been worried about exactly this kind of issue, though in fact it worked in the case I had.) - no trailing ; ... Old C (and awk) habits die hard ... Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 13-Oct-03 Time: 10:50:16 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 12-Oct-03 Time: 13:44:27 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
On Sat, 11 Oct 2003 [EMAIL PROTECTED] wrote: Thanks, Thomas. I don't seem to have anything related to this in R-1.7.1 (16/06/03). However, some web-searching finally tracked down http://stat.ethz.ch/R-manual/R-devel/library/base/html/conditions.html so what would be involved in making this stuff available? Upgrade to current R? Install a beta-version? Update to the current R. It was introduced in 1.8.0 -thomas __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
On 09-Oct-03 Thomas Lumley wrote: On Thu, 9 Oct 2003 [EMAIL PROTECTED] 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)). Thanks, Thomas. I don't seem to have anything related to this in R-1.7.1 (16/06/03). However, some web-searching finally tracked down http://stat.ethz.ch/R-manual/R-devel/library/base/html/conditions.html so what would be involved in making this stuff available? Upgrade to current R? Install a beta-version? Thanks again, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 11-Oct-03 Time: 10:52:34 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
Have you considered try? hope this helps. spencer graves (Ted Harding) wrote: Hi Folks, I'm seeking advice about how to resume an outer loop following failure of a function within the loop (which issues an error message). Essentially, I'm repeating (simulating) a process which involves random sampling, EM and MCMC. I'm walking on very edge of rather thin ice -- data rather thinly spread over many parameters! Occasionally some component of the loop fails, with an error message. At this point, R quits the run altogether and returns to the command prompt. I then have to restart the whole script. 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 ) do it? Any other suggestions? With thanks, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 09-Oct-03 Time: 18:03:37 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
On Thursday 09 October 2003 19:03, Ted Harding wrote: Hi Folks, I'm seeking advice about how to resume an outer loop following failure of a function within the loop (which issues an error message). Essentially, I'm repeating (simulating) a process which involves random sampling, EM and MCMC. I'm walking on very edge of rather thin ice -- data rather thinly spread over many parameters! Occasionally some component of the loop fails, with an error message. At this point, R quits the run altogether and returns to the command prompt. I then have to restart the whole script. 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 ) do it? Any other suggestions? Look at try(). hth, Z With thanks, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 09-Oct-03 Time: 18:03:37 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
On Thursday 09 October 2003 19:03, Ted Harding wrote: Hi Folks, I'm seeking advice about how to resume an outer loop following failure of a function within the loop (which issues an error message). Essentially, I'm repeating (simulating) a process which involves random sampling, EM and MCMC. I'm walking on very edge of rather thin ice -- data rather thinly spread over many parameters! Occasionally some component of the loop fails, with an error message. At this point, R quits the run altogether and returns to the command prompt. I then have to restart the whole script. 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 ) do it? Argh, didn't read carefully enough, sorry. Usually, I would expect from your description that try() would be good enough. You can try() the critical functions inside the loop and modify the actions inside the loop accordingly if something goes wrong... Z Any other suggestions? With thanks, Ted. E-Mail: (Ted Harding) [EMAIL PROTECTED] Fax-to-email: +44 (0)870 167 1972 Date: 09-Oct-03 Time: 18:03:37 -- XFMail -- __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Automatic re-looping after error
On Thu, 9 Oct 2003 [EMAIL PROTECTED] 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)). -thomas __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help