[R] Progress Monitor in R / looping
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Oct 26 03:12:33 CEST 2006
This is easy to do with the proto package. Here is a basic version:
library(proto)
ml <- proto(start = 1, end = 5, val = NULL, it = function(.) {
if (is.null(.$val)) return(.$val <- .$start) else .$val <- .$val + 1
if (.$val > .$end) return(FALSE) else .$val
})
# test 1
while(ml$it()) cat("iteration:", ml$val, "of", ml$end, "\n")
# test 2 - delegate, i.e. inherit, a second iterator
new.ml <- ml$proto(start = 3, end = 6, val = NULL)
while(new.ml$it()) cat("iteration:", new.ml$val, "of", new.ml$end, "\n")
and it does not take much more code than this to create an even
fancier version.
On 10/25/06, Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> wrote:
> Xiaofan Cao wrote:
> > Hi there,
> >
> > I'm writing a program in R that has a few nested loops. I'd like to
> > monitor the progress when the program is running and be able to estimate
> > the remaining time.
>
> A long time ago I started writing some code to give R something like
> an 'iterator' object. You could do this:
>
> > ml=loop(5)
>
> > while(iterate(ml))
> + {cat("doing ",iteration(ml)," of ",N(ml),"\n","Ending at
> ",predictEnd(ml),"\n");sleep(5)}
>
> doing 1 of 5
> Ending at Wed 25 Oct 2006 11:00:05 BST
> doing 2 of 5
> Ending at Wed 25 Oct 2006 11:00:20 BST
> doing 3 of 5
> Ending at Wed 25 Oct 2006 11:00:20 BST
> doing 4 of 5
> Ending at Wed 25 Oct 2006 11:00:20 BST
> doing 5 of 5
> Ending at Wed 25 Oct 2006 11:00:20 BST
>
> you use loop(N) to construct a 1:N loop object, while(iterate(ml)) to
> loop round it, iteration(ml) to get the current iteration number, N(ml)
> to get the iteration limit, and predictEnd(ml) to guess when the whole
> thing will finish.
>
> All the information about the loop is encapsulated in the ml object.
>
> It needs a chunk of polishing up and nobody seemed that interested in
> it last time I mentioned it. My particular application was to MCMC,
> where you could have an MCMC iterator object that was a subclass of my
> simple loop class, and then you could have methods like if(isBurnIn(ml))
> to decide when to start taking samples, or if(!isThinned(ml)) to decide
> whether to store a sample from a thinned chain. Again, all the info
> encapsulated in the loop object.
>
> Another advantage is that unlike for(i in 1:10000000) it doesn't
> create a vector of 10000000 objects...
>
> If anyone thinks this is worth me working on then I'll try and find
> some spare time (hah!) to fix it up. Or if anyone wants to take over, I
> can throw my code at you at see if it sticks.
>
> Barry
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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