Follow-up Q Re: [R] displaying computation outputs inside "for" loops

Prof Brian Ripley ripley at
Mon Aug 9 18:34:23 CEST 2004

On Mon, 9 Aug 2004, Dave Atkins wrote:

> I have a somewhat related question.  A while back I was doing some simulations 
> using for() loops, and I wanted to keep track of the iterations using a line of 
> code quite similar to what Dimitris presented below.  Instead of printing the 
> iteration message at the end of each iteration (actually, at the end of every 
> 100th), nothing was printed until the for() loop was complete, and *then* all 
> the iteration messages were reported.  Extending Dimitris' example:
> for(i in 1:10000){
>    x <- rnorm(5)
>    cat("the values of `x' are:", format(x), "\n")
>    cat("computation", i, "finished\n\n")
> }
> demonstrates what I'm referring to (using R v1.9.1 with Windows XP): Iterations 
> are not printed as they occur, only at the end of the for() loop computation.

This *is* in the rw-FAQ.  Please see the posting guide.

> An interesting wrinkle is that I sometimes use Emacs (and ESS).  Running the 
> code from within Emacs prints the iterations as they occur; also, if I switch to 
> other programs and then back to R, iterations have been printed.
Brian D. Ripley,                  ripley at
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list