13

I was just wondering what is the best way in R to keep on printing on the same line in a loop, to avoid swamping your console? Let's say to print a value indicating your progress, as in

for (i in 1:10) {print(i)}

Edit:

I tried inserting carriage returns before each value as in

for (i in 1:10000) {cat("\r",i)}

but that also doesn't quite work as it will just update the value on the screen after the loop, just returning 10000 in this case.... Any thoughts?

NB this is not to make a progress bar, as I know there are various features for that, but just to be able to print some info during the progression of some loop without swamping the console

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103

3 Answers3

14

You have the answer, it's just looping too quickly for you to see. Try:

for (i in 1:10) {Sys.sleep(1); cat("\r",i)}

EDIT: Actually, this is very close to @Simon O'Hanlon's answer, but given the confusion in the comments and the fact that it isn't exactly the same, I'll leave it here.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • Ha many thanks - ha, I wasn't far off then! I guess I was expecting some result like for (i in 1:10000) {Sys.sleep(1/30); cat("\r",i)} but of course that's not ideal because then it will delays things quite a bit, so printing only every so many elements will still be best then I guess... Thx! – Tom Wenseleers Jan 10 '14 at 14:06
  • +1 I *really* should've tried `"\r"`!! I did say some bright spark would figure it out! – Simon O'Hanlon Jan 10 '14 at 15:50
3

Try using cat()...

for (i in 1:10) {cat(paste(i," "))}
#1  2  3  4  5  6  7  8  9  10  

cat() performs much less conversion than print() (from the horses mouth).

To repeatedly print in the same place, you need to clear the console. I am not aware of another way to do this, but thanks to this great answer this works (in RStudio on Windows at least):

for (i in 1:1e3) {
  cat( i )
  Sys.sleep(0.01)
  cat("\014")
}
Community
  • 1
  • 1
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • Ha many thanks - yes that's already better! But no way to go back to the start of the line when printing each value by any chance (ie overprinting the previous value)? And yes, @tonytonov I was aware of those text progress bars, but this was more to be able to print out some kind of information without cluttering the console... Maybe by separating the values with a carriage return or something? – Tom Wenseleers Jan 10 '14 at 11:22
  • Many thanks - it's still not entirely what I need though as I don't want to clear the whole console - I just want to keep on printing in the same place. I tried for (i in 1:10000) {cat("\r",paste(i," "))}; but that also doesn't quite work as it will just update the value after the loop.... – Tom Wenseleers Jan 10 '14 at 12:02
  • @TomWenseleers I understand what you want to do, I'm just not sure there is a way to do it (but don't take my word for it - I'm sure some bright spark will figure it out). Sorry I couldn't be of more help. – Simon O'Hanlon Jan 10 '14 at 12:03
  • Well thx for the pointer though - clearing the console is already a handy thing to be able to do! – Tom Wenseleers Jan 10 '14 at 12:04
0

Well... are you worried about hangs, or just about being notified when the job completes?

In the first case, I'd stick w/ my j%%N suggestion, where N is large enough that you don't swamp the console.

In the second case, add a final line to your script or function which, e.g., calls "Beep" .

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73