user1357015
user1357015

Reputation: 11686

R ending a repeat statement after a certain amount of time has elapsed

I have a function that potentially can take quite a long time.

I was wondering if there is a cleaner way to stop the function other than something like this:

repeat{

  time1 <- Sys.time()
  myfunction(x,y,z,...)
  time2 <- Sys.time()
  if(time2 - time1 > my.time.limit) {
   break
  }
}

Upvotes: 2

Views: 336

Answers (1)

GSee
GSee

Reputation: 49820

There is an evalWithTimeout function in the R.utils package. You can use it like this:

require("R.utils")
evalWithTimeout({
  repeat{
    myfunction(x,y,z,...)
  }
}, timeout=my.time.limit, onTimeout="warning")

Run example(evalWithTimeout) to see other ways to use it.

Upvotes: 6

Related Questions