Skip to content

Commit bc964e1

Browse files
authored
Allow progress bar to access caller environment (#1221)
Fixes #1078
1 parent 0c0dab4 commit bc964e1

File tree

7 files changed

+52
-0
lines changed

7 files changed

+52
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# purrr (development version)
22

3+
* Formatted strings for the progress bar could only access the global environment. `map()`, `map2()`, and `pmap()` have been updated to add `caller = .purrr_user_env` to `.progress` by default, allowing formatted strings to access to the current/parent environment (@jcolt45, #1078).
4+
35
* `as_mapper.default()` optimized by removing special named argument handling for primitive functions (@mtcarsalot, #1088).
46

57
* `list_flatten()` gains an `is_node` parameter taking a predicate function that determines whether an input element is a node or a leaf (@salim-b, #1179).

R/map.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ map_ <- function(
198198
.purrr_user_env = caller_env(2),
199199
.purrr_error_call = caller_env()
200200
) {
201+
.progress <- as_progress(
202+
.progress,
203+
user_env = .purrr_user_env,
204+
caller_env = .purrr_error_call
205+
)
206+
201207
.x <- vctrs_vec_compat(.x, .purrr_user_env)
202208
vec_assert(.x, arg = ".x", call = .purrr_error_call)
203209

R/map2.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ map2_ <- function(
6969
.purrr_user_env = caller_env(2),
7070
.purrr_error_call = caller_env()
7171
) {
72+
.progress <- as_progress(
73+
.progress,
74+
user_env = .purrr_user_env,
75+
caller_env = .purrr_error_call
76+
)
77+
7278
.x <- vctrs_vec_compat(.x, .purrr_user_env)
7379
.y <- vctrs_vec_compat(.y, .purrr_user_env)
7480

R/pmap.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ pmap_ <- function(
131131
.purrr_user_env = caller_env(2),
132132
.purrr_error_call = caller_env()
133133
) {
134+
.progress <- as_progress(
135+
.progress,
136+
user_env = .purrr_user_env,
137+
caller_env = .purrr_error_call
138+
)
139+
134140
.l <- vctrs_list_compat(.l, error_call = .purrr_error_call)
135141
.l <- map(.l, vctrs_vec_compat)
136142

R/progress-bars.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,23 @@
4949
#'
5050
#' @name progress_bars
5151
NULL
52+
53+
as_progress <- function(
54+
progress,
55+
user_env = caller_env(2),
56+
caller_env = caller_env()
57+
) {
58+
if (isFALSE(progress) || isTRUE(progress) || is_string(progress)) {
59+
progress
60+
} else if (is.list(progress)) {
61+
progress$caller <- progress$caller %||% user_env
62+
progress
63+
} else {
64+
stop_input_type(
65+
progress,
66+
c("TRUE", "FALSE", "a string", "a named list"),
67+
arg = ".progress",
68+
call = caller_env
69+
)
70+
}
71+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# useful for bad progress spec
2+
3+
Code
4+
map(1, .progress = 1)
5+
Condition
6+
Error in `map()`:
7+
! `.progress` must be TRUE, FALSE, a string, or a named list, not the number 1.
8+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test_that("useful for bad progress spec", {
2+
# Test map() to make sure we're passing the caller env correctly
3+
expect_snapshot(map(1, .progress = 1), error = TRUE)
4+
})

0 commit comments

Comments
 (0)