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

I am wondering if it is possible to update existing text in the R console? For example, if I run a function that takes a little longer to execute, I would like to know how far it is now.

I could achieve this by releasing print("at 10%"), print("at 20%")etc. in the appropriate places of the function. But this can be a relatively long output, as each time it creates a new line.

Is there a way to update the console text from a running function so that it updates the current line in the console and does not create a new line? For example. >at 10%in the console, if necessary, changes to >at 20%.

+4
source share
3 answers

, . , , \r .

progress rcane:

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')
}

:

Screen shot

, , , :

map_with_progress(some_function, some_data)

(. , Rs txtprogressBar .)

+6

, " R", , . , , . ( , , , , , "" .)

, , : http://www.r-bloggers.com/r-monitoring-the-function-progress-with-a-progress-bar/

, txtProgressBar() ( , , 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)
+2

, CTL-L :

for(i in 1:10) {
  cat("\014")        ## I clear the screen 
  cat(paste0('a',i)) ## progress message in the first coin of the console.
  Sys.sleep(0.5)
}
0

Source: https://habr.com/ru/post/1527743/


All Articles