[R] Time out for a R Function

Henrik Bengtsson hb at biostat.ucsf.edu
Tue Dec 7 08:46:23 CET 2010


See also R help thread 'Wait for keystroke or timeout':

http://www.mail-archive.com/r-help@r-project.org/msg97742.html

/H

On Mon, Dec 6, 2010 at 9:11 PM, Michael Bedward
<michael.bedward at gmail.com> wrote:
> Below is a toy function with one way of doing it. There are bound to
> be better ways :)
>
> function(niter = 10, time.out = 3) {
>  pretend.task <- function() {
>        Sys.sleep(0.5)
>  }
>
>  start <- proc.time()
>
>  for (iter in 1:niter) {
>    pretend.task()
>    cur <- proc.time() - start
>    if (cur[3] > time.out) return("timed out")
>  }
>
>  return("completed")
> }
>
>
> On 7 December 2010 13:04, Santosh Srinivas <santosh.srinivas at gmail.com> wrote:
>> Hello Group,
>>
>> I have an R-function that works fine for most part but sometime runs into a
>> long loop! (I'm lazy and short on time to debug right now so want to do
>> something easy)
>> For my purpose, it is ok to make few errors .... is there a way I can put a
>> timeout on a function and the r-process needes to move on to the next step?
>>
>> Thank you.
>> S
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list