Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to update existing text in the R console?

Tags:

r

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.

like image 594
Crt Ahlin Avatar asked Feb 19 '14 16:02

Crt Ahlin


People also ask

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 run RGUI?

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.

What does source on Save mean in R?

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.

How do I run a script in RStudio?

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.


2 Answers

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:

Screenshot

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.)

like image 159
Konrad Rudolph Avatar answered Nov 02 '22 00:11

Konrad Rudolph


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)
like image 31
Crt Ahlin Avatar answered Nov 02 '22 00:11

Crt Ahlin