[R] printing a variable during a loop

J.delasHeras at ed.ac.uk J.delasHeras at ed.ac.uk
Fri Oct 22 12:59:43 CEST 2010


Thank you for this!

I had also wanted in the past to do this, and ended up writing dummy  
files with informative names to a folder I set to collect these  
messages, so I'd check the folder to see the new files being  
generated... It did the job, and at the same time I could see how long  
it took for my program to reach certain points (filer creation time)  
but not the most elegant! I didn't know about flush.console()

However I used that approach to generate some diagnostic files, so  
that if a complex process broke (sometimes it involved several system  
calls to external programs) I had good information about what the  
program was doing and at what stage it failed. I created a vector to  
store teh names of all teh files being generated and they could be  
removed automatically afterwards.

Not what the OP wanted, but this strategy may be useful for certain tasks.

Jose


Quoting Joshua Wiley <jwiley.psych at gmail.com>:

> On Thu, Oct 21, 2010 at 12:03 PM, David Winsemius
> <dwinsemius at comcast.net> wrote:
>>
>> On Oct 21, 2010, at 8:58 PM, Antonio Olinto wrote:
>>
>>> Thanks Adrienne, but I still in doubt. The behavior of print and message
>>> looks the same.
>>>
>>> Nothing is displayed on the screen after minutes of routine processing .
>>> All values of "i" are displayed only when I press the stop button   
>>> (I'm under
>>> Windows) or when "i" reaches the maximum value.
>>>
>>
>> In the past people have needed to use flush.console() to get output to the
>> screen. Unable to test since A) I'm not running your OS, and B) no
>> reproducible example offered.
>
> I am running your OS (though it would also be nice if you reported the
> results of sessionInfo() ).  In any case, this worked for me on R
> 2.12.0 (i386-pc-mingw32):
>
> for(i in 1:6) {Sys.sleep(3); print(i); flush.console()}
>
> For your problem, I imagine something like (though untested because no data):
>
> for (i in 1:23194) {
> dat.stat[i,c(2:8)]<-quantile(dat.bat[BL==block[i],2],prob=c(0,0.025,0.25,0.5,0.75,0.975,1))
> print(i)
> flush.console()
> }
>
>>
>>> Thanks again,
>>>
>>> Antônio Olitno
>>>
>>>
>>> Citando Adrienne Wootten <amwootte at ncsu.edu>:
>>>
>>>> instead of print use this
>>>>
>>>> message(i)
>>>>
>>>> the message command is used for things like this and it will print the
>>>> value
>>>> of i as you are looping through, but you can also do this:
>>>>
>>>> message("Counter value is: ",i)
>>>>
>>>> which returns for i = 20 for example
>>>>
>>>> "Counter value is 20"
>>>>
>>>> for more check out the message help section in the html
>>>>
>>>> ? message
>>>>
>>>>
>>>> Adrienne Wootten
>>>> NCSU
>>>>
>>>> On Thu, Oct 21, 2010 at 2:05 PM, Antonio Olinto
>>>> <aolinto_r at bignet.com.br>wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> About looping, consider the example:
>>>>>
>>>>> for (i in 1:23194) {
>>>>>
>>>>>
>>>>> dat.stat[i,c(2:8)]<-quantile(dat.bat[BL==block[i],2],prob=c(0,0.025,0.25,0.5,0.75,0.975,1))
>>>>> print(i)
>>>>> }
>>>>>
>>>>> I'd like to have the value of "i" printed for each loop (step). As I
>>>>> could
>>>>> see the values of "i" are shown on screen only after all the work is
>>>>> done.
>>>>>
>>>>> Thanks in advance for any suggestion.
>>>>>
>>>>> Best regards,
>>>>>
>>>>> Antonio
>>>
>> --
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.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.
>



-- 
Dr. Jose I. de las Heras                      Email: J.delasHeras at ed.ac.uk
The Wellcome Trust Centre for Cell Biology    Phone: +44 (0)131 6507095
Institute for Cell & Molecular Biology        Fax:   +44 (0)131 6507360
Swann Building, Mayfield Road
University of Edinburgh
Edinburgh EH9 3JR
UK


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



More information about the R-help mailing list