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

Andrew Robinson andrewr at uidaho.edu
Sat Mar 27 02:24:47 CET 2004


Andy,

I'm really not sure that he hasn't told us.  All he wants to do is stop the 
process, without throwing an error.  return() won't work if he's in a nested 
function, it will just send him up to the next level.  For example, let's say 
he's in a loop and calling his function in the loop.  If he detects a 
situation he wants to stop the whole program and report it.  I think that 
return will just terminate the function, and the loop will continue.

e.g.


spend.time <- function(i) {
    if (i == 50) {
        return()
    }
    if (i == 75) {
        stop("Is this an error?")
    }
}

   
for(i in 1:100)
    spend.time(i)

i

Andrew

On Friday 26 March 2004 16:41, Liaw, Andy wrote:
> So you still have not told us what exactly what you are looking for.  What
> do you want some sort of stop() to do inside a function, that is neither an
> error nor returning?  Can you show an example in some other language that
> has such a feature?
>
> Andy
>
> > From: ivo welch [mailto:ivo.welch at yale.edu]
> >
> > Hi jim:  ahhh, but then the error messages are off for "real errors"
> > that occur later.
> >
> > Hi pierre: a return() in a (2nd-level) function can be
> > different from a
> > stop().  one could argue about whether it should be possible
> > to stop()
> > in a function of course, without it being an error().
> >
> > regards,
> >
> > /iaw
> >
> > James MacDonald wrote:
> > > And a closer examination of the help page would lead you to this:
> > >
> > > options(show.error.messages=FALSE)
> > > stop()
> > >
> > > which is what I believe you want....
> > >
> > > Jim
> >
> > also From Pierre Kleiber:
> >
> > The thing is that functions don't really stop... they return.
> >  So what
> > you want is return()
> >   Cheers, Pierre
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

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