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

Pierre Kleiber pkleiber at honlab.nmfs.hawaii.edu
Sat Mar 27 03:05:02 CET 2004


OK... how about defining a function exit() as follows

 > exit <- function() {
+  opt <- options(show.error.messages=FALSE)
+  on.exit(options(opt))
+  stop()
+}

then

 > spend.time <- function(i) {
+   print(i)
+   if (i == 5) {
+       exit()
+   }
+   if (i == 75) {
+      stop("Is this an error?")
+   }
+}
 > for(i in 1:100) spend.time(i)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
 >



Andrew Robinson wrote:
> 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
> 
> 

-- 
-----------------------------------------------------------------
Pierre Kleiber, Ph.D       Email: pkleiber at honlab.nmfs.hawaii.edu
Fishery Biologist                     Tel: 808 983-5399/737-7544
NOAA FISHERIES - Honolulu Laboratory         Fax: 808 983-2902
2570 Dole St., Honolulu, HI 96822-2396
-----------------------------------------------------------------
  "God could have told Moses about galaxies and mitochondria and
   all.  But behold... It was good enough for government work."




More information about the R-help mailing list