Overwrite current output in the R console

Sure you can:

while(1) {
  cat("\b\b\b\b\b\b\b\b",format(Sys.time(), "%H:%M:%S"),sep="")
}

Instead of "\b\b\b\b" you can just use "\r" to go to the beginning of the line and overwrite everything on the line (make sure to still use cat and don't put in a line feed).

Though if you want to display progress it might be better to use winProgressBar (windows only) or tkProgressBar (tcltk package, all platforms) which can be updated with a label in addition to the progress bar.

On windows you can also use the setWindowTitle or setStatusBar functions to put that type of information into the top or bottom of the larger window.


This program seems to work:

while (1) {
cat('\b\b\b\b\b\b',format(Sys.time(),'%H:%M'))
flush.console() 
}

Are there any reasons this might be a bad idea?

/edit: even better (thanks @Greg Snow):

while (1) {
cat('\r',format(Sys.time(),'%H:%M:%S'))
flush.console() 
}

Tags:

Time

Date

Console

R