Search code examples
rnewlinerstudiooutput-formatting

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

Solution

  • Use cat() instead of print():

    cat("0%")
    cat("..10%")
    

    Outputs:

    0%..10%