Debian Codegraph @

R/utils.R

raw
1 select_loc <- function(x,
2 sel,
3 ...,
4 include = NULL,
5 exclude = NULL,
6 strict = TRUE,
7 name_spec = NULL,
8 allow_rename = TRUE,
9 allow_empty = TRUE,
10 allow_predicates = TRUE,
11 error_call = current_env()) {
12 check_dots_empty()
13
14 eval_select(
15 enquo(sel),
16 x,
17 include = include,
18 exclude = exclude,
19 strict = strict,
20 name_spec = name_spec,
21 allow_rename = allow_rename,
22 allow_empty = allow_empty,
23 allow_predicates = allow_predicates,
24 error_call = error_call
25 )
26 }
27
28 rename_loc <- function(x,
29 sel,
30 ...,
31 strict = TRUE,
32 name_spec = NULL,
33 error_call = current_env()) {
34 check_dots_empty()
35 rename_impl(
36 x,
37 names(x),
38 enquo(sel),
39 strict = strict,
40 name_spec = name_spec,
41 error_call = error_call
42 )
43 }
44
45 relocate_loc <- function(x,
46 sel,
47 ...,
48 before = NULL,
49 after = NULL,
50 strict = TRUE,
51 name_spec = NULL,
52 allow_rename = TRUE,
53 allow_empty = TRUE,
54 before_arg = "before",
55 after_arg = "after",
56 error_call = current_env()) {
57 check_dots_empty()
58
59 eval_relocate(
60 expr = enquo(sel),
61 data = x,
62 before = enquo(before),
63 after = enquo(after),
64 strict = strict,
65 name_spec = name_spec,
66 allow_rename = allow_rename,
67 allow_empty = allow_empty,
68 before_arg = before_arg,
69 after_arg = after_arg,
70 error_call = error_call
71 )
72 }
73
74 any_valid_names <- function(nms) {
75 if (is_null(nms)) {
76 FALSE
77 } else {
78 !all(are_empty_name(nms))
79 }
80 }
81
82 are_empty_name <- function(nms) {
83 if (!is_character(nms)) {
84 abort("Expected a character vector")
85 }
86
87 nms == "" | is.na(nms)
88 }
89
90 # https://github.com/r-lib/vctrs/issues/571
91 vec_is_coercible <- function(x, to, ..., x_arg = "x", to_arg = "to") {
92 try_fetch(
93 vctrs_error_incompatible_type = function(...) FALSE,
94 {
95 vctrs::vec_ptype2(x, to, ..., x_arg = x_arg, y_arg = to_arg)
96 TRUE
97 }
98 )
99 }
100
101 flat_map_int <- function(.x, .fn, ...) {
102 out <- map(unname(.x), .fn, ...)
103 vctrs::vec_c(!!!out, .ptype = int())
104 }
105
106 loc_validate <- function(pos, vars, call = caller_env()) {
107 check_missing(pos, call = call)
108 check_negative(pos, call = call)
109
110 pos <- vctrs::vec_as_subscript(
111 pos,
112 logical = "error",
113 character = "error",
114 call = call
115 )
116 pos <- vctrs::vec_as_location(
117 pos,
118 n = length(vars),
119 call = call
120 )
121
122 named(sel_unique(pos))
123 }
124 check_missing <- function(x, call) {
125 if (anyNA(x)) {
126 abort("Selections can't have missing values.", call = call)
127 }
128 }
129 check_negative <- function(x, call) {
130 if (any(x < 0L)) {
131 abort("Selections can't have negative values.", call = call)
132 }
133 }
134
135 quo_get_expr2 <- function(x, default) {
136 if (is_quosure(x)) {
137 quo_get_expr(x)
138 } else {
139 default
140 }
141 }
142 quo_set_expr2 <- function(x, value, default) {
143 if (is_quosure(x)) {
144 quo_set_expr(x, value)
145 } else {
146 default
147 }
148 }
149
150 # Always returns a fresh non-shared call
151 call_expand_dots <- function(call, env) {
152 if (!is_call(call)) {
153 abort("`call` must be a call.")
154 }
155
156 call <- duplicate(call, shallow = TRUE)
157
158 prev <- call
159 node <- node_cdr(call)
160
161 while (!is_null(node)) {
162 if (is_symbol(node_car(node), "...")) {
163 # Capture dots in a pairlist of quosures
164 dots_mask <- env(env, enquos = enquos)
165 dots <- eval_bare(quote(enquos(...)), dots_mask)
166 dots <- as.pairlist(dots)
167
168 # Splice the dots in the call
169 if (!is_null(dots)) {
170 node_poke_tail(dots, node_cdr(node))
171 }
172 node_poke_cdr(prev, dots)
173
174 break
175 }
176
177 prev <- node
178 node <- node_cdr(node)
179 }
180
181 call
182 }
183
184 node_compact_missing <- function(node) {
185 first <- new_node(NULL, node)
186 prev <- first
187
188 while (!is_null(node)) {
189 car <- node_car(node)
190 cdr <- node_cdr(node)
191
192 if (is_missing(car) || (is_quosure(car) && quo_is_missing(car))) {
193 node_poke_cdr(prev, cdr)
194 } else {
195 prev <- node
196 }
197
198 node <- cdr
199 }
200
201 node_cdr(first)
202 }
203
204 node_tail <- function(node) {
205 rest <- node_cdr(node)
206
207 while (!is_null(rest)) {
208 node <- rest
209 rest <- node_cdr(node)
210 }
211
212 node
213 }
214
215 node_poke_tail <- function(node, new) {
216 node_poke_cdr(node_tail(node), new)
217 }
218
219 named <- function(x) {
220 set_names(x, names2(x))
221 }
222
223 mask_error_call <- function(data_mask) {
224 data_mask$.__tidyselect__.$internal$error_call
225 }
226
227 paste_lines <- function(...) paste(c(...), collapse = "\n")