Skip to content

stop()

6 messages · Dimitris Rizopoulos, Doran, Harold, Nordlund, Dan (DSHS/RDA) +1 more

#
You could use return(), e.g.,

myFun <- function (x, max.iter = 5) {
     for (i in 1:10) {
         result <- x + i
         iter <- i
         if (iter == max.iter) {
             return(result)
         }
     }
     result
}

myFun(10, max.iter = 4)


I hope it helps.

Best,
Dimitris
On 10/11/2011 7:31 PM, Doran, Harold wrote:

  
    
#
Thanks, Dimitris. Very helpful on something I *should* know by now.
#
Or, just use break :

myFun <- function (x, max.iter = 5) {
     for (i in 1:10) {
         result <- x + i
         iter <- i
         if (iter == max.iter) break
     }
     result
}


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
Replace "stop()" with "break" to see if that does what you want.  (you may also want to include "cat()" or "warn()" to indicate the early stopping.
#
Thank you, Greg. This indeed works well for this purpose.