# Printing a variable in a loop

14 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Printing a variable in a loop

 Dear R Users: I'm a STATA user converting to R, and I'd like to be to do the following. #Assign var_1 and var_2 a value 10->var1 20->var2 #Now I'd like to print the values of var_1 and var_2 by looping through var_1 and var_2 in such a manner: while(y<3){   print(var_y) y+1->y } In STATA, the "y" appended to " var_" is called the local variable and the code would look like this: while y<3 ( display var`y' y=y+1 ) Stata would understand that `y' is first 1 and then 2, and print out the values assigned to var_1 and var_2. Is there a way to do this in R? How would R users fram this questions even? What terminology would you use? Thanks!
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Printing a variable in a loop

 Hi Kat, On Thu, Jun 28, 2012 at 8:22 AM, kat_the_great <[hidden email]> wrote: > Dear R Users: > > I'm a STATA user converting to R, and I'd like to be to do the following. > #Assign var_1 and var_2 a value > 10->var1 > 20->var2 > > #Now I'd like to print the values of var_1 and var_2 by looping through > var_1 and var_2 in such a manner: > > while(y<3){ >  print(var_y) > y+1->y > } The nearest you can get is while (y < 3) {   print(.GlobalEnv[[paste("var", y, sep="")]])   y <- y + 1 } .GlobalEnv (a list, or strictly speaking an environment) contains all variables at the top-level of the REPL But this is not how we do it in R. 1. if you want to "display" the variable, just type it > var1 2. In Stata, you are working with one (tabular) data set at any time. In R, you can work with multiple data sets (R construct: dataframes) at the same time. For example using the builtin anscombe data set Stata: use anscombe di x1 y1 x2 y2   // display all di x1 y1 x2 y2 if _n <= 10 R: # data(anscombe)           # optional anscombe[, c("x1", "y1", "x2", "y2")]   # index by column anscombe[1:10, c("x1", "y1", "x2", "y2")]  # index by row & column head(anscombe[, c("x1","y1","x2","y2")], n=10]   # same as above Regards, Jon ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Printing a variable in a loop

 In reply to this post by kat_the_great      Hello,   Can't you just use vectors? Untested example:   var[1] <- 10   var[2] <- 20   y <- 1   while(y < 3) {     print(var[y])     y <- y+1   }      Take care      Oliver On Wed, Jun 27, 2012 at 8:22 PM, kat_the_great <[hidden email]> wrote: > Dear R Users: > > I'm a STATA user converting to R, and I'd like to be to do the following. > #Assign var_1 and var_2 a value > 10->var1 > 20->var2 > > #Now I'd like to print the values of var_1 and var_2 by looping through > var_1 and var_2 in such a manner: > > while(y<3){ >  print(var_y) > y+1->y > } > > In STATA, the "y" appended to " var_" is called the local variable and the > code would look like this: > > while y<3 ( > display var`y' > y=y+1 > ) > > Stata would understand that `y' is first 1 and then 2, and print out the > values assigned to var_1 and var_2. Is there a way to do this in R? How > would R users fram this questions even? What terminology would you use? > > Thanks! > > > -- > View this message in context: http://r.789695.n4.nabble.com/Printing-a-variable-in-a-loop-tp4634673.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. -- Oliver Ruebenacker, Bioinformatics and Network Analysis Consultant President and Founder of Knowomics (http://www.knowomics.com/wiki/Oliver_Ruebenacker) Consultant at Predictive Medicine (http://predmed.com/people/oliverruebenacker.html) SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org) ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Printing a variable in a loop

 In reply to this post by miguel manese Thanks for your reply Jon. I need to actually do more than print the name of the variable (I just made the example simpler). I need to manipulate var_1, var_2 etc. but setting values of NA to 0.  So as you said, "1. if you want to "display" the variable, just type it >var_1 " But how do I do this in a loop where the  number portion of var is the counter in the loop? Thanks! Kat
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Printing a variable in a loop

 In reply to this post by Oliver Ruebenacker Hi Oliver, Thank you for your reply. I can't use vectors as each var_1, var_2 is a new column/variable, not a new observation within a vector. But thank you, Kat
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Printing a variable in a loop

 Hello, Try the following. dd <- data.frame(A=letters[1:10], var_1=1:10, var_2=11:20) index <- grep("var_", names(dd)) for(i in index)         print(dd[[ i ]])  # or dd[, i] # vars <- paste("var", 1:2, sep="_") for(v in vars)         print(dd[, v])   # or dd[[ v ]] There's nothing you can do in Stata you can't in R. I don't know Stata but I know I'm right. Almost surely. Hope this helps, Rui Barradas Em 28-06-2012 14:42, kat_the_great escreveu: > Hi Oliver, > Thank you for your reply. > > I can't use vectors as each var_1, var_2 is a new column/variable, not a new > observation within a vector. > > But thank you, > Kat > > -- > View this message in context: http://r.789695.n4.nabble.com/Printing-a-variable-in-a-loop-tp4634673p4634756.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Printing a variable in a loop

 In reply to this post by kat_the_great Hi > > Thanks for your reply Jon. > > I need to actually do more than print the name of the variable (I just made > the example simpler). I need to manipulate var_1, var_2 etc. but setting > values of NA to 0. Why? R has pretty strong system for handling NAs. The only exception AFAIK is cumsum x<-1:10 sum(x) [1] 55 x[5]<-NA sum(x) [1] NA sum(x, na.rm=T) [1] 50 cumsum(x)  [1]  1  3  6 10 NA NA NA NA NA NA > >  So as you said, "1. if you want to "display" the variable, just type it > >var_1 > " > > But how do I do this in a loop where the  number portion of var is the > counter in the loop? You probably shall get familiar with list concept which is another strong feature of R. You can easily subset lists either by *apply functions or in for cycle just by indexing. > > Thanks! > Kat > > -- > View this message in context: http://r.789695.n4.nabble.com/Printing-a-> variable-in-a-loop-tp4634673p4634754.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## How to defeat buffering in Rgui? (was: Re: Printing a variable in a loop)

 Hello, All:        Does anyone know how to defeat buffering of output to the console from Rgui?  I routinely print progress reports to the console from within Rterm running under Emacs with ESS (Emacs Speaks Statistics); see the example below.  However, when I run the same example under Rgui, it queues all the output until the computations are complete.        How can I monitor the progress of computations in both Rgui and Rterm?        Consider the following: for(i in 1:1e7){      tst <- sin(i)      if((i%%1e5)==0)cat(i, "")      if((i%%1e6)==0)cat('\n') }        For Rterm 2.15.1 running under Emacs with ESS (Emacs Speaks Statistics), this prints 10000, then pauses before printing 200000, etc., until it gets to 1000000, printing 10 numbers in each row.        However, in Rgui 2.15.1, it queues all the numbers and prints them all together when it completes the computation.  The following is similar: for(i in 1:1e7){      tst <- sin(i)      if((i%%1e5)==0)print(i)      if((i%%1e6)==0)cat('\n') } > sessionInfo() R version 2.15.1 (2012-06-22) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats     graphics  grDevices utils     datasets  methods base        Thanks,        Spencer On 6/28/2012 9:07 AM, Petr PIKAL wrote: > Hi >> Thanks for your reply Jon. >> >> I need to actually do more than print the name of the variable (I just > made >> the example simpler). I need to manipulate var_1, var_2 etc. but setting >> values of NA to 0. > Why? R has pretty strong system for handling NAs. The only exception AFAIK > is cumsum > x<-1:10 > sum(x) > [1] 55 > x[5]<-NA > sum(x) > [1] NA > sum(x, na.rm=T) > [1] 50 > cumsum(x) >   [1]  1  3  6 10 NA NA NA NA NA NA > >>   So as you said, "1. if you want to "display" the variable, just type it >>> var_1 >> " >> >> But how do I do this in a loop where the  number portion of var is the >> counter in the loop? > You probably shall get familiar with list concept which is another strong > feature of R. You can easily subset lists either by *apply functions or in > for cycle just by indexing. > >> Thanks! >> Kat >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/Printing-a->> variable-in-a-loop-tp4634673p4634754.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] 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. > ______________________________________________ > [hidden email] 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. > -- Spencer Graves, PE, PhD President and Chief Technology Officer Structure Inspection and Monitoring, Inc. 751 Emerson Ct. San José, CA 95126 ph:  408-655-4567 web:  www.structuremonitoring.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to defeat buffering in Rgui?

 See the posting guide: this is in the FAQ which you are expected to consult before posting. See http://cran.r-project.org/bin/windows/base/rw-FAQ.html#The-output-to-the-console-seems-to-be-delayedand ?flush.console . On 29/06/2012 08:01, Spencer Graves wrote: > Hello, All: > > >        Does anyone know how to defeat buffering of output to the console > from Rgui?  I routinely print progress reports to the console from > within Rterm running under Emacs with ESS (Emacs Speaks Statistics); see > the example below.  However, when I run the same example under Rgui, it > queues all the output until the computations are complete. > > >        How can I monitor the progress of computations in both Rgui and > Rterm? > > >        Consider the following: > > > for(i in 1:1e7){ >      tst <- sin(i) >      if((i%%1e5)==0)cat(i, "") >      if((i%%1e6)==0)cat('\n') > } > > >        For Rterm 2.15.1 running under Emacs with ESS (Emacs Speaks > Statistics), this prints 10000, then pauses before printing 200000, > etc., until it gets to 1000000, printing 10 numbers in each row. > > >        However, in Rgui 2.15.1, it queues all the numbers and prints > them all together when it completes the computation.  The following is > similar: > > > for(i in 1:1e7){ >      tst <- sin(i) >      if((i%%1e5)==0)print(i) >      if((i%%1e6)==0)cat('\n') > } > > >> sessionInfo() > R version 2.15.1 (2012-06-22) > Platform: i386-pc-mingw32/i386 (32-bit) > > locale: > [1] LC_COLLATE=English_United States.1252 > [2] LC_CTYPE=English_United States.1252 > [3] LC_MONETARY=English_United States.1252 > [4] LC_NUMERIC=C > [5] LC_TIME=English_United States.1252 > > attached base packages: > [1] stats     graphics  grDevices utils     datasets  methods base > > >        Thanks, >        Spencer -- Brian D. Ripley,                  [hidden email] Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/University of Oxford,             Tel:  +44 1865 272861 (self) 1 South Parks Road,                     +44 1865 272866 (PA) Oxford OX1 3TG, UK                Fax:  +44 1865 272595 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to defeat buffering in Rgui?

 Dear Prof. Ripley:        Thanks for the reply.  Unfortunately, flush.console() seems to lock up my system.  I tried the following: for(i in 1:1e7){      tst <- sin(i)      if((i%%1e5)==0)cat(i, "")      if((i%%1e6)==0)cat('\n')      flush.console() }        This slows down Rgui 2.15.1 (32-bit) by a factor of roughly 350:   In Rterm (64-bit), on my dual core 64-bit Windows 7 system, this ran to completion in 1 minute;  this same code without flush.console() ran to completion in 35 seconds in Rgui 2.15.1 (64-bit), then presented the display.  Watching it run in Rgui, it seemed to consume roughly half of one CPU while making very slow progress.  I started timing it when it displayed 200000;  24 minutes later, I noticed it was displaying 900000.  That produces an estimate of 340 minutes to complete.        I'd like to use this to give users (e.g., of a CRAN package) a progress report on long computations:  Otherwise, a user doesn't know if the computation will every complete.        Suggestions?        Thanks,        Spencer On 6/29/2012 12:12 AM, Prof Brian Ripley wrote: > See the posting guide: this is in the FAQ which you are expected to > consult before posting. See > > http://cran.r-project.org/bin/windows/base/rw-FAQ.html#The-output-to-the-console-seems-to-be-delayed  > > > and ?flush.console . > > On 29/06/2012 08:01, Spencer Graves wrote: >> Hello, All: >> >> >>        Does anyone know how to defeat buffering of output to the console >> from Rgui?  I routinely print progress reports to the console from >> within Rterm running under Emacs with ESS (Emacs Speaks Statistics); see >> the example below.  However, when I run the same example under Rgui, it >> queues all the output until the computations are complete. >> >> >>        How can I monitor the progress of computations in both Rgui and >> Rterm? >> >> >>        Consider the following: >> >> >> for(i in 1:1e7){ >>      tst <- sin(i) >>      if((i%%1e5)==0)cat(i, "") >>      if((i%%1e6)==0)cat('\n') >> } >> >> >>        For Rterm 2.15.1 running under Emacs with ESS (Emacs Speaks >> Statistics), this prints 10000, then pauses before printing 200000, >> etc., until it gets to 1000000, printing 10 numbers in each row. >> >> >>        However, in Rgui 2.15.1, it queues all the numbers and prints >> them all together when it completes the computation.  The following is >> similar: >> >> >> for(i in 1:1e7){ >>      tst <- sin(i) >>      if((i%%1e5)==0)print(i) >>      if((i%%1e6)==0)cat('\n') >> } >> >> >>> sessionInfo() >> R version 2.15.1 (2012-06-22) >> Platform: i386-pc-mingw32/i386 (32-bit) >> >> locale: >> [1] LC_COLLATE=English_United States.1252 >> [2] LC_CTYPE=English_United States.1252 >> [3] LC_MONETARY=English_United States.1252 >> [4] LC_NUMERIC=C >> [5] LC_TIME=English_United States.1252 >> >> attached base packages: >> [1] stats     graphics  grDevices utils     datasets  methods base >> >> >>        Thanks, >>        Spencer > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to defeat buffering in Rgui?

 On Fri, Jun 29, 2012 at 10:15 AM, Spencer Graves <[hidden email]> wrote: > Dear Prof. Ripley: > > >      Thanks for the reply.  Unfortunately, flush.console() seems to lock up > my system.  I tried the following: > > > for(i in 1:1e7){ >    tst <- sin(i) >    if((i%%1e5)==0)cat(i, "") >    if((i%%1e6)==0)cat('\n') >    flush.console() > } > > >      This slows down Rgui 2.15.1 (32-bit) by a factor of roughly 350:  In > Rterm (64-bit), on my dual core 64-bit Windows 7 system, this ran to > completion in 1 minute;  this same code without flush.console() ran to > completion in 35 seconds in Rgui 2.15.1 (64-bit), then presented the > display.  Watching it run in Rgui, it seemed to consume roughly half of one > CPU while making very slow progress.  I started timing it when it displayed > 200000;  24 minutes later, I noticed it was displaying 900000.  That > produces an estimate of 340 minutes to complete. > > >      I'd like to use this to give users (e.g., of a CRAN package) a progress > report on long computations:  Otherwise, a user doesn't know if the > computation will every complete. > > >      Suggestions? Only do flush.console() when you actually print something: if((i%%1e5)==0) flush.console() As a system call, flush.console() takes some time, and you do it unnecessarily in each pass through your loop even though you only print something out once every 10^5 passes. Peter ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to defeat buffering in Rgui?

 In reply to this post by Spencer Graves-2 One of the things that you should learn is how to profile (Rprof) your code to see where time is being spent.  If you did, you would have seen that all the time is being spent in 'flush.console'.  As was pointed out in a previous response, only call it when something is printed.   0  31.1 root   1.   31.1 flush.console   1.    0.0 %%   1.    0.0 sin   1.    0.0 cat If you modify your code: for(i in 1:1e4){    tst <- sin(i)    if((i%%1e2)==0)cat(i, "")    if((i%%1e3)==0){cat('\n'); flush.console()}    # flush.console() } You will notice a very large speedup.   0   0.5 root   1.    0.4 flush.console   1.    0.0 cat   1.    0.0 == On Fri, Jun 29, 2012 at 1:15 PM, Spencer Graves <[hidden email]> wrote: > Dear Prof. Ripley: > > >      Thanks for the reply.  Unfortunately, flush.console() seems to lock up > my system.  I tried the following: > > > > for(i in 1:1e7){ >    tst <- sin(i) >    if((i%%1e5)==0)cat(i, "") >    if((i%%1e6)==0)cat('\n') >    flush.console() > } > > >      This slows down Rgui 2.15.1 (32-bit) by a factor of roughly 350:  In > Rterm (64-bit), on my dual core 64-bit Windows 7 system, this ran to > completion in 1 minute;  this same code without flush.console() ran to > completion in 35 seconds in Rgui 2.15.1 (64-bit), then presented the > display.  Watching it run in Rgui, it seemed to consume roughly half of one > CPU while making very slow progress.  I started timing it when it displayed > 200000;  24 minutes later, I noticed it was displaying 900000.  That > produces an estimate of 340 minutes to complete. > > >      I'd like to use this to give users (e.g., of a CRAN package) a progress > report on long computations:  Otherwise, a user doesn't know if the > computation will every complete. > > >      Suggestions? > > >      Thanks, >      Spencer > > > > On 6/29/2012 12:12 AM, Prof Brian Ripley wrote: >> >> See the posting guide: this is in the FAQ which you are expected to >> consult before posting. See >> >> >> http://cran.r-project.org/bin/windows/base/rw-FAQ.html#The-output-to-the-console-seems-to-be-delayed>> >> and ?flush.console . >> >> On 29/06/2012 08:01, Spencer Graves wrote: >>> >>> Hello, All: >>> >>> >>>       Does anyone know how to defeat buffering of output to the console >>> from Rgui?  I routinely print progress reports to the console from >>> within Rterm running under Emacs with ESS (Emacs Speaks Statistics); see >>> the example below.  However, when I run the same example under Rgui, it >>> queues all the output until the computations are complete. >>> >>> >>>       How can I monitor the progress of computations in both Rgui and >>> Rterm? >>> >>> >>>       Consider the following: >>> >>> >>> for(i in 1:1e7){ >>>     tst <- sin(i) >>>     if((i%%1e5)==0)cat(i, "") >>>     if((i%%1e6)==0)cat('\n') >>> } >>> >>> >>>       For Rterm 2.15.1 running under Emacs with ESS (Emacs Speaks >>> Statistics), this prints 10000, then pauses before printing 200000, >>> etc., until it gets to 1000000, printing 10 numbers in each row. >>> >>> >>>       However, in Rgui 2.15.1, it queues all the numbers and prints >>> them all together when it completes the computation.  The following is >>> similar: >>> >>> >>> for(i in 1:1e7){ >>>     tst <- sin(i) >>>     if((i%%1e5)==0)print(i) >>>     if((i%%1e6)==0)cat('\n') >>> } >>> >>> >>>> sessionInfo() >>> >>> R version 2.15.1 (2012-06-22) >>> Platform: i386-pc-mingw32/i386 (32-bit) >>> >>> locale: >>> [1] LC_COLLATE=English_United States.1252 >>> [2] LC_CTYPE=English_United States.1252 >>> [3] LC_MONETARY=English_United States.1252 >>> [4] LC_NUMERIC=C >>> [5] LC_TIME=English_United States.1252 >>> >>> attached base packages: >>> [1] stats     graphics  grDevices utils     datasets  methods base >>> >>> >>>       Thanks, >>>       Spencer >> >> > > ______________________________________________ > [hidden email] 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 Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to defeat buffering in Rgui?

 duh, of course:  Thanks very much. Spencer On 6/29/2012 11:44 AM, jim holtman wrote: > One of the things that you should learn is how to profile (Rprof) your > code to see where time is being spent.  If you did, you would have > seen that all the time is being spent in 'flush.console'.  As was > pointed out in a previous response, only call it when something is > printed. > >    0  31.1 root >    1.   31.1 flush.console >    1.    0.0 %% >    1.    0.0 sin >    1.    0.0 cat > > If you modify your code: > > for(i in 1:1e4){ >     tst <- sin(i) >     if((i%%1e2)==0)cat(i, "") >     if((i%%1e3)==0){cat('\n'); flush.console()} >     # flush.console() > } > > > You will notice a very large speedup. > >    0   0.5 root >    1.    0.4 flush.console >    1.    0.0 cat >    1.    0.0 == > > > > > On Fri, Jun 29, 2012 at 1:15 PM, Spencer Graves > <[hidden email]> wrote: >> Dear Prof. Ripley: >> >> >>       Thanks for the reply.  Unfortunately, flush.console() seems to lock up >> my system.  I tried the following: >> >> >> >> for(i in 1:1e7){ >>     tst <- sin(i) >>     if((i%%1e5)==0)cat(i, "") >>     if((i%%1e6)==0)cat('\n') >>     flush.console() >> } >> >> >>       This slows down Rgui 2.15.1 (32-bit) by a factor of roughly 350:  In >> Rterm (64-bit), on my dual core 64-bit Windows 7 system, this ran to >> completion in 1 minute;  this same code without flush.console() ran to >> completion in 35 seconds in Rgui 2.15.1 (64-bit), then presented the >> display.  Watching it run in Rgui, it seemed to consume roughly half of one >> CPU while making very slow progress.  I started timing it when it displayed >> 200000;  24 minutes later, I noticed it was displaying 900000.  That >> produces an estimate of 340 minutes to complete. >> >> >>       I'd like to use this to give users (e.g., of a CRAN package) a progress >> report on long computations:  Otherwise, a user doesn't know if the >> computation will every complete. >> >> >>       Suggestions? >> >> >>       Thanks, >>       Spencer >> >> >> >> On 6/29/2012 12:12 AM, Prof Brian Ripley wrote: >>> See the posting guide: this is in the FAQ which you are expected to >>> consult before posting. See >>> >>> >>> http://cran.r-project.org/bin/windows/base/rw-FAQ.html#The-output-to-the-console-seems-to-be-delayed>>> >>> and ?flush.console . >>> >>> On 29/06/2012 08:01, Spencer Graves wrote: >>>> Hello, All: >>>> >>>> >>>>        Does anyone know how to defeat buffering of output to the console >>>> from Rgui?  I routinely print progress reports to the console from >>>> within Rterm running under Emacs with ESS (Emacs Speaks Statistics); see >>>> the example below.  However, when I run the same example under Rgui, it >>>> queues all the output until the computations are complete. >>>> >>>> >>>>        How can I monitor the progress of computations in both Rgui and >>>> Rterm? >>>> >>>> >>>>        Consider the following: >>>> >>>> >>>> for(i in 1:1e7){ >>>>      tst <- sin(i) >>>>      if((i%%1e5)==0)cat(i, "") >>>>      if((i%%1e6)==0)cat('\n') >>>> } >>>> >>>> >>>>        For Rterm 2.15.1 running under Emacs with ESS (Emacs Speaks >>>> Statistics), this prints 10000, then pauses before printing 200000, >>>> etc., until it gets to 1000000, printing 10 numbers in each row. >>>> >>>> >>>>        However, in Rgui 2.15.1, it queues all the numbers and prints >>>> them all together when it completes the computation.  The following is >>>> similar: >>>> >>>> >>>> for(i in 1:1e7){ >>>>      tst <- sin(i) >>>>      if((i%%1e5)==0)print(i) >>>>      if((i%%1e6)==0)cat('\n') >>>> } >>>> >>>> >>>>> sessionInfo() >>>> R version 2.15.1 (2012-06-22) >>>> Platform: i386-pc-mingw32/i386 (32-bit) >>>> >>>> locale: >>>> [1] LC_COLLATE=English_United States.1252 >>>> [2] LC_CTYPE=English_United States.1252 >>>> [3] LC_MONETARY=English_United States.1252 >>>> [4] LC_NUMERIC=C >>>> [5] LC_TIME=English_United States.1252 >>>> >>>> attached base packages: >>>> [1] stats     graphics  grDevices utils     datasets  methods base >>>> >>>> >>>>        Thanks, >>>>        Spencer >>> >> ______________________________________________ >> [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.