Skip to content

Commit 829e253

Browse files
Merge branch 'release/0.1.2'
2 parents a8aeb4b + 1f565f8 commit 829e253

File tree

15 files changed

+231
-36
lines changed

15 files changed

+231
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*~
33
**/*~
44
.local
5+
inst/doc

DESCRIPTION

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
Package: progressr
2-
Version: 0.1.1
2+
Version: 0.1.2
33
Title: A Unifying API for Progress Updates
44
Description: A minimal API for reporting progress updates upstream. The design is to separate the representation of progress updates from how they are presented. What type of progress to signal is controlled by the developer. How these progress updates are rendered is controlled by the end user. For instance, some users may prefer visual feedback such as a horizontal progress bar in the terminal, whereas others may prefer auditory feedback.
55
Authors@R: c(
66
person("Henrik", "Bengtsson", role=c("aut", "cre", "cph"),
77
email = "[email protected]"))
88
License: GPL (>= 3)
99
Imports:
10-
digest
10+
digest
1111
Suggests:
12-
utils,
13-
tcltk,
14-
beepr,
15-
notifier,
16-
pbmcapply,
17-
plyr,
18-
progress
12+
utils,
13+
tcltk,
14+
beepr,
15+
notifier,
16+
pbmcapply,
17+
plyr,
18+
progress,
19+
future,
20+
doFuture,
21+
future.apply,
22+
furrr
1923
Remotes:
2024
gaborcsardi/notifier@d92b1b6
2125
URL: https://github.com/HenrikBengtsson/progressr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
S3method(print,progression)
4+
S3method(print,progression_handler)
45
S3method(print,progressor)
56
export(ascii_alert_handler)
67
export(beepr_handler)

NEWS

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
Package: progressr
22
==================
33

4+
Version: 0.1.2 [2019-06-14]
5+
6+
NEW FEATURES:
7+
8+
* with_progress() gained arguments 'enable' and 'interval' as an alternative
9+
to setting corresponding options progressr.*.
10+
11+
* Now option 'progressr.interval' defaults to 0.0 (was 0.5 seconds).
12+
13+
* Added print() for 'progression_handler' objects.
14+
15+
BUG FIXES:
16+
17+
* with_progress(..., delay_conditions = "condition"), introduced in v0.1.0,
18+
would also capture conditions produced by progression handlers, e.g.
19+
progress::progress_bar() output would not be displayed until the very end.
20+
21+
422
Version: 0.1.1 [2019-06-08]
523

624
NEW FEATURES:

R/progression_handler.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#' @return A function of class `progression_handler`.
2929
#'
3030
#' @export
31-
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.5), intrusiveness = 1.0, clear = getOption("progressr.clear", TRUE)) {
31+
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)) {
3232
if (!enable) times <- 0
3333
name <- as.character(name)
3434
stop_if_not(length(name) == 1L, !is.na(name), nzchar(name))
@@ -279,3 +279,35 @@ progression_handler <- function(name, reporter = list(), handler = NULL, enable
279279

280280
handler
281281
}
282+
283+
284+
#' @export
285+
print.progression_handler <- function(x, ...) {
286+
print(sys.calls())
287+
s <- sprintf("Progression handler of class %s:", sQuote(class(x)[1]))
288+
289+
env <- environment(x)
290+
s <- c(s, " * configuration:")
291+
s <- c(s, sprintf(" - name: %s", sQuote(env$name %||% "<NULL>")))
292+
s <- c(s, sprintf(" - max_steps: %s", env$max_steps %||% "<NULL>"))
293+
s <- c(s, sprintf(" - enable: %s", env$enable))
294+
s <- c(s, sprintf(" - enable_after: %g seconds", env$enable_after))
295+
s <- c(s, sprintf(" - times: %g", env$times))
296+
s <- c(s, sprintf(" - interval: %g seconds", env$interval))
297+
s <- c(s, sprintf(" - intrusiveness: %g", env$intrusiveness))
298+
s <- c(s, sprintf(" - auto_finish: %s", env$auto_finish))
299+
s <- c(s, sprintf(" - clear: %s", env$clear))
300+
s <- c(s, sprintf(" - milestones: %s", hpaste(env$milestones %||% "<NULL>")))
301+
s <- c(s, sprintf(" - owner: %s", hpaste(env$owner %||% "<NULL>")))
302+
303+
s <- c(s, " * state:")
304+
s <- c(s, sprintf(" - enabled: %s", env$enabled))
305+
s <- c(s, sprintf(" - finished: %s", env$finished))
306+
s <- c(s, sprintf(" - step: %s", env$step %||% "<NULL>"))
307+
s <- c(s, sprintf(" - prev_milestone: %s", env$prev_milestone %||% "<NULL>"))
308+
s <- c(s, sprintf(" - delta: %g", (env$step - env$prev_milestone) %||% 0L))
309+
s <- c(s, sprintf(" - timestamps: %s", hpaste(env$timestamps %||% "<NULL>")))
310+
311+
s <- paste(s, collapse = "\n")
312+
cat(s, "\n", sep = "")
313+
}

R/slow_sum.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
slow_sum <- function(x, delay = getOption("delay", 0.05), stdout = FALSE, message = FALSE) {
1919
progress <- progressor(length(x))
2020

21-
res <- 0
21+
sum <- 0
2222
for (kk in seq_along(x)) {
23-
if (stdout) cat(sprintf("Adding element #%d\n", kk))
23+
if (stdout) cat(sprintf("O: Element #%d\n", kk))
2424
Sys.sleep(delay)
25-
res <- res + x[kk]
26-
progress(message = sprintf("Adding %g", kk))
27-
if (message) message(sprintf("Added value %g", x[kk]))
25+
sum <- sum + x[kk]
26+
progress(message = sprintf("P: Adding %g", kk))
27+
if (message) message(sprintf("M: Added value %g", x[kk]))
2828
}
2929

30-
res
30+
sum
3131
}

R/utils.R

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
## From R.utils 2.0.2 (2015-05-23)
2+
hpaste <- function(..., sep = "", collapse = ", ", lastCollapse = NULL, maxHead = if (missing(lastCollapse)) 3 else Inf, maxTail = if (is.finite(maxHead)) 1 else Inf, abbreviate = "...") {
3+
if (is.null(lastCollapse)) lastCollapse <- collapse
4+
5+
# Build vector 'x'
6+
x <- paste(..., sep = sep)
7+
n <- length(x)
8+
9+
# Nothing todo?
10+
if (n == 0) return(x)
11+
if (is.null(collapse)) return(x)
12+
13+
# Abbreviate?
14+
if (n > maxHead + maxTail + 1) {
15+
head <- x[seq_len(maxHead)]
16+
tail <- rev(rev(x)[seq_len(maxTail)])
17+
x <- c(head, abbreviate, tail)
18+
n <- length(x)
19+
}
20+
21+
if (!is.null(collapse) && n > 1) {
22+
if (lastCollapse == collapse) {
23+
x <- paste(x, collapse = collapse)
24+
} else {
25+
xT <- paste(x[1:(n-1)], collapse = collapse)
26+
x <- paste(xT, x[n], sep = lastCollapse)
27+
}
28+
}
29+
30+
x
31+
} # hpaste()
32+
133
# More efficient than the default utils::capture.output()
234
#' @importFrom utils capture.output
335
capture_output <- function(expr, envir = parent.frame(), ...) {
@@ -70,6 +102,12 @@ is_fake <- local({
70102
known_progression_handlers <- function() {
71103
ns <- asNamespace(.packageName)
72104
handlers <- ls(envir = ns, pattern = "_handler$")
73-
handlers <- setdiff(handlers, "progression_handler")
74-
mget(handlers, envir = ns, inherits = FALSE)
105+
handlers <- setdiff(handlers, c("progression_handler", "print.progression_handler"))
106+
handlers <- mget(handlers, envir = ns, inherits = FALSE)
107+
handlers
108+
}
109+
110+
111+
`%||%` <- function(lhs, rhs) {
112+
if (is.null(lhs)) rhs else lhs
75113
}

R/with_progress.R

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,40 @@
1515
#' classes to be captured and relayed at the end after any captured
1616
#' standard output is relayed.
1717
#'
18+
#' @param interval (numeric) The minimum time (in seconds) between
19+
#' successive progression updates from handlers.
20+
#'
21+
#' @param enable (logical) If FALSE, then progress is not reported.
22+
#'
1823
#' @return Return nothing (reserved for future usage).
1924
#'
2025
#' @example incl/with_progress.R
2126
#'
2227
#' @export
23-
with_progress <- function(expr, handlers = getOption("progressr.handlers", txtprogressbar_handler()), cleanup = TRUE, delay_stdout = TRUE, delay_conditions = c("condition")) {
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) {
2429
stop_if_not(is.logical(cleanup), length(cleanup) == 1L, !is.na(cleanup))
2530

2631
## FIXME: With zero handlers, progression conditions will be
2732
## passed on upstream just as without with_progress().
2833
## Is that what we want? /HB 2019-05-17
2934
if (length(handlers) == 0L) return(expr)
3035
if (!is.list(handlers)) handlers <- list(handlers)
31-
36+
37+
## Temporarily set progressr options
38+
options <- list()
39+
if (!is.null(interval)) {
40+
stop_if_not(is.numeric(interval), length(interval) == 1L, !is.na(interval))
41+
options[["progressr.interval"]] <- interval
42+
}
43+
if (!is.null(enable)) {
44+
stop_if_not(is.logical(enable), length(enable) == 1L, !is.na(enable))
45+
options[["progressr.enable"]] <- enable
46+
}
47+
if (length(options) > 0L) {
48+
oopts <- options(options)
49+
on.exit(options(oopts))
50+
}
51+
3252
for (kk in seq_along(handlers)) {
3353
handler <- handlers[[kk]]
3454
stopifnot(is.function(handler))
@@ -63,7 +83,7 @@ with_progress <- function(expr, handlers = getOption("progressr.handlers", txtpr
6383
signalCondition(control_progression("shutdown", status = status))
6484
}, muffleProgression = function(p) NULL)
6585
}, progression = handler)
66-
})
86+
}, add = TRUE)
6787
}
6888

6989
## Captured stdout output and conditions
@@ -79,7 +99,7 @@ with_progress <- function(expr, handlers = getOption("progressr.handlers", txtpr
7999
stdout <- rawToChar(rawConnectionValue(stdout_file))
80100
close(stdout_file)
81101
if (length(stdout) > 0) cat(stdout, file = stdout())
82-
})
102+
}, add = TRUE)
83103
}
84104

85105
## Delay conditions?
@@ -99,7 +119,7 @@ with_progress <- function(expr, handlers = getOption("progressr.handlers", txtpr
99119
}
100120
}, add = TRUE)
101121
}
102-
}
122+
} ## if (delay_stdout || length(delay_conditions) > 0)
103123

104124
## Reset all handlers up start
105125
withCallingHandlers({
@@ -109,11 +129,17 @@ with_progress <- function(expr, handlers = getOption("progressr.handlers", txtpr
109129
}, progression = handler)
110130

111131
## Evaluate expression
132+
capture_conditions <- TRUE
112133
withCallingHandlers(
113134
expr,
114-
progression = handler,
135+
progression = function(p) {
136+
## Don't capture conditions that are produced by progression handlers
137+
capture_conditions <<- FALSE
138+
on.exit(capture_conditions <<- TRUE)
139+
handler(p)
140+
},
115141
condition = function(c) {
116-
if (inherits(c, c("progression", "error"))) return()
142+
if (!capture_conditions || inherits(c, c("progression", "error"))) return()
117143
if (inherits(c, delay_conditions)) {
118144
## Record
119145
conditions[[length(conditions) + 1L]] <<- c

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Assume that we have a function `slow_sum()` for adding up the values in a vector
1414
```r
1515
slow_sum <- function(x) {
1616
progress <- progressr::progressor(length(x))
17-
res <- 0
17+
sum <- 0
1818
for (kk in seq_along(x)) {
1919
Sys.sleep(0.1)
20-
res <- res + x[kk]
21-
progress()
20+
sum <- sum + x[kk]
21+
progress(message = sprintf("Added %g", x[kk]))
2222
}
23-
res
23+
sum
2424
}
2525
```
2626

@@ -59,7 +59,7 @@ options(progressr.handlers = progress_handler)
5959
This progress handler will present itself as:
6060
```r
6161
> with_progress(y <- slow_sum(1:10))
62-
[=====================>--------------------------------] 40%
62+
[==================>---------------------------] 40% Added 4
6363
```
6464

6565

@@ -83,11 +83,33 @@ will present itself as sounds played at the beginning, while progressing, and at
8383

8484
It is possible to have multiple progress handlers presenting progress updates at the same time. For example, to get both visual and auditory updates, use:
8585
```r
86-
options(progressr.handlers = list(txtprogress_barhandler, beepr_handler))
86+
options(progressr.handlers = list(txtprogressbar_handler, beepr_handler))
87+
```
88+
89+
90+
### Debugging
91+
92+
To debug progress updates, use:
93+
```r
94+
> options(progressr.handlers = debug_handler)
95+
> with_progress(y <- slow_sum(1:10))
96+
[13:33:50.776] (1.033s => +0.001s) shutdown: 10/10 (+0) '' {clear=TRUE, enabled=TRUE, status=ok}
97+
[13:33:49.743] (0.000s => +0.002s) initiate: 0/10 (+0) '' {clear=TRUE, enabled=TRUE, status=}
98+
[13:33:49.847] (0.104s => +0.001s) update: 1/10 (+1) 'Added 1' {clear=TRUE, enabled=TRUE, status=}
99+
[13:33:49.950] (0.206s => +0.001s) update: 2/10 (+1) 'Added 2' {clear=TRUE, enabled=TRUE, status=}
100+
[13:33:50.052] (0.309s => +0.000s) update: 3/10 (+1) 'Added 3' {clear=TRUE, enabled=TRUE, status=}
101+
[13:33:50.154] (0.411s => +0.001s) update: 4/10 (+1) 'Added 4' {clear=TRUE, enabled=TRUE, status=}
102+
[13:33:50.257] (0.514s => +0.001s) update: 5/10 (+1) 'Added 5' {clear=TRUE, enabled=TRUE, status=}
103+
[13:33:50.361] (0.618s => +0.002s) update: 6/10 (+1) 'Added 6' {clear=TRUE, enabled=TRUE, status=}
104+
[13:33:50.464] (0.721s => +0.001s) update: 7/10 (+1) 'Added 7' {clear=TRUE, enabled=TRUE, status=}
105+
[13:33:50.567] (0.824s => +0.001s) update: 8/10 (+1) 'Added 8' {clear=TRUE, enabled=TRUE, status=}
106+
[13:33:50.670] (0.927s => +0.001s) update: 9/10 (+1) 'Added 9' {clear=TRUE, enabled=TRUE, status=}
107+
[13:33:50.773] (1.030s => +0.001s) update: 10/10 (+1) 'Added 10' {clear=TRUE, enabled=TRUE, status=}
108+
[13:33:50.774] (1.031s => +0.003s) update: 10/10 (+0) 'Added 10' {clear=TRUE, enabled=TRUE, status=}
87109
```
88110

89111
<small>
90-
(*) To set the default progress presenter in all R session, set this option in your '~/.Rprofile' file.
112+
(*) To set the default progress presenter in all R session, set this option in your `~/.Rprofile` file.
91113
</small>
92114

93115

man/progression_handler.Rd

Lines changed: 1 addition & 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)