Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you unprint a line in R?

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!

like image 698
kpie Avatar asked Mar 16 '23 02:03

kpie


1 Answers

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")
like image 174
Aaron left Stack Overflow Avatar answered Mar 23 '23 18:03

Aaron left Stack Overflow