Robin Kohrs
Robin Kohrs

Reputation: 697

Progress bar with non-changing line above (in R)

I would like to give a little informative message about where I am in the for-loop. So I have this snippet of code:

for (i in 1:20) {
 dashes = paste0(replicate(20, "-"), collapse = "") 
 cat("This should be above and not change")
 cat(paste0("\r", i, " ", dashes)) 
 Sys.sleep(0.3)
}

However, the output in the console looks like this:

9 --------------------This should be above and not change

While the number updates in place (which is the behavior I wanted), the "This should...", should be placed above and not move at all. I tried a couple of things but did not really succeed in doing so.

Upvotes: 0

Views: 74

Answers (1)

Robin Kohrs
Robin Kohrs

Reputation: 697

Not a very good question...

This simple if does the trick.

for (i in 1:20) {
 dashes = paste0(replicate(20, "-"), collapse = "") 
 if (i == 1) {
   cat("This should be above and not change\n")
 }
 cat(paste0("\r", i, " ", dashes)) 
 Sys.sleep(0.3)
}


Upvotes: 2

Related Questions