Skip to content

Commit dd86848

Browse files
handlers() gained argument 'default'
1 parent 3147b8b commit dd86848

22 files changed

+101
-38
lines changed

NEWS

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
Package: progressr
22
==================
33

4-
Version: 0.1.5-9000 [2019-10-26]
4+
Version: 0.1.5-9000 [2019-11-02]
55

6-
* ...
6+
NEW FEATURES:
7+
8+
* handlers() gained argument 'default' specifying a progression handler to
9+
be returned if none is set.
710

811

912
Version: 0.1.5 [2019-10-26]

R/handlers.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#' is produces and the missing handlers is ignored. If `"ignore"`, the
1111
#' missing handlers is ignored.
1212
#'
13+
#' @param default The default progression calling handler to use if none
14+
#' are set.
15+
#'
1316
#' @return (invisibly) the previous list of progression handlers set.
1417
#' If no arguments are specified, then the current set of progression
1518
#' handlers is returned.
@@ -21,11 +24,11 @@
2124
#' @example incl/handlers.R
2225
#'
2326
#' @export
24-
handlers <- function(..., on_missing = c("error", "warning", "ignore")) {
27+
handlers <- function(..., on_missing = c("error", "warning", "ignore"), default = txtprogressbar_handler) {
2528
args <- list(...)
2629

2730
## Get the current set of progression handlers?
28-
if (length(args) == 0L) return(getOption("progressr.handlers"))
31+
if (length(args) == 0L) return(getOption("progressr.handlers", default))
2932

3033
on_missing <- match.arg(on_missing)
3134

R/progression_handler.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@
2929
#' @param clear (logical) If TRUE, any output, typically visual, produced
3030
#' by a reporter will be cleared/removed upon completion, if possible.
3131
#'
32+
#' @param target (character vector) Specifies where progression updates are
33+
#' rendered.
34+
#'
3235
#' @return A function of class `progression_handler` that takes a
3336
#' [progression] condition as its first and only argument.
3437
#'
3538
#' @seealso
3639
#' [base::withCallingHandlers()].
3740
#'
3841
#' @export
39-
make_progression_handler <- function(name, reporter = list(), handler = NULL, enable = getOption("progressr.enable", interactive()), enable_after = getOption("progressr.enable_after", 0.0), times = getOption("progressr.times", +Inf), interval = getOption("progressr.interval", 0.0), intrusiveness = 1.0, clear = getOption("progressr.clear", TRUE)) {
42+
make_progression_handler <- function(name, reporter = list(), handler = NULL, enable = getOption("progressr.enable", interactive()), enable_after = getOption("progressr.enable_after", 0.0), times = getOption("progressr.times", +Inf), interval = getOption("progressr.interval", 0.0), intrusiveness = 1.0, clear = getOption("progressr.clear", TRUE), target = "terminal") {
4043
if (!enable) times <- 0
4144
name <- as.character(name)
4245
stop_if_not(length(name) == 1L, !is.na(name), nzchar(name))
@@ -50,7 +53,8 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
5053
times >= 0)
5154
stop_if_not(length(interval) == 1L, is.numeric(interval),
5255
!is.na(interval), interval >= 0)
53-
56+
stop_if_not(is.character(target))
57+
5458
## Disable progress updates?
5559
if (times == 0 || is.infinite(interval) || is.infinite(intrusiveness)) {
5660
handler <- function(p) NULL
@@ -101,7 +105,8 @@ make_progression_handler <- function(name, reporter = list(), handler = NULL, en
101105
interval = interval,
102106
enable_after = enable_after,
103107
auto_finish = auto_finish,
104-
clear = clear
108+
clear = clear,
109+
target = target
105110
)
106111

107112
state <- list(
@@ -348,6 +353,7 @@ print.progression_handler <- function(x, ...) {
348353
s <- c(s, sprintf(" - intrusiveness: %g", env$intrusiveness))
349354
s <- c(s, sprintf(" - auto_finish: %s", env$auto_finish))
350355
s <- c(s, sprintf(" - clear: %s", env$clear))
356+
s <- c(s, sprintf(" - target: %s", paste(sQuote(env$target), collapse = ", ")))
351357
s <- c(s, sprintf(" - milestones: %s", hpaste(env$milestones %||% "<NULL>")))
352358
s <- c(s, sprintf(" - owner: %s", hpaste(env$owner %||% "<NULL>")))
353359

R/progression_handlers.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' @example incl/ascii_alert_handler.R
1515
#'
1616
#' @export
17-
ascii_alert_handler <- function(symbol = "\a", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.auditory", 5.0), ...) {
17+
ascii_alert_handler <- function(symbol = "\a", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.auditory", 5.0), target = c("terminal", "audio"), ...) {
1818
reporter <- local({
1919
list(
2020
update = function(config, state, progression, ...) {
@@ -44,7 +44,7 @@ ascii_alert_handler <- function(symbol = "\a", file = stderr(), intrusiveness =
4444
#'
4545
#' @importFrom utils file_test flush.console txtProgressBar setTxtProgressBar
4646
#' @export
47-
txtprogressbar_handler <- function(style = 3L, file = stderr(), intrusiveness = getOption("progressr.intrusiveness.terminal", 1), ...) {
47+
txtprogressbar_handler <- function(style = 3L, file = stderr(), intrusiveness = getOption("progressr.intrusiveness.terminal", 1), target = "terminal", ...) {
4848
reporter <- local({
4949
## Import functions
5050
eraseTxtProgressBar <- function(pb) {
@@ -123,7 +123,7 @@ txtprogressbar_handler <- function(style = 3L, file = stderr(), intrusiveness =
123123
#' @example incl/tkprogressbar_handler.R
124124
#'
125125
#' @export
126-
tkprogressbar_handler <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), ...) {
126+
tkprogressbar_handler <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), target = "terminal", ...) {
127127
## Used for package testing purposes only when we want to perform
128128
## everything except the last part where the backend is called
129129
if (!is_fake("tkprogressbar_handler")) {
@@ -190,7 +190,7 @@ tkprogressbar_handler <- function(intrusiveness = getOption("progressr.intrusive
190190
#' @param \ldots Additional arguments passed to [make_progression_handler()].
191191
#'
192192
#' @export
193-
winprogressbar_handler <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), ...) {
193+
winprogressbar_handler <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), target = "gui", ...) {
194194
## Used for package testing purposes only when we want to perform
195195
## everything except the last part where the backend is called
196196
if (!is_fake("winprogressbar_handler")) {
@@ -268,7 +268,7 @@ winprogressbar_handler <- function(intrusiveness = getOption("progressr.intrusiv
268268
#'
269269
#' @importFrom utils file_test flush.console txtProgressBar setTxtProgressBar
270270
#' @export
271-
pbmcapply_handler <- function(substyle = 3L, style = "ETA", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.terminal", 1), ...) {
271+
pbmcapply_handler <- function(substyle = 3L, style = "ETA", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.terminal", 1), target = "terminal", ...) {
272272
if (!is_fake("pbmcapply_handler")) {
273273
progressBar <- pbmcapply::progressBar
274274
eraseTxtProgressBar <- function(pb) {
@@ -366,7 +366,7 @@ pbmcapply_handler <- function(substyle = 3L, style = "ETA", file = stderr(), int
366366
#' @example incl/progress_handler.R
367367
#'
368368
#' @export
369-
progress_handler <- function(format = "[:bar] :percent :message", show_after = 0.0, intrusiveness = getOption("progressr.intrusiveness.terminal", 1), ...) {
369+
progress_handler <- function(format = "[:bar] :percent :message", show_after = 0.0, intrusiveness = getOption("progressr.intrusiveness.terminal", 1), target = "terminal", ...) {
370370
if (!is_fake("progress_handler")) {
371371
progress_bar <- progress::progress_bar
372372
} else {
@@ -440,7 +440,7 @@ progress_handler <- function(format = "[:bar] :percent :message", show_after = 0
440440
#' @example incl/beepr_handler.R
441441
#'
442442
#' @export
443-
beepr_handler <- function(initiate = 2L, update = 10L, finish = 11L, intrusiveness = getOption("progressr.intrusiveness.auditory", 5.0), ...) {
443+
beepr_handler <- function(initiate = 2L, update = 10L, finish = 11L, intrusiveness = getOption("progressr.intrusiveness.auditory", 5.0), target = "audio", ...) {
444444
## Used for package testing purposes only when we want to perform
445445
## everything except the last part where the backend is called
446446
if (!is_fake("beepr_handler")) {
@@ -491,7 +491,7 @@ beepr_handler <- function(initiate = 2L, update = 10L, finish = 11L, intrusiven
491491
#' @example incl/notifier_handler.R
492492
#'
493493
#' @export
494-
notifier_handler <- function(intrusiveness = getOption("progressr.intrusiveness.notifier", 10), ...) {
494+
notifier_handler <- function(intrusiveness = getOption("progressr.intrusiveness.notifier", 10), target = "gui", ...) {
495495
## Used for package testing purposes only when we want to perform
496496
## everything except the last part where the backend is called
497497
if (!is_fake("notifier_handler")) {
@@ -548,7 +548,7 @@ notifier_handler <- function(intrusiveness = getOption("progressr.intrusiveness.
548548
#' @example incl/debug_handler.R
549549
#'
550550
#' @export
551-
debug_handler <- function(interval = getOption("progressr.interval", 0), intrusiveness = getOption("progressr.intrusiveness.debug", 0), ...) {
551+
debug_handler <- function(interval = getOption("progressr.interval", 0), intrusiveness = getOption("progressr.intrusiveness.debug", 0), target = "terminal", ...) {
552552
reporter <- local({
553553
t_init <- NULL
554554

@@ -599,7 +599,7 @@ debug_handler <- function(interval = getOption("progressr.interval", 0), intrusi
599599
#' @param \ldots Additional arguments passed to [make_progression_handler()].
600600
#'
601601
#' @export
602-
newline_handler <- function(symbol = "\n", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.debug", 0), ...) {
602+
newline_handler <- function(symbol = "\n", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.debug", 0), target = "terminal", ...) {
603603
reporter <- local({
604604
list(
605605
initiate = function(...) cat(file = file, symbol),
@@ -630,7 +630,7 @@ newline_handler <- function(symbol = "\n", file = stderr(), intrusiveness = getO
630630
#'
631631
#' @importFrom utils file_test
632632
#' @export
633-
filesize_handler <- function(file = "default.progress", intrusiveness = getOption("progressr.intrusiveness.file", 5), ...) {
633+
filesize_handler <- function(file = "default.progress", intrusiveness = getOption("progressr.intrusiveness.file", 5), target = "file", ...) {
634634
reporter <- local({
635635
set_file_size <- function(config, state, progression) {
636636
ratio <- state$step / config$max_steps
@@ -696,7 +696,7 @@ filesize_handler <- function(file = "default.progress", intrusiveness = getOptio
696696
#' }}
697697
#'
698698
#' @export
699-
shiny_handler <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), ...) {
699+
shiny_handler <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), target = "gui", ...) {
700700
reporter <- local({
701701
list(
702702
update = function(config, state, progression, ...) {

R/withProgress2.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @example incl/shiny-withProgress.R
1010
#'
1111
#' @export
12-
withProgress2 <- function(expr, ..., env = parent.frame(), quoted = FALSE, handlers = c(shiny = shiny_handler, getOption("progressr.handlers"))) {
12+
withProgress2 <- function(expr, ..., env = parent.frame(), quoted = FALSE, handlers = c(shiny = shiny_handler, progressr::handlers(default = NULL))) {
1313
if (!quoted) expr <- substitute(expr)
1414
expr <- bquote(progressr::with_progress({.(expr)}, handlers = .(handlers)))
1515
res <- withVisible(shiny::withProgress(expr, ..., env = env, quoted = TRUE))

R/with_progress.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#' @example incl/with_progress.R
2626
#'
2727
#' @export
28-
with_progress <- function(expr, handlers = getOption("progressr.handlers", txtprogressbar_handler), cleanup = TRUE, delay_stdout = getOption("progressr.delay_stdout", interactive()), delay_conditions = getOption("progressr.delay_conditions", if (interactive()) c("condition") else character(0L)), interval = NULL, enable = NULL) {
28+
with_progress <- function(expr, handlers = progressr::handlers(), cleanup = TRUE, delay_stdout = getOption("progressr.delay_stdout", interactive()), delay_conditions = getOption("progressr.delay_conditions", if (interactive()) c("condition") else character(0L)), interval = NULL, enable = NULL) {
2929
stop_if_not(is.logical(cleanup), length(cleanup) == 1L, !is.na(cleanup))
3030

3131
## FIXME: With zero handlers, progression conditions will be

man/ascii_alert_handler.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/beepr_handler.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/debug_handler.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/filesize_handler.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)