I am wondering if it is possible to update existing text in the R console? E.g., if I run a function that takes a bit longer to execute, I would like to know how far along it is currently.
I could achieve this by issuing print("at 10%")
, print("at 20%")
, etc. at appropriate locations in the function. But this could be a relatively long output, since it produces a new line each time.
Is there a way to update the console text from the running function in a way that it updates the current line in the console and not create a new line? E.g. >at 10%
in the console changes to >at 20%
when appropriate.
In R, press the “Ctrl” + “L” keys simultaneously. The screen will now be refreshed and the console should be cleared.
Start R by double-clicking on the R icon on the desktop, or by clicking on the R icon in the start menu. The R graphical user interface (GUI) will open, containing a single window called the command or console window. The greater-than sign ( > ) is R's "prompt;" it indicates that R is ready for you to enter commands.
When editing re-usable functions (as opposed to freestanding lines of R) you may wish to set the Source on Save option for the document (available on the toolbar next to the Save icon). Enabling this option will cause the file to automatically be sourced into the global environment every time it is saved.
You create new R Script by clicking on File > New File > R Script in the RStudio menu bar. To execute your code in the R script, you can either highlight the code and click on Run, or you can highlight the code and press CTRL + Enter on your keyboard.
Flushing the console works but may cause flickering. If you want a very simple text line update, then it’s sufficient to go back to the beginning of the line by printing \r
and to overwrite the text there.
Here’s a progress
function from the rcane
module:
progress <- function (x, max = 100) {
percent <- x / max * 100
cat(sprintf('\r[%-50s] %d%%',
paste(rep('=', percent / 2), collapse = ''),
floor(percent)))
if (x == max)
cat('\n')
}
Here’s a screenshot of the progress bar in action:
And finally, this can be wrapped nicely so that its usage is effortless:
map_with_progress(some_function, some_data)
(See the link above for an implementation; of course R’s own txtprogressBar
is quite a bit more flexible.)
Although a general answer to the question of "is it possible to update text in R console" was not given, it is not necessary for my particular problem. The progress bars mentioned by Ananda and Dason in comments to my questions are a solution to the problem of tracking the status of an executing function. (Updating text would actually probably not make sense in this case, if at all possible, since sometimes there is other output from the functions, which could "hide" the updated text.)
Examples of how to implemented different progress bars are well described at: http://www.r-bloggers.com/r-monitoring-the-function-progress-with-a-progress-bar/
For completeness, a slightly updated example from above mentioned link of the txtProgressBar()
(which works at least with Windows+RStudio):
total <- 20
# create progress bar
pb <- txtProgressBar(min = 0, max = total, style = 3)
for(i in 1:total){
Sys.sleep(0.1)
print("Result XZY")
# update progress bar
setTxtProgressBar(pb, i)
}
close(pb)
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