4

I am writing up some data processing stuff and I wanted to have a concise progress status printing a fraction that updates over time on a single line in the console.

To get this done I wanted to have something like this

print(Initiating data processing...)
for(sample in 1:length(data)){
   print(paste(sample,length(data),sep="/"))
   process(data[[sample]])
   #Unprint the bottom line in the console ... !!! ... !!!.. ?
}

Keeping the screen clean and what not. I don't quite know how to do it. I know that there is a R text progress bar but for utilities sake I'm looking for a little more control.

Thanks!

kpie
  • 9,588
  • 5
  • 28
  • 50

1 Answers1

7

I think your best bet is to do exactly what the R text progress bar does, which is "\r to return to the left margin", as seen in the help file. You'll have to use cat instead of print because print ends with a newline.

cat("Initiating data processing...\n")
for(sample in 1:length(data)){
   cat(sample, length(data), sep="/")
   process(data[[sample]])
   cat("\r")
}
cat("\n")
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142