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

Spencer Graves spencer.graves at structuremonitoring.com
Fri Jun 29 09:01:15 CEST 2012


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.
>>
>> ______________________________________________
>> 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.
> ______________________________________________
> 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.
>

-- 
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



More information about the R-help mailing list