Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite current output in the R console

Tags:

date

time

r

console

I have been playing around with the R function txtProgressBar(). How can I hijack the function's ability to overwrite the current output in the console?

i.e. the progress bar updates like this:

> some R function
============

becomes

> some R function
========================

NOT

> some R function
============
========================

For example, how do I write function that will display the current time in the console:

> some R function
13:01

becomes

> some R function
13:02

NOT

> some R function
13:01
13:01
13:01
13:01
13:02
13:02
13:02
13:02
like image 559
Zach Avatar asked May 10 '11 17:05

Zach


People also ask

How do you overwrite stdout?

To overwrite the current standard output line (or parts of it) use \r (or \b .) The special character \r (carriage return) will return the caret to the beginning of the line, allowing you to overwrite it.

How do you refresh the console in R?

In R, press the “Ctrl” + “L” keys simultaneously. The screen will now be refreshed and the console should be cleared.

How do I save terminal output in R?

Saving your workspace is how you save your data within R. Click on the Console window, go to the File menu and select “Save Workspace...”. In another R session, you open this workspace with the “Load Workspace...” command. To save everything that has scrolled past on the Console window, click on the Console window.


1 Answers

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.

like image 115
Greg Snow Avatar answered Sep 20 '22 05:09

Greg Snow