[R] Show Progress in loop

Philippe Grosjean phgrosjean at sciviews.org
Mon Oct 10 11:17:46 CEST 2005


Hello,

You must explicitly use print(), show() on an object -here, use 
print(i)- in a loop or alternatively, use cat() to display string like:
cat("loop", i, "\n")

With RGui under Windows, there is another subtility: if you have turn on 
'Misc -> Buffered output' (it is ON by default), all output are delayed 
until the end of the command processing. You need to use flush.console() 
to tell to print i immediatelly within a loop. The best synthax is 
(since the command is only usable under Windows):
 > for (i in 1:10) {
 >	print(i)	# You must use print explicitly within a loop
 >	# or, better, use: cat("loop", i, "\n")
 >	# Next command is to overcome buffered output in RGui
 >	if (.Platform$OS.type == "windows") flush.console()
 >	# Next command simulates a "long" process (taking 1 sec)
 >	Sys.sleep(1)
 >	# ... your loop code here...
 > }

Alternatively, you can use the progress() function in svMisc package 
(SciViews bundle). Load svMisc and look at its online help... you have 
several examples of use.
 > library(svMisc)
 > ?progress

Best,

Philippe Grosjean

..............................................<°}))><........
  ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
  ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
  ) ) ) ) )   Mons-Hainaut University, Pentagone (3D08)
( ( ( ( (    Academie Universitaire Wallonie-Bruxelles
  ) ) ) ) )   8, av du Champ de Mars, 7000 Mons, Belgium
( ( ( ( (
  ) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
( ( ( ( (    email: Philippe.Grosjean at umh.ac.be
  ) ) ) ) )
( ( ( ( (    web:   http://www.umh.ac.be/~econum
  ) ) ) ) )          http://www.sciviews.org
( ( ( ( (
..............................................................

Rainer M. Krug wrote:
> Hi
> 
> I have a loop which is doing time consuming calculations and I would 
> like to be able to have some feedback on where it is in it's 
> calculations. I tried to simply show the counter variable in the loop, 
> but id doesn't work as all display seems to be delayed until the loop is 
> completed. Is there any way of displaying the progress of a loop?
> 
> Rainer
> 
> The loop:
> 
> for (i in 2:Result$NoSims)
> {
> 	ppp <- runifpoint(Result$NoPlants)
> 	K <- Kest(ppp)
> 	Result$LSim[i,] <- sqrt(K$iso / pi) - K$r
> 	CM <- (Result$LSim[i,] * Result$LSim[i,]) / abs(K$r[2] - K$r[1])
> 	Result$SigCM[i] <- sum(CM, na.rm=TRUE)
> 	i  #<========================Doesn't display in the loop
> }
>




More information about the R-help mailing list