[R] Progress Monitor in R / looping

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed Oct 25 12:06:06 CEST 2006


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



More information about the R-help mailing list