> From: Prof Brian Ripley [mailto:rip...@stats.ox.ac.uk] > Using setTimeLimit(transient = TRUE) is slightly simpler here.
Brian, I thought I should have been able to use transient=TRUE instead of the on.exit(setTimeLimit()) but that gave me an unwanted error report after the top-level expression finished (if there was no timeout). E.g., > to <- function(expr, ...) { + setTimeLimit(..., transient=TRUE) + expr + } > f <- function(n) sum(sapply(seq_len(n), function(i)1/i)) - log(n) > to(f(1e7), elapsed=1) Error in FUN(1:10000000[[371022L]], ...) : reached elapsed time limit > to(f(1e2), elapsed=1) [1] 0.5822073 Error: reached elapsed time limit > > > > sessionInfo() R version 2.14.2 (2012-02-29) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base (On a new 64-bit Windows 7 PC) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: Prof Brian Ripley [mailto:rip...@stats.ox.ac.uk] > Sent: Friday, March 16, 2012 4:28 AM > To: Bert Gunter > Cc: William Dunlap; r-help@r-project.org; Ramiro Barrantes > Subject: Re: [R] Timer on a function > > On Thu, 15 Mar 2012, Bert Gunter wrote: > > > Bill et. al: > > > > 1. This is new to me. Thanks. > > > > 2. As I read the man page, this is not guaranteed to work if the model > > fitting function does not contain sufficient interrupts. Is that > > correct? > > Yes. If someone wrote a model fitting package in Fortran with no > callbacks then it is not interruptible, including not by timers. In > that case all you can do is to kill the R session. > > The author of setTimeLimit() > > > > > -- Bert > > > > On Thu, Mar 15, 2012 at 4:14 PM, William Dunlap <wdun...@tibco.com> wrote: > >> There is a setTimeLimit function in base. It could be encapsulated into > >> the following to limit the time spent on an expression: > >> > >> timeOut <- function (expr, ...) { > >> on.exit(setTimeLimit()) > >> setTimeLimit(...) > >> expr > >> } > > Using setTimeLimit(transient = TRUE) is slightly simpler here. > > >> E.g., with the following slow way to compute Euler's phi > >> f <- function(n) sum(sapply(seq_len(n), function(i)1/i)) - log(n) > >> I get > >> > timeOut(f(1e5), elapsed=1) > >> [1] 0.5772207 > >> > timeOut(f(1e6), elapsed=1) > >> Error in FUN(1:1000000[[711624L]], ...) : reached elapsed time limit > >> Use try() or tryCatch() to check for the error. E.g., > >> > sapply(1:7, function(n)tryCatch(timeOut(f(10^n), elapsed=1), > >> error=function(e)-1)) > >> [1] 0.6263832 0.5822073 0.5777156 0.5772657 0.5772207 -1.0000000 > >> -1.0000000 > >> You could look at 'e' in the error handler to see if it is a time out > >> problem. > >> > >> Bill Dunlap > >> Spotfire, TIBCO Software > >> wdunlap tibco.com > >> > >>> -----Original Message----- > >>> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > >>> On > Behalf > >>> Of Bert Gunter > >>> Sent: Thursday, March 15, 2012 3:05 PM > >>> To: Ramiro Barrantes > >>> Cc: r-help@r-project.org > >>> Subject: Re: [R] Timer on a function > >>> > >>> On Thu, Mar 15, 2012 at 2:24 PM, Ramiro Barrantes > >>> <ram...@precisionbioassay.com> wrote: > >>>> Hello, > >>>> > >>>> I have a program that consists of a loop fitting a function over many > >>> models. Sometimes the fitting on a particular model takes minutes to > >>> converge. Is > there > >>> a way that I can limit the amount of time that R spends on a given model: > >>> > >>> AFAIK, no -- this is an OS level issue. > >>> > >>> Of course, most iterative fitting procedures have controls for the > >>> number of iterations, convergence criteria, etc. , but there is no > >>> awareness of timing except when the OS is interrogated, e.g. by > >>> ?proc.time or ?system.time . Such calls would have to be built into > >>> the fitting function or OS level services would have to be invoked to > >>> run the R process with timing limitations built in. See e.g. ?Rscript > >>> for one possible approach. > >>> > >>> Corrections or clever tricks to get around these perceived limitations > >>> welcomed, of course. > >>> > >>> -- Bert > >>> > >>> Cheers, > >>> Bert > >>>> > >>>> say if my line is: > >>>> > >>>> fittingFunction( func, model.1) > >>>> > >>>> can I have some function: > >>>> > >>>> stopIfUnderTime( fittingFunction( func, model.1) , 5 ) > >>>> > >>>> where stopIfUnderTime will return the result if it finishes under 5 > >>>> seconds, or NA > >>> otherwise. > >>>> > >>>> Is there anything like this? (just looked through the web but did not > >>>> find anything) > >>>> > >>>> Thanks, > >>>> Ramiro > >>>> > >>>> > >>>> > >>>> [[alternative HTML version deleted]] > >>>> > >>>> ______________________________________________ > >>>> R-help@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. > >>> > >>> > >>> > >>> -- > >>> > >>> Bert Gunter > >>> Genentech Nonclinical Biostatistics > >>> > >>> Internal Contact Info: > >>> Phone: 467-7374 > >>> Website: > >>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb- > >>> biostatistics/pdb-ncb-home.htm > >>> > >>> ______________________________________________ > >>> R-help@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. > > > > > > > > -- > > > > Bert Gunter > > Genentech Nonclinical Biostatistics > > > > Internal Contact Info: > > Phone: 467-7374 > > Website: > > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb- > biostatistics/pdb-ncb-home.htm > > > > ______________________________________________ > > R-help@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. > > > > -- > Brian D. Ripley, rip...@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 ______________________________________________ R-help@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.