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
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.
In R, press the “Ctrl” + “L” keys simultaneously. The screen will now be refreshed and the console should be cleared.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With