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%...
You can use the cat() function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat(variable1, variable2, variable3, ...) The following examples show how to use this syntax in different scenarios.
To display ( or print) a text with R, use either the R-command cat() or print(). Note that in each case, the text is considered by R as a script, so it should be in quotes. Note there is subtle difference between the two commands so type on your prompt help(cat) and help(print) to see the difference.
Use'SHIFT-ENTER' to get to a new line in R without executing the command.
R provides a method paste() to print output with string and variable together. This method defined inside the print() function. paste() converts its arguments to character strings. One can also use paste0() method.
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 ..
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