with_pb: Allow a function to be called with a progress bar

Description Usage Arguments Value Author(s) Examples

View source: R/utils.R

Description

Allow a function to be called with a progress bar

Usage

1
with_pb(.f, .pb, ...)

Arguments

.f

Function to be called. Pre-processed with rlang::as_function(), so it can be an anonymous "lambda" function.

.pb

Progress bar object. Both progress::progress_bar() and utils::txtProgressBar() are supported.

...

Arguments to function .f

Value

Function .f modified to tick the progress bar

Author(s)

Alexey Shiklomanov

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# The progress package has nice
f <- function(x) Sys.sleep(1)
pb <- progress::progress_bar$new(total = 5)
invisible(lapply(1:5, with_pb(f, pb)))

# Base R's txtProgressBar is also supported, but note that it does
# not close itself
pb <- txtProgressBar(max = 5, style = 3)
invisible(lapply(1:5, with_pb(~Sys.sleep(0.5), pb)))
close(pb)

jinshijian/UrbanKfs documentation built on Jan. 9, 2021, 9:54 a.m.