Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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%... 
like image 537
DuckMaestro Avatar asked Jan 18 '13 09:01

DuckMaestro


People also ask

How do I print on the same line in R?

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.

How do I output a message in R?

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.

How do you skip a line in R studio?

Use'SHIFT-ENTER' to get to a new line in R without executing the command.

How do I print a string and output in R?

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.


2 Answers

Use cat() instead of print():

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

Outputs:

0%..10% 
like image 91
Andrie Avatar answered Sep 30 '22 23:09

Andrie


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 .. 
like image 42
Roman Luštrik Avatar answered Sep 30 '22 21:09

Roman Luštrik