Terminal Progress Bars

Configurable Progress bars, they may include percentage, elapsed time, and/or the estimated completion time. They work in terminals, in 'Emacs' 'ESS', 'RStudio', 'Windows' 'Rgui' and the 'macOS' 'R.app'. The package also provides a 'C++' 'API', that works with or without 'Rcpp'.




progress


Linux Build Status Windows Build status

An R package to show ASCII progress bars. Heavily influenced by the https://github.com/tj/node-progress JavaScript project.

devtools::install_github("r-lib/progress")

Usage

Use the progress_bar R6 class:

library(progress)
pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
}
[==========================================================-------------]  81%

The progress bar is displayed after the first tick command. This might not be desirable for long computations, because nothing is shown before the first tick. It is good practice to call tick(0) at the beginning of the computation or download, which shows the progress bar immediately.

pb <- progress_bar$new(total = 100)
f <- function() {
  pb$tick(0)
  Sys.sleep(3)
  for (i in 1:100) {
    pb$tick()
    Sys.sleep(1 / 100)
  }
}
f()

Custom format, with estimated time of completion:

pb <- progress_bar$new(
  format = "  downloading [:bar] :percent eta: :eta",
  total = 100, clear = FALSE, width= 60)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
}
  downloading [========----------------------]  28% eta:  1s

With elapsed time:

pb <- progress_bar$new(
  format = "  downloading [:bar] :percent in :elapsed",
  total = 100, clear = FALSE, width= 60)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
}
  downloading [==========================------]  80% in  1s
pb <- progress_bar$new(
  format = "  downloading [:bar] :elapsedfull",
  total = 1000, clear = FALSE, width= 60)
for (i in 1:1000) {
  pb$tick()
  Sys.sleep(1 / 100)
}
  downloading [=====================--------------] 00:00:08

With custom tokens:

pb <- progress_bar$new(
  format = "  downloading :what [:bar] :percent eta: :eta",
  clear = FALSE, total = 200, width = 60)
f <- function() {
  for (i in 1:100) {
    pb$tick(tokens = list(what = "foo   "))
    Sys.sleep(2 / 100)
  }
  for (i in 1:100) {
    pb$tick(tokens = list(what = "foobar"))
    Sys.sleep(2 / 100)
  }
}
f()
  downloading foo    [======------------------]  27% eta:  4s

It can show download rates for files with unknown sizes:

pb <- progress_bar$new(
  format = "  downloading foobar at :rate, got :bytes in :elapsed",
  clear = FALSE, total = 1e7, width = 60)
f <- function() {
  for (i in 1:100) {
    pb$tick(sample(1:100 * 1000, 1))
    Sys.sleep(2/100)
  }
  pb$tick(1e7)
  invisible()
}
f()
  downloading foobar at 5.42 MB/s, got 15.45 MB in  3s

Progress bars can also digress, by supplying negative values to tick():

pb <- progress_bar$new()
f <- function() {
  pb$tick(50)  ; Sys.sleep(1)
  pb$tick(-20) ; Sys.sleep(1)
  pb$tick(50)  ; Sys.sleep(1)
  pb$tick(-30) ; Sys.sleep(1)
  pb$tick(100)
}
f()

See the manual for details and other options.

Creating a plyr compatible progress bar

It is easy to create progress bars for plyr:

progress_progress <- function(...) {
  pb <- NULL
  list(
    init = function(x, ...) {
      pb <<- progress_bar$new(total = x, ...)
    },
    step = function() {
      pb$tick()
    },
    term = function() NULL
  )
}

You can try it with

plyr::l_ply(
  1:100,
  .fun = function(...) Sys.sleep(0.01),
  .progress = 'progress'
)

C++ API

The package also provides a C++ API, that can be used with or without Rcpp. See the example package that is included within progress. Here is a short excerpt that shows how it works:

 
#include <RProgress.h>
 
...
 
RProgress::RProgress pb("Downloading [:bar] ETA: :eta");
 
  pb.tick(0);
  for (int i = 0; i < 100; i++) {
    usleep(2.0 / 100 * 1000000);
    pb.tick();
  }
 
...
 

The C++ API has almost the same functionality as the R API, except that it does not currently support custom tokens, custom streams, and callback functions.

Note that the C++ and the R APIs are independent and for a single progress bar you need to use either one exclusively.

License

MIT @ Gábor Csárdi, RStudio Inc

News

1.2.0

  • Option to change the 'current' character in the progress bar. #47

  • New :tick_rate token, similar to :rate, but units are not assumed to be bytes. #45

  • Total number of ticks can be set to NA now, if unknown. #48

  • All progress bars (including the ones from the C++ API) can be turned off via the progress_enabled option #44

  • The stream argument is deprecated and the progress bar is always printed with message() calls.

  • Total number of iterations can be zero, to support special cases better.

  • Add finished variable, which can be queried to see if a progress bar is finished. #54 @jimhester.

  • Add a terminate() method, which can be called to explicitly remove the progress bar, #53 @jimhester.

  • Outputs greater than the bar width are automatically trimmed, @jimhester.

  • Add a message() method to print a message line(s) above the progress bar, @jimhester.

  • :elapsedfull token: elapsed time in hh:mm:ss format.

  • Fix C++ compiler warnings. #43

  • Fix C++ API on Windows. #56

  • Pass self to the callback function. #55 @jimhester

1.1.2

  • Do not run tests on CRAN, Solaris does not have microbenchmark

1.1.1

  • Move README.md, so that it is not parsed on CRAN
  • Use https URLs instead of http, whenever possible

1.1.0

  • Support for the :spin token which adds a simple ASCII spinner, @richfitz
  • Respect custom token width for calculation of the bar width, @mllg

1.0.2

  • Fix the C++ API on Windows, and on older compilers in general.

1.0.1

First version on CRAN.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.