-----Original Message-----
From: Dimitris Rizopoulos [mailto:d.rizopoulos at erasmusmc.nl]
Sent: Tuesday, October 11, 2011 1:43 PM
To: Doran, Harold
Cc: r-help at r-project.org
Subject: Re: [R] stop()
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:
Suppose I have a function, such as the toy example below:
myFun<- function(x, max.iter = 5) {
for(i in 1:10){
result<- x + i
iter<- i
if(iter == max.iter) stop('Max reached')
}
result
}
I can of course do this:
myFun(10, max.iter = 11)
However, if I reach the maximum number of iterations before my "algorithm"
has finished (in my real application there are EM steps for a mixed model), I
actually want the function to return the value of "result" up to that point.
Currently using stop(), I would get
Error in myFun(10, max.iter = 4) : Max reached
But, in this toy case the function should return the value of "result" up to
Not sure how I can adjust this.
Thanks,
Harold
[[alternative HTML version deleted]]