-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathlist-modify.R
More file actions
147 lines (131 loc) · 3.87 KB
/
list-modify.R
File metadata and controls
147 lines (131 loc) · 3.87 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
#' Modify a list
#'
#' @description
#' * `list_assign()` modifies the elements of a list by name or position.
#' * `list_modify()` modifies the elements of a list recursively.
#' * `list_merge()` merges the elements of a list recursively.
#'
#' `list_modify()` is inspired by [utils::modifyList()].
#'
#' @param .x List to modify.
#' @param ... New values of a list. Use `zap()` to remove values.
#'
#' These values should be either all named or all unnamed. When
#' inputs are all named, they are matched to `.x` by name. When they
#' are all unnamed, they are matched by position.
#'
#' [Dynamic dots][rlang::dyn-dots] are supported. In particular, if your
#' replacement values are stored in a list, you can splice that in with
#' `!!!`.
#' @inheritParams map_depth
#' @export
#' @examples
#' x <- list(x = 1:10, y = 4, z = list(a = 1, b = 2))
#' str(x)
#'
#' # Update values
#' str(list_assign(x, a = 1))
#'
#' # Replace values
#' str(list_assign(x, z = 5))
#' str(list_assign(x, z = NULL))
#' str(list_assign(x, z = list(a = 1:5)))
#'
#' # Replace recursively with list_modify(), leaving the other elements of z alone
#' str(list_modify(x, z = list(a = 1:5)))
#'
#' # Remove values
#' str(list_assign(x, z = zap()))
#'
#' # Combine values with list_merge()
#' str(list_merge(x, x = 11, z = list(a = 2:5, c = 3)))
#'
#' # All these functions support dynamic dots features. Use !!! to splice
#' # a list of arguments:
#' l <- list(new = 1, y = zap(), z = 5)
#' str(list_assign(x, !!!l))
list_assign <- function(.x, ..., .is_node = NULL) {
check_list(.x)
y <- dots_list(..., .named = NULL, .homonyms = "error")
list_recurse(.x, y, function(x, y) y, recurse = FALSE, is_node = .is_node)
}
#' @export
#' @rdname list_assign
list_modify <- function(.x, ..., .is_node = NULL) {
check_list(.x)
y <- dots_list(..., .named = NULL, .homonyms = "error")
list_recurse(.x, y, function(x, y) y, is_node = .is_node)
}
#' @export
#' @rdname list_assign
list_merge <- function(.x, ..., .is_node = NULL) {
check_list(.x)
y <- dots_list(..., .named = NULL, .homonyms = "error")
list_recurse(.x, y, c, is_node = .is_node)
}
list_recurse <- function(
x,
y,
base_f,
recurse = TRUE,
error_call = caller_env(),
is_node = NULL
) {
is_node <- as_is_node(
is_node,
error_call = error_call,
error_arg = ".is_node"
)
if (!is_null(names(y)) && !is_named(y)) {
cli::cli_abort(
"`...` arguments must be either all named or all unnamed.",
call = error_call
)
}
idx <- names(y) %||% rev(seq_along(y))
for (i in idx) {
x_i <- pluck(x, i)
y_i <- pluck(y, i)
if (is_zap(y_i)) {
x[[i]] <- NULL
} else if (recurse && is_node(x_i) && is_node(y_i)) {
list_slice2(x, i) <- list_recurse(x_i, y_i, base_f)
} else {
list_slice2(x, i) <- base_f(x_i, y_i)
}
}
x
}
check_list <- function(x, call = caller_env(), arg = caller_arg(x)) {
if (!is.list(x)) {
cli::cli_abort(
"{.arg {arg}} must be a list, not {.obj_type_friendly {x}}.",
call = call,
arg = arg
)
}
}
#' Update a list with formulas
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `update_list()` was deprecated in purrr 1.0.0, because we no longer believe
#' that functions that use NSE are a good fit for purrr.
#'
#' `update_list()` handles formulas and quosures that can refer to
#' values existing within the input list. This function is deprecated
#' because we no longer believe that functions that use tidy evaluation are
#' a good fit for purrr.
#'
#' @inheritParams list_modify
#' @export
#' @keywords internal
update_list <- function(.x, ...) {
lifecycle::deprecate_warn("1.0.0", "update_list()")
dots <- dots_list(...)
formulas <- map_lgl(dots, is_bare_formula, lhs = FALSE, scoped = TRUE)
dots <- map_if(dots, formulas, as_quosure)
dots <- map_if(dots, is_quosure, eval_tidy, data = .x)
list_recurse(.x, dots, function(x, y) y)
}