Skip to content

Commit 2b67622

Browse files
committed
Return the value of expr from with_progress()
1 parent 470b94b commit 2b67622

File tree

5 files changed

+90
-48
lines changed

5 files changed

+90
-48
lines changed

NEWS

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Package: progressr
33

44
Version: 0.6.0-9000 [2020-05-18]
55

6-
* ...
7-
6+
* with_progress() and without_progress() now return the result of evaluating
7+
'expr'.
8+
89

910
Version: 0.6.0 [2020-05-18]
1011

R/with_progress.R

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -232,55 +232,61 @@ with_progress <- function(expr, handlers = progressr::handlers(), cleanup = TRUE
232232

233233
## Evaluate expression
234234
capture_conditions <- TRUE
235-
withCallingHandlers(
236-
expr,
237-
progression = function(p) {
238-
## Don't capture conditions that are produced by progression handlers
239-
capture_conditions <<- FALSE
240-
on.exit(capture_conditions <<- TRUE)
235+
res <- withVisible(
236+
withCallingHandlers(
237+
expr,
238+
progression = function(p) {
239+
## Don't capture conditions that are produced by progression handlers
240+
capture_conditions <<- FALSE
241+
on.exit(capture_conditions <<- TRUE)
241242

242-
## Any buffered output to flush?
243-
if (flush_terminal) {
244-
if (length(conditions) > 0L || has_buffered_stdout(stdout_file)) {
245-
calling_handler(control_progression("hide"))
246-
stdout_file <<- flush_stdout(stdout_file, close = FALSE)
247-
conditions <<- flush_conditions(conditions)
248-
calling_handler(control_progression("unhide"))
243+
## Any buffered output to flush?
244+
if (flush_terminal) {
245+
if (length(conditions) > 0L || has_buffered_stdout(stdout_file)) {
246+
calling_handler(control_progression("hide"))
247+
stdout_file <<- flush_stdout(stdout_file, close = FALSE)
248+
conditions <<- flush_conditions(conditions)
249+
calling_handler(control_progression("unhide"))
250+
}
249251
}
250-
}
251-
252-
calling_handler(p)
253-
},
254-
condition = function(c) {
255-
if (!capture_conditions || inherits(c, c("progression", "error"))) return()
256-
if (inherits(c, delay_conditions)) {
257-
## Record
258-
conditions[[length(conditions) + 1L]] <<- c
259-
## Muffle
260-
if (inherits(c, "message")) {
261-
invokeRestart("muffleMessage")
262-
} else if (inherits(c, "warning")) {
263-
invokeRestart("muffleWarning")
264-
} else if (inherits(c, "condition")) {
265-
## If there is a "muffle" restart for this condition,
266-
## then invoke that restart, i.e. "muffle" the condition
267-
restarts <- computeRestarts(c)
268-
for (restart in restarts) {
269-
name <- restart$name
270-
if (is.null(name)) next
271-
if (!grepl("^muffle", name)) next
272-
invokeRestart(restart)
273-
break
252+
253+
calling_handler(p)
254+
},
255+
condition = function(c) {
256+
if (!capture_conditions || inherits(c, c("progression", "error"))) return()
257+
if (inherits(c, delay_conditions)) {
258+
## Record
259+
conditions[[length(conditions) + 1L]] <<- c
260+
## Muffle
261+
if (inherits(c, "message")) {
262+
invokeRestart("muffleMessage")
263+
} else if (inherits(c, "warning")) {
264+
invokeRestart("muffleWarning")
265+
} else if (inherits(c, "condition")) {
266+
## If there is a "muffle" restart for this condition,
267+
## then invoke that restart, i.e. "muffle" the condition
268+
restarts <- computeRestarts(c)
269+
for (restart in restarts) {
270+
name <- restart$name
271+
if (is.null(name)) next
272+
if (!grepl("^muffle", name)) next
273+
invokeRestart(restart)
274+
break
275+
}
274276
}
275277
}
276278
}
277-
}
279+
)
278280
)
279-
281+
280282
## Success
281283
status <- "ok"
282-
283-
invisible(NULL)
284+
285+
if (res$visible) {
286+
res$value
287+
} else {
288+
invisible(res$value)
289+
}
284290
}
285291

286292

R/without_progress.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
#' @rdname with_progress
66
#' @export
77
without_progress <- function(expr) {
8-
withCallingHandlers(expr, progression = function(p) {
9-
invokeRestart("muffleProgression")
10-
})
11-
12-
invisible(NULL)
8+
res <- withVisible(
9+
withCallingHandlers(expr, progression = function(p) {
10+
invokeRestart("muffleProgression")
11+
})
12+
)
13+
14+
if (res$visible) {
15+
res$value
16+
} else {
17+
invisible(res$value)
18+
}
1319
}

tests/with_progress.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ if (requireNamespace("utils", quietly = TRUE)) {
167167
message("with_progress() - multiple handlers ... done")
168168

169169

170+
message("with_progress() - return value and visibility ...")
171+
172+
res <- with_progress(x)
173+
stopifnot(identical(x, res))
174+
175+
res <- withVisible(with_progress(x))
176+
stopifnot(identical(res$visible, TRUE))
177+
178+
res <- withVisible(with_progress(y <- x))
179+
stopifnot(identical(res$visible, FALSE))
180+
181+
message("with_progress() - return value and visibility ... done")
182+
183+
170184
message("with_progress() ... done")
171185

172186
source("incl/end.R")

tests/without_progress.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,19 @@ with_progress(without_progress(y <- slow_sum(x)))
1313

1414
message("without_progress() ... done")
1515

16+
17+
message("without_progress() - return value and visibility ...")
18+
19+
res <- without_progress(x)
20+
stopifnot(identical(x, res))
21+
22+
res <- withVisible(without_progress(x))
23+
stopifnot(identical(res$visible, TRUE))
24+
25+
res <- withVisible(without_progress(y <- x))
26+
stopifnot(identical(res$visible, FALSE))
27+
28+
message("without_progress() - return value and visibility ... done")
29+
30+
1631
source("incl/end.R")

0 commit comments

Comments
 (0)