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

Patrick Connolly p.connolly at hortresearch.co.nz
Mon Mar 29 01:31:31 CEST 2004


On Sat, 27-Mar-2004 at 10:13AM -0500, ivo welch wrote:

|> 
|> hi andy:  mea culpa.  it is the exit function in most other languages.  
|> It would be exit(0) invocation in C (C++) and perl, for example.

I've not had enough experience in perl to have used exit, but this is
my guess at what is wanted:


spend.time <- function(i) {
  i <- 1
  while(i < 10){
    print(i)
    if (i == 5)
      break
    if (i == 7)
      stop("Is this an error?")
    i <- i + 1
  }
}

> spend.time(7)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> 

We don't get as far as 7, so the error message doesn't appear.



However, if the break is put after 7, an error has already occurred, as in:

spend.time2 <- function(i) {
  i <- 1
  while(i < 10){
    print(i)
    if (i == 9)
      break
    if (i == 7)
      stop("Is this an error?")
    i <- i + 1
  }
}

> spend.time2(7)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
Error in spend.time2(7) : Is this an error?
> 


HTH


-- 
Patrick Connolly
HortResearch
Mt Albert
Auckland
New Zealand 
Ph: +64-9 815 4200 x 7188
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it.  ---Steven Wright 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~




More information about the R-help mailing list