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

Dave Atkins datkins at u.washington.edu
Mon Aug 9 18:03:00 CEST 2004


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.

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.

Can anybody help me out with what's going on (and how to print iterations from 
within a for() loop as they occur)?

Thanks, Dave

Dave Atkins, PhD
University of Washington

Message: 28
Date: Mon, 9 Aug 2004 11:26:08 +0200
From: "Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be>
Subject: Re: [R] displaying computation outputs inside "for" loops
To: "Dewez Thomas" <t.dewez at brgm.fr>
Cc: r-help at stat.math.ethz.ch
Message-ID: <009101c47df2$e7e13430$ad133a86 at www.domain>
Content-Type: text/plain;	charset="iso-8859-1"

Hi Thomas,

is this what you would like to get,

for(i in 1:3){
   x <- rnorm(5)
   cat("the values of `x' are:", format(x), "\n")
   cat("computation", i, "finished\n\n")
}

for(i in 1:3) print(i)


I hope this helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Doctoral Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
      http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message -----
From: "Dewez Thomas" <t.dewez at brgm.fr>
To: "'R mailing list'" <r-help at stat.math.ethz.ch>
Sent: Monday, August 09, 2004 11:15 AM
Subject: [R] displaying computation outputs inside "for" loops



 >> Dear R-users,
 >>
 >> I am puzzled by for loops and am kind of ashamed to ask because it

is so

 >> simple. There must be something I am missing in the way they are

executed.

 >>
 >> Basically, I would like to iterate a given number of time and

generate a

 >> bunch of stats (that's what loops are designed for, right?). Before

doing

 >> this I simply want to test simple procedure and see if they work (ie

got the

 >> syntax right - my main problem as I am new to R - and produce

expected

 >> results).
 >>
 >> Even for something as basic as
 >> for (i in 1:3) {i}
 >>
 >> I get no screen output. Shouldn't R systematically display i for

every loop

 >> just like I am requesting with invoking "i"? When checking at the

end of the

 >> looping, i is indeed assigned to 5 but I cannot get intermediate

values.

 >>
 >> Further testing shows that i takes all the values in turn.
 >
 >>> > for (i in 1:3) {str(i)}
 >
 >>  int 1
 >>  int 2
 >>  int 3
 >>
 >> but summary(i) doesn't display anything. Isn't there something weird

with

 >> this? Am I expecting something wrong and for loops just don't work

that way,

 >> unless using str() command? I tried print() and other descriptive

commands

 >> but to no avail.
 >>
 >> A quick explanation would be grately appreciated
 >>
 >> Thomas
 >> ***
 >> Le contenu de cet e-mail et de ses pièces jointes est

destin...{{dropped}}

 >>
 >> ______________________________________________
 >> R-help at stat.math.ethz.ch mailing list
 >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
 >> PLEASE do read the posting guide!

http://www.R-project.org/posting-guide.html




More information about the R-help mailing list