[R] stop() vs. error() ?

Liaw, Andy andy_liaw at merck.com
Sat Mar 27 15:31:48 CET 2004

[I don't see any reason to keep this off-list.  Apology to those bored by
this thread.]

> From: Andrew Robinson [mailto:andrewr at uidaho.edu] 
> On Friday 26 March 2004 18:43, you wrote:
> > OK, I guess I'm dense (again?)...

I ain't kidding!

> > Stop where, and do what?  
> Stop where the condition is detected, and report which among 
> the plausible 
> conditions has been detected.
> > I don't know many languages, but I haven't seen
> > (or heard) any that allows stopping in the middle of a 
> function and do
> > nothing.  
> He doesn't want to do nothing.  He just doesn't want to throw 
> an error.

So the intent is to jump all the way back to the R prompt?  The only good
reason for doing so, that my apparently not very good brain can think of, is
when there's an error.  What I learned in a programming class is that a
program should have one and only one exit point.  I.e., if f() calls g(),
it's not desirable to have g() terminating the execution, but rather to have
g() return a value that f() can check for.  Of course, this can get tedious
if you have f() calling g(), g() calling h(), etc., but it should make the
code easier to maintain down the road.

Having this behavior will also lead to confusion.  Consider this scenario:
Suppose I write a function f() that uses the Stop() and place it in a
package, and upload to CRAN.  Some poor soul downloads the package (actually
believing that it'll do something useful), and try something like:

result <- apply(x, 1, f, ...)

and somehow trigger the Stop() inside f().  There's no error raised.  What
should the user expect?  I can just see the poor R core fuming over the bug
reports for apply() not returning something it promised to in ?apply...

> > What's the poor user to do at that point?
> RTFM, just like at every other point.

If you would be so kind as to as advise which FM to R, I'd be more than
happy to do so.  I'm very serious.

> Andrew
> -- 
> Andrew Robinson                      Ph: 208 885 7115
> Department of Forest Resources       Fa: 208 885 6226
> University of Idaho                  E : andrewr at uidaho.edu
> PO Box 441133                        W : 
> http://www.uidaho.edu/~andrewr
> Moscow ID 83843                
>       Or: http://www.biometrics.uidaho.edu
> No statement above necessarily represents my employer's opinion.

More information about the R-help mailing list