Hello, I'm running a very long for loop that usually takes hours. For my own piece of mind, it would be nice if I could check periodically and see which iteration the loop is on. A line of code that told R to print the iteration number every 100 or 200 iterations would be perfect. Does anyone know something like this? I've never known how to print anything within a for loop before the loop ends. Thanks, Mitch
Just put the code in yourself; use 'cat' with a test to print every 'n' iterations. You can also check out the 'winProgressBar' in the plyr package. On Tue, Jun 22, 2010 at 9:07 AM, Downey, Patrick <PDowney at urban.org> wrote:> Hello, > > I'm running a very long for loop that usually takes hours. For my own piece > of mind, it would be nice if I could check periodically and see which > iteration the loop is on. A line of code that told R to print the iteration > number every 100 or 200 iterations would be perfect. > > Does anyone know something like this? I've never known how to print > anything within a for loop before the loop ends. > > Thanks, > Mitch > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this: for(i in 1:10) { print(sprintf("Interation: %d", i)) flush.console() Sys.sleep(1) } On Tue, Jun 22, 2010 at 10:07 AM, Downey, Patrick <PDowney@urban.org> wrote:> Hello, > > I'm running a very long for loop that usually takes hours. For my own piece > of mind, it would be nice if I could check periodically and see which > iteration the loop is on. A line of code that told R to print the iteration > number every 100 or 200 iterations would be perfect. > > Does anyone know something like this? I've never known how to print > anything within a for loop before the loop ends. > > Thanks, > Mitch > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
You can use cat() to print something during a loop. The help for for() even gives an example. Sarah On Tue, Jun 22, 2010 at 9:07 AM, Downey, Patrick <PDowney at urban.org> wrote:> Hello, > > I'm running a very long for loop that usually takes hours. For my own piece > of mind, it would be nice if I could check periodically and see which > iteration the loop is on. A line of code that told R to print the iteration > number every 100 or 200 iterations would be perfect. > > Does anyone know something like this? I've never known how to print > anything within a for loop before the loop ends. > > Thanks, > Mitch >Sarah Goslee http://www.functionaldiversity.org
for (i in 1:10000) { if( (i - 100) %%100 == 0) print(i); } -- View this message in context: http://r.789695.n4.nabble.com/Displaying-Iteration-Count-tp2264088p2264124.html Sent from the R help mailing list archive at Nabble.com.
Downey, Patrick wrote:> Hello, > > I'm running a very long for loop that usually takes hours. For my own piece > of mind, it would be nice if I could check periodically and see which > iteration the loop is on. A line of code that told R to print the iteration > number every 100 or 200 iterations would be perfect. > > Does anyone know something like this? I've never known how to print > anything within a for loop before the loop ends. > > Thanks, > Mitch >You can also use message() to print a message to the console during execution (I often do this while debugging functions) use paste to put the required text together i.e for(i in 1:10){ #some code message(paste("Iteration ",i," of 10",sep="")) #more code } Regards Paul.