Skip to content

Commit

Permalink
Adding handlers(globals = TRUE) for the global progression handler [#95]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Dec 4, 2020
1 parent ae15c3d commit 2f0ec66
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
8 changes: 5 additions & 3 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
Package: progressr
==================

Version: 0.6.0-9000 [2020-11-16]
Version: 0.6.0-9000 [2020-12-04]

SIGNIFICANT CHANGES:

* The user can how use handlers(globals = TRUE) to enable progress reports
everywhere without having to use with_progress(). This works on in
R (>= 4.0.0) because it relies on global calling handlers.

* A progressor must not be created in the global environment unless wrapped
in with_progress() or without_progress() call. Ideally, a progressor is
created within a function or a local() environment.

NEW FEATURES:

* Add register_global_progression_handler().

* progressor() gained argument 'on_exit = TRUE'.

* Add the 'pbcol' handler, which renders the progress as a colored progress
Expand Down
36 changes: 30 additions & 6 deletions R/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
#' @param default The default progression calling handler to use if none
#' are set.
#'
#' @param global If TRUE, then the global progression handler is enabled.
#' If FALSE, it is disabled. If NA, then TRUE is returned if it is enabled,
#' otherwise FALSE. Argument `global` must not used with other arguments.
#'
#' @return (invisibly) the previous list of progression handlers set.
#' If no arguments are specified, then the current set of progression
#' handlers is returned.
#' If `global` is specified, then TRUE is returned if the global progression
#' handlers is enabled, otherwise false.
#'
#' @details
#' This function provides a convenient alternative for getting and setting
Expand All @@ -32,19 +38,37 @@
#' @example incl/handlers.R
#'
#' @export
handlers <- function(..., append = FALSE, on_missing = c("error", "warning", "ignore"), default = handler_txtprogressbar) {
handlers <- function(..., append = FALSE, on_missing = c("error", "warning", "ignore"), default = handler_txtprogressbar, global = NULL) {
stop_if_not(
is.null(global) ||
( is.logical(global) && length(global) == 1L )
)
args <- list(...)
nargs <- length(args)

if (nargs == 0L) {
## Get the current set of progression handlers?
if (is.null(global)) {
if (!is.list(default) && !is.null(default)) default <- list(default)
return(getOption("progressr.handlers", default))
}

## Check, register, or reset global calling handlers?
if (is.na(global)) {
return(register_global_progression_handler(action = "query"))
}
action <- if (isTRUE(global)) "add" else "remove"
return(invisible(register_global_progression_handler(action = action)))
}

## Get the current set of progression handlers?
if (length(args) == 0L) {
if (!is.list(default) && !is.null(default)) default <- list(default)
return(getOption("progressr.handlers", default))
if (!is.null(global)) {
stop("Argument 'global' must not be specified when also registering progress handlers")
}

on_missing <- match.arg(on_missing)

## Was a list specified?
if (length(args) == 1L && is.vector(args[[1]])) {
if (nargs == 1L && is.vector(args[[1]])) {
args <- args[[1]]
}

Expand Down
14 changes: 14 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
.onLoad <- function(libname, pkgname) {
## R CMD check
if (in_r_cmd_check()) {
options(progressr.demo.delay = 0.0)
}

## R CMD build
register_vignette_engine_during_build_only(pkgname)

## Register a global progression handler on load?
global <- Sys.getenv("R_PROGRESSR_GLOBAL_HANDLER", "FALSE")
global <- getOption("progressr.global.handler", as.logical(global))
if (isTRUE(global)) {
utils::str(globalCallingHandlers())
globalCallingHandlers(foo=function(c) utils::str(c))
# register_global_progression_handler()
}
}


11 changes: 10 additions & 1 deletion incl/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@ handlers("txtprogressbar")
if (requireNamespace("beepr", quietly = TRUE))
handlers("beepr", append = TRUE)

with_progress({ y <- slow_sum(10) })
with_progress({ y <- slow_sum(1:5) })
print(y)


if (getRversion() >= "4.0.0") {
handlers(global = TRUE)
y <- slow_sum(1:4)
z <- slow_sum(6:9)
handlers(global = FALSE)
}

20 changes: 18 additions & 2 deletions man/handlers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f0ec66

Please sign in to comment.