Skip to content

Commit 49f76ae

Browse files
Experimental support for progressor(..., finalize = TRUE)
1 parent 35dfd2d commit 49f76ae

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: progressr
2-
Version: 0.17.0-9003
2+
Version: 0.17.0-9004
33
Title: An Inclusive, Unifying API for Progress Updates
44
Description: A minimal, unifying API for scripts and packages to report progress updates from anywhere including when using parallel processing. The package is designed such that the developer can to focus on what progress should be reported on without having to worry about how to present it. The end user has full control of how, where, and when to render these progress updates, e.g. in the terminal using utils::txtProgressBar(), cli::cli_progress_bar(), in a graphical user interface using utils::winProgressBar(), tcltk::tkProgressBar() or shiny::withProgress(), via the speakers using beepr::beep(), or on a file system via the size of a file. Anyone can add additional, customized, progression handlers. The 'progressr' package uses R's condition framework for signaling progress updated. Because of this, progress can be reported from almost anywhere in R, e.g. from classical for and while loops, from map-reduce API:s like the lapply() family of functions, 'purrr', 'plyr', and 'foreach'. It will also work with parallel processing via the 'future' framework, e.g. future.apply::future_lapply(), furrr::future_map(), and 'foreach' with 'doFuture'. The package is compatible with Shiny applications.
55
Authors@R: c(person("Henrik", "Bengtsson",

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
* Add `length()` for `progressor` objects, which enables using
66
`seq_along()` and `seq()` on progressors.
77

8+
## Beta Features
9+
10+
* Add experimental support for `progressor(..., finalize = TRUE)`,
11+
which will cause the progressor to signal a progression 'shutdown'
12+
condition when the progressor is garbage collected. This can serve
13+
as a backstop in yet-to-be-observed cases where a progressor did
14+
not shutdown properly leaving for instance active sinks behind.
15+
For now, `finalize = FALSE` is the default.
16+
817
## Bug Fixes
918

1019
* It was not possible to create more than one progressor in a

R/progressor.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#' @param initiate (logical) If TRUE, the progressor will signal a
2323
#' [progression] 'initiate' condition when created.
2424
#'
25+
#' @param finalize (logical) If TRUE, the progressor signals a [progression]
26+
#' 'shutdown' condition when finalized by the garbage collector.
27+
#'
2528
#' @param auto_finish (logical) If TRUE, then the progressor will signal a
2629
#' [progression] 'finish' condition as soon as the last step has been reached.
2730
#'
@@ -60,7 +63,7 @@ progressor <- local({
6063
environment(void_progressor)$enable <- FALSE
6164
class(void_progressor) <- c("progressor", class(void_progressor))
6265

63-
function(steps = length(along), along = NULL, offset = 0L, scale = 1L, transform = function(steps) scale * steps + offset, message = character(0L), label = NA_character_, trace = FALSE, initiate = TRUE, auto_finish = TRUE, on_exit = !identical(envir, globalenv()), enable = getOption("progressr.enable", TRUE), envir = parent.frame()) {
66+
function(steps = length(along), along = NULL, offset = 0L, scale = 1L, transform = function(steps) scale * steps + offset, message = character(0L), label = NA_character_, trace = FALSE, initiate = TRUE, finalize = FALSE, auto_finish = TRUE, on_exit = !identical(envir, globalenv()), enable = getOption("progressr.enable", TRUE), envir = parent.frame()) {
6467
stop_if_not(is.logical(enable), length(enable) == 1L, !is.na(enable))
6568

6669
## Quickly return a moot progressor function?
@@ -113,7 +116,7 @@ progressor <- local({
113116
muffleProgression = function(p) NULL
114117
)
115118
invisible(cond)
116-
}
119+
} ## fcn()
117120
formals(fcn)$message <- message
118121
class(fcn) <- c("progressor", class(fcn))
119122

@@ -163,6 +166,18 @@ progressor <- local({
163166
do.call(base::on.exit, args = list(call, add = TRUE), envir = envir)
164167
}
165168

169+
if (finalize) {
170+
## Self-reference progressor function needed by the finalizer function
171+
progressor_envir$self <- fcn
172+
reg.finalizer(progressor_envir, function(e) {
173+
print(utils::ls.str(e))
174+
if (is.function(e$self)) {
175+
e$self(type = "shutdown")
176+
e$self <- NULL
177+
}
178+
})
179+
}
180+
166181
fcn
167182
}
168183
})

man/progressor.Rd

Lines changed: 4 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)