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 } -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Department of Conservation Ecology University of Stellenbosch Matieland 7602 South Africa
"Rainer M. Krug" <RKrug at sun.ac.za> writes:> 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 > }Just print(i) and if on Windows, remember to unset output buffering. -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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 > } >- Update your console (I guess you are on Windows?) by using flush.console(). - You might want to measure time consumption, hence see ?Rprof. - Save some more time by moving as much as possible out of your loop by doing it in a vectorized way (I don't know all the functions you are using, hence cannot make any further recommendations). Uwe Ligges
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 > } >
Rainer M. Krug a ??crit :> 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? > > for (i in 2:Result$NoSims) > { > ... > i #<========================Doesn't display in the loop > } >Hi, for your last line, use : print(i); flush.console(); #<====== now displays in the loop hih
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 >} > > >Hi, You can simply include a command like cat("loop: " , i, "\n") inside your loop. EJ