I am running this piece of code from a Rgui.exe window in Windows:
i = 1
while(i <= 10){
cat(i, "\n")
i = i + 1
Sys.sleep(2)
}
The cat() piece does not get output every 2 seconds, but only collectively at the end of the 20 seconds, i.e. 10 iterations.
I am used to print() and cat() calls getting output immediately, so I don't know why this is happening here.
How do I make R print immediately in each iteration?
It's "output buffering"; try flush.console() after using cat. From the help page:
This does nothing except on console-based versions of R. On the macOS and Windows GUIs, it ensures that the display of output in the console is current, even if output buffering is on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With