How to output text in the R console without creating new lines?
I would like to output a progress indicator during my lengthy running algorithms. I can easily "bubble up" a progress value from within my algorithm (e.g. via invoking a provided function callback specifically for this purpose), but the difficulty is in the actual text output process. Every call to print
creates a new line, and each prefixed with [1]
.
Is there a way to print at different moments in time, without introducing line breaks?
To be concrete, I want to achieve an "animation" that would look like the following if observed at two different times.
0%...
...
0%...2%...4%...
Use cat()
instead of print()
:
cat("0%")
cat("..10%")
Outputs:
0%..10%
Bah, Andrie beat me to it by 28 seconds.
> for (i in 1:10) {
+ cat(paste("..", i, ".."))
+ }
.. 1 .... 2 .... 3 .... 4 .... 5 .... 6 .... 7 .... 8 .... 9 .... 10 ..