[R] printing a variable during a loop

David Winsemius dwinsemius at comcast.net
Thu Oct 21 21:03:28 CEST 2010


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.

> 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



More information about the R-help mailing list