-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathpmap.R
More file actions
205 lines (192 loc) · 5.53 KB
/
pmap.R
File metadata and controls
205 lines (192 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#' Map over multiple input simultaneously (in "parallel")
#'
#' @description
#' These functions are variants of [map()] that iterate over multiple arguments
#' simultaneously. They are parallel in the sense that each input is processed
#' in parallel with the others, not in the sense of multicore computing, i.e.
#' they share the same notion of "parallel" as [base::pmax()] and [base::pmin()].
#'
#' @param .l A list of vectors. The length of `.l` determines the number of
#' arguments that `.f` will be called with. Arguments will be supply by
#' position if unnamed, and by name if named.
#'
#' Vectors of length 1 will be recycled to any length; all other elements
#' must be have the same length.
#'
#' A data frame is an important special case of `.l`. It will cause `.f`
#' to be called once for each row.
#' @param .f A function, specified in one of the following ways:
#'
#' * A named function.
#' * An anonymous function, e.g. `\(x, y, z) x + y / z` or
#' `function(x, y, z) x + y / z`
#' * A formula, e.g. `~ ..1 + ..2 / ..3`. No longer recommended.
#'
#' `r lifecycle::badge("experimental")`
#'
#' Wrap a function with [in_parallel()] to declare that it should be performed
#' in parallel. See [in_parallel()] for more details.
#' Use of `...` is not permitted in this context.
#' @inheritParams map
#' @returns
#' The output length is determined by the maximum length of all elements of `.l`.
#' The output names are determined by the names of the first element of `.l`.
#' The output type is determined by the suffix:
#'
#' * No suffix: a list; `.f()` can return anything.
#'
#' * `_lgl()`, `_int()`, `_dbl()`, `_chr()` return a logical, integer, double,
#' or character vector respectively; `.f()` must return a compatible atomic
#' vector of length 1.
#'
#' * `_vec()` return an atomic or S3 vector, the same type that `.f` returns.
#' `.f` can return pretty much any type of vector, as long as it is length 1.
#'
#' * `pwalk()` returns the input `.l` (invisibly). This makes it easy to
#' use in a pipe. The return value of `.f()` is ignored.
#'
#' Any errors thrown by `.f` will be wrapped in an error with class
#' [purrr_error_indexed].
#' @family map variants
#' @export
#' @examples
#' x <- list(1, 1, 1)
#' y <- list(10, 20, 30)
#' z <- list(100, 200, 300)
#' pmap(list(x, y, z), sum)
#'
#' # Matching arguments by position
#' pmap(list(x, y, z), function(first, second, third) (first + third) * second)
#'
#' # Matching arguments by name
#' l <- list(a = x, b = y, c = z)
#' pmap(l, function(c, b, a) (a + c) * b)
#'
#' # Vectorizing a function over multiple arguments
#' df <- data.frame(
#' x = c("apple", "banana", "cherry"),
#' pattern = c("p", "n", "h"),
#' replacement = c("P", "N", "H"),
#' stringsAsFactors = FALSE
#' )
#' pmap(df, gsub)
#' pmap_chr(df, gsub)
#'
#' # Use `...` to absorb unused components of input list .l
#' df <- data.frame(
#' x = 1:3,
#' y = 10:12,
#' z = letters[1:3]
#' )
#' plus <- function(x, y) x + y
#' \dontrun{
#' # this won't work
#' pmap(df, plus)
#' }
#' # but this will
#' plus2 <- function(x, y, ...) x + y
#' pmap_dbl(df, plus2)
#'
#' # The "p" for "parallel" in pmap() is the same as in base::pmin()
#' # and base::pmax()
#' df <- data.frame(
#' x = c(1, 2, 5),
#' y = c(5, 4, 8)
#' )
#' # all produce the same result
#' pmin(df$x, df$y)
#' map2_dbl(df$x, df$y, min)
#' pmap_dbl(df, min)
pmap <- function(.l, .f, ..., .progress = FALSE) {
pmap_("list", .l, .f, ..., .progress = .progress)
}
#' @export
#' @rdname pmap
pmap_lgl <- function(.l, .f, ..., .progress = FALSE) {
pmap_("logical", .l, .f, ..., .progress = .progress)
}
#' @export
#' @rdname pmap
pmap_int <- function(.l, .f, ..., .progress = FALSE) {
pmap_("integer", .l, .f, ..., .progress = .progress)
}
#' @export
#' @rdname pmap
pmap_dbl <- function(.l, .f, ..., .progress = FALSE) {
pmap_("double", .l, .f, ..., .progress = .progress)
}
#' @export
#' @rdname pmap
pmap_chr <- function(.l, .f, ..., .progress = FALSE) {
pmap_("character", .l, .f, ..., .progress = .progress)
}
pmap_ <- function(
.type,
.l,
.f,
...,
.progress = FALSE,
.purrr_user_env = caller_env(2),
.purrr_error_call = caller_env()
) {
.progress <- as_progress(
.progress,
user_env = .purrr_user_env,
caller_env = .purrr_error_call
)
.l <- vctrs_list_compat(.l, error_call = .purrr_error_call)
.l <- map(.l, vctrs_vec_compat)
n <- vec_size_common(!!!.l, .arg = ".l", .call = .purrr_error_call)
.l <- vec_recycle_common(
!!!.l,
.size = n,
.arg = ".l",
.call = .purrr_error_call
)
if (length(.l) > 0L) {
names <- vec_names(.l[[1L]])
} else {
names <- NULL
}
.f <- as_mapper(.f, ...)
if (running_in_parallel(.f)) {
attributes(.l) <- list(
names = names(.l),
class = "data.frame",
row.names = if (is.null(names)) .set_row_names(n) else names
)
return(mmap_(.l, .f, .progress, .type, .purrr_error_call, ...))
}
call_names <- names(.l)
call_n <- length(.l)
i <- 0L
with_indexed_errors(
i = i,
names = names,
error_call = .purrr_error_call,
call_with_cleanup(
pmap_impl,
environment(),
.type,
.progress,
n,
names,
i,
call_names,
call_n
)
)
}
#' @export
#' @rdname pmap
pmap_vec <- function(.l, .f, ..., .ptype = NULL, .progress = FALSE) {
.f <- as_mapper(.f, ...)
out <- pmap(.l, .f, ..., .progress = .progress)
simplify_impl(out, ptype = .ptype)
}
#' @export
#' @rdname pmap
pwalk <- function(.l, .f, ..., .progress = FALSE) {
pmap(.l, .f, ..., .progress = .progress)
invisible(.l)
}