-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathdeprec-rerun.R
More file actions
80 lines (75 loc) · 2.07 KB
/
deprec-rerun.R
File metadata and controls
80 lines (75 loc) · 2.07 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
#' Re-run expressions multiple times
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' This function was deprecated in purrr 1.0.0 because we believe that NSE
#' functions are not a good fit for purrr. Also, `rerun(n, x)` can just as
#' easily be expressed as `map(1:n, \(i) x)`
#'
#' `rerun()` is a convenient way of generating sample data. It works similarly to
#' \code{\link{replicate}(..., simplify = FALSE)}.
#'
#' @param .n Number of times to run expressions
#' @param ... Expressions to re-run.
#' @return A list of length `.n`. Each element of `...` will be
#' re-run once for each `.n`.
#'
#' There is one special case: if there's a single unnamed input, the second
#' level list will be dropped. In this case, `rerun(n, x)` behaves like
#' `replicate(n, x, simplify = FALSE)`.
#' @export
#' @keywords internal
#' @examples
#' # old
#' 5 |> rerun(rnorm(5)) |> str()
#' # new
#' 1:5 |> map(\(i) rnorm(5)) |> str()
#'
#' # old
#' 5 |>
#' rerun(x = rnorm(5), y = rnorm(5)) |>
#' map_dbl(\(l) cor(l$x, l$y))
#' # new
#' 1:5 |>
#' map(\(i) list(x = rnorm(5), y = rnorm(5))) |>
#' map_dbl(\(l) cor(l$x, l$y))
rerun <- function(.n, ...) {
deprec_rerun(.n, ..., .purrr_user_env = caller_env())
dots <- quos(...)
# Special case: if single unnamed argument, insert directly into the output
# rather than wrapping in a list.
if (length(dots) == 1 && !is_named(dots)) {
dots <- dots[[1]]
eval_dots <- eval_tidy
} else {
eval_dots <- function(x) lapply(x, eval_tidy)
}
out <- vector("list", .n)
for (i in seq_len(.n)) {
out[[i]] <- eval_dots(dots)
}
out
}
deprec_rerun <- function(.n, ..., .purrr_user_env) {
n <- .n
old <- substitute(rerun(n, ...))
if (dots_n(...) == 1) {
new <- substitute(map(1:n, ~...))
} else {
new <- substitute(map(1:n, ~ list(...)))
}
lifecycle::deprecate_warn(
when = "1.0.0",
what = "rerun()",
with = "map()",
details = c(
" " = "# Previously",
" " = expr_deparse(old),
"",
" " = "# Now",
" " = expr_deparse(new)
),
user_env = .purrr_user_env
)
}