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)
}
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)
}
If you prefer to do your iterative tasks using the purrr family of functional programming tools, rather than with for loops, there are two straightforward ways to add progress bars:
Increment the ticks in-line when calling the purrr iterator.
Define the task and increment the ticks in a separate wrapper function.
Option 1 is concise for simple one-line tasks (e.g. requiring only a single function call), while Option 2 is probably preferred for more complex multi-line tasks.
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.
An R package to show ASCII progress bars. Heavily influenced by the https://github.com/tj/node-progress JavaScript project.
Installation
Install the package from CRAN:
If you need the development version, install it from GitHub:
Usage
Use the
progress_barR6 class:The progress bar is displayed after the first
tickcommand. This might not be desirable for long computations, because nothing is shown before the first tick. It is good practice to calltick(0)at the beginning of the computation or download, which shows the progress bar immediately.Custom format, with estimated time of completion:
With elapsed time:
With number of number of ticks/total:
With custom tokens:
It can show download rates for files with unknown sizes:
Progress bars can also digress, by supplying negative values to
tick():See the manual for details and other options.
Usage with
purrriteratorsIf you prefer to do your iterative tasks using the
purrrfamily of functional programming tools, rather than withforloops, there are two straightforward ways to add progress bars:Increment the ticks in-line when calling the
purrriterator.Define the task and increment the ticks in a separate wrapper function.
Option 1 is concise for simple one-line tasks (e.g. requiring only a single function call), while Option 2 is probably preferred for more complex multi-line tasks.
Creating a plyr compatible progress bar
It is easy to create progress bars for plyr:
You can try it with
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: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