Skip to content

Commit 5924d5d

Browse files
progressor() gained argument 'enable'
This is a very efficient (near-zero overhead) way to disable the progress framework (fixes #102)
1 parent 26ee9c0 commit 5924d5d

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

NEWS

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: progressr
22
==================
33

4-
Version: 0.6.0-9000 [2020-12-07]
4+
Version: 0.6.0-9000 [2020-12-10]
55

66
SIGNIFICANT CHANGES:
77

@@ -20,6 +20,16 @@ SIGNIFICANT CHANGES:
2020

2121
NEW FEATURES:
2222

23+
* progressor() gained argument 'enable' to control whether or not the
24+
progressor signals 'progression' conditions. It defaults to option
25+
'progressr.enable' so that progress updates can be disabled globally.
26+
The 'enable' argument makes it easy for package developers who already
27+
provide a 'progress = TRUE/FALSE' argument in their functions to migrate
28+
to the 'progressr' package without having to change their existing API,
29+
e.g. the setup becomes 'p <- progressor(along = x, enabled = progress)'.
30+
The p() function created by p <- progressor(..., enable = FALSE) is an
31+
empty function with near-zero overhead.
32+
2333
* Now with_progress() and without_progress() returns the value of the
2434
evaluated expression.
2535

R/progressor.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#' @param auto_finish (logical) If TRUE, then the progressor will signal a
2323
#' [progression] 'finish' condition as soon as the last step has been reached.
2424
#'
25+
#' @param enable (logical) If TRUE, [progression] conditions are signaled when
26+
#' calling the progressor function created by this function.
27+
#' If FALSE, no [progression] conditions is signaled because the progressor
28+
#' function is an empty function that does nothing.
29+
#'
2530
#' @param on_exit,envir (logical) If TRUE, then the created progressor will
2631
#' signal a [progression] 'finish' condition when the calling frame exits.
2732
#' This is ignored if the calling frame (`envir`) is the global environment.
@@ -31,8 +36,17 @@
3136
#' @export
3237
progressor <- local({
3338
progressor_count <- 0L
34-
35-
function(steps = length(along), along = NULL, offset = 0L, scale = 1L, transform = function(steps) scale * steps + offset, message = character(0L), label = NA_character_, initiate = TRUE, auto_finish = TRUE, on_exit = !identical(envir, globalenv()), envir = parent.frame()) {
39+
40+
void_progressor <- function(...) NULL
41+
environment(void_progressor)$enable <- FALSE
42+
class(void_progressor) <- c("progressor", class(void_progressor))
43+
44+
function(steps = length(along), along = NULL, offset = 0L, scale = 1L, transform = function(steps) scale * steps + offset, message = character(0L), label = NA_character_, initiate = TRUE, auto_finish = TRUE, on_exit = !identical(envir, globalenv()), enable = getOption("progressr.enable", TRUE), envir = parent.frame()) {
45+
stop_if_not(is.logical(enable), length(enable) == 1L, !is.na(enable))
46+
47+
## Quickly return a moot progressor function?
48+
if (!enable) return(void_progressor)
49+
3650
stop_if_not(!is.null(steps) || !is.null(along))
3751
stop_if_not(length(steps) == 1L, is.numeric(steps), !is.na(steps),
3852
steps >= 0)
@@ -123,6 +137,8 @@ print.progressor <- function(x, ...) {
123137
s <- c(s, paste("- progression_index:", e$progression_index))
124138
owner_session_uuid <- e$owner_session_uuid
125139
s <- c(s, paste("- owner_session_uuid:", owner_session_uuid))
140+
141+
s <- c(s, paste("- enable:", e$enable))
126142

127143
s <- paste(s, collapse = "\n")
128144
cat(s, "\n", sep = "")

man/progressor.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)