utils_progress: Utilities for text progress bar in the terminal

utils_progressR Documentation

Utilities for text progress bar in the terminal

Description

[Experimental]

Progress bars are configurable, may include percentage, elapsed time, and custom text.

  • progress(): Initiate a custom progress bar of class pb_metan.

  • run_progress(): Run the progress bar and should be called within a 'for loop' statement, a lapply() family or purrr::map() family of functional programming tools.

Usage

progress(
  min = 0,
  max = 100,
  leftd = "|",
  rightd = "|",
  char = "=",
  style = 2,
  width = getOption("width"),
  time = Sys.time()
)

run_progress(pb, actual, text = "", digits = 0, sleep = 0)

Arguments

min, max

Numeric values for the extremes of the progress bar. Must have min < max.

leftd, rightd

The left and right delimiters for the progress bar. Defaults to "|".

char

The character (or character string) to form the progress bar.

style

The 'style' of the progress bar. Elapsed time is counted from calling progress() up to each call of run_progress().

  • type = 1: Shows a progress bar without percentage or elapsed time.

  • type = 2: The default, shows the progress bar and its percentage.

  • type = 3: Shows the progress bar and elapsed time.

  • type = 4: Shows the progress bar, percentage, and elapsed time.

width

The the width of the progress bar. Defaults to the number of characters is that which fits into getOption("width").

time

The system time used to compute the elapsed time from calling progress() to each call of run_progress(). Defaults to Sys.time().

pb

An object created with progress()

actual

The actual value, for example, a loop variable that define the loop index value.

text

An optional character string to be shown at the begining of the progress bar.

digits

The number of significant figures in percentage value. Defaults to 0.

sleep

Suspend execution for a time interval with Sys.sleep() within run_progress(). Defaults to 0.

Value

progress() returns a list of class pb_metan that contains the set parameters that will called by run_progress().

Author(s)

Tiago Olivoto tiagoolivoto@gmail.com

Examples



################### A for looping approach ################
pb <-  progress()
for (i in 1:100) {
 run_progress(pb, actual = i, sleep = 0.01)
}

################### An apply family approach ##############
pb <- progress(max = 10)
foo <- function(...){
   run_progress(pb, ...)
   rnorm(100) %>%  mean()
 }
(a <- sapply(1:10, FUN = foo, sleep = 0.05))

######## A purrr functional programming approach ##########
foo2 <- function(...){
     run_progress(pb2, ...)
     rnorm(100) %>%  mean()
}
pb2 <- progress(max = 10000,
                style = 4,
                leftd = "",
                char = ".",
                rightd = "!")

b <- purrr::map_dbl(1:10000, foo2, text = "Progress bar for sampling")
hist(b)


metan documentation built on March 7, 2023, 5:34 p.m.