| 1 |
vars_select_eval <- function(vars, |
| 2 |
expr, |
| 3 |
strict, |
| 4 |
data = NULL, |
| 5 |
name_spec = NULL, |
| 6 |
uniquely_named = NULL, |
| 7 |
allow_rename = TRUE, |
| 8 |
allow_empty = TRUE, |
| 9 |
allow_predicates = TRUE, |
| 10 |
type = "select", |
| 11 |
error_call) { |
| 12 |
wrapped <- quo_get_expr2(expr, expr) |
| 13 |
|
| 14 |
if (is_missing(wrapped)) { |
| 15 |
pos <- named(int()) |
| 16 |
check_empty(pos, allow_empty, call = error_call) |
| 17 |
return(pos) |
| 18 |
} |
| 19 |
|
| 20 |
uniquely_named <- uniquely_named %||% is.data.frame(data) |
| 21 |
|
| 22 |
if (!is_symbolic(wrapped)) { |
| 23 |
pos <- as_indices_sel_impl( |
| 24 |
wrapped, |
| 25 |
vars = vars, |
| 26 |
strict = strict, |
| 27 |
data = data, |
| 28 |
allow_predicates = allow_predicates, |
| 29 |
call = error_call |
| 30 |
) |
| 31 |
pos <- loc_validate(pos, vars) |
| 32 |
pos <- ensure_named( |
| 33 |
pos, |
| 34 |
vars, |
| 35 |
uniquely_named = uniquely_named, |
| 36 |
allow_rename = allow_rename, |
| 37 |
allow_empty = allow_empty, |
| 38 |
call = error_call |
| 39 |
) |
| 40 |
return(pos) |
| 41 |
} |
| 42 |
|
| 43 |
vars <- peek_vars() |
| 44 |
|
| 45 |
vars_split <- vctrs::vec_split(seq_along(vars), vars) |
| 46 |
|
| 47 |
# Mark data duplicates so we can fail instead of disambiguating them |
| 48 |
# when renaming |
| 49 |
if (uniquely_named) { |
| 50 |
vars_split$val <- map(vars_split$val, mark_data_dups) |
| 51 |
} |
| 52 |
|
| 53 |
# We are intentionally lenient towards partially named inputs |
| 54 |
vars_split <- vctrs::vec_slice(vars_split, !are_empty_name(vars_split$key)) |
| 55 |
|
| 56 |
top <- env() |
| 57 |
bottom <- env(top, !!!set_names(vars_split$val, vars_split$key)) |
| 58 |
data_mask <- new_data_mask(bottom, top) |
| 59 |
data_mask$.data <- as_data_pronoun(data_mask) |
| 60 |
|
| 61 |
context_mask <- new_data_mask(env(!!!vars_select_helpers)) |
| 62 |
|
| 63 |
if (is_null(name_spec)) { |
| 64 |
if (inherits(data, "data.frame")) { |
| 65 |
name_spec <- unique_name_spec |
| 66 |
} else { |
| 67 |
name_spec <- minimal_name_spec |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
# Save metadata in mask |
| 72 |
internal <- list( |
| 73 |
vars = vars, |
| 74 |
data = data, |
| 75 |
strict = strict, |
| 76 |
name_spec = name_spec, |
| 77 |
uniquely_named = uniquely_named, |
| 78 |
allow_predicates = allow_predicates, |
| 79 |
error_call = error_call |
| 80 |
) |
| 81 |
data_mask$.__tidyselect__.$internal <- internal |
| 82 |
|
| 83 |
pos <- walk_data_tree(expr, data_mask, context_mask) |
| 84 |
pos <- loc_validate(pos, vars, call = error_call) |
| 85 |
|
| 86 |
if (type == "rename" && !is_named(pos)) { |
| 87 |
cli::cli_abort("All renaming inputs must be named.", call = error_call) |
| 88 |
} |
| 89 |
|
| 90 |
ensure_named( |
| 91 |
pos, |
| 92 |
vars, |
| 93 |
uniquely_named = uniquely_named, |
| 94 |
allow_rename = allow_rename, |
| 95 |
allow_empty = allow_empty, |
| 96 |
call = error_call |
| 97 |
) |
| 98 |
} |
| 99 |
|
| 100 |
ensure_named <- function(pos, |
| 101 |
vars, |
| 102 |
uniquely_named = FALSE, |
| 103 |
allow_rename = TRUE, |
| 104 |
allow_empty = TRUE, |
| 105 |
call = caller_env()) { |
| 106 |
check_empty(pos, allow_empty, call = call) |
| 107 |
|
| 108 |
if (!allow_rename && any(names2(pos) != "")) { |
| 109 |
cli::cli_abort( |
| 110 |
"Can't rename variables in this context.", |
| 111 |
class = "tidyselect:::error_disallowed_rename", |
| 112 |
call = call |
| 113 |
) |
| 114 |
} |
| 115 |
|
| 116 |
nms <- names(pos) <- names2(pos) |
| 117 |
nms_missing <- nms == "" |
| 118 |
names(pos)[nms_missing] <- vars[pos[nms_missing]] |
| 119 |
|
| 120 |
# Duplicates are not allowed for data frames |
| 121 |
if (uniquely_named) { |
| 122 |
vctrs::vec_as_names(names(pos), repair = "check_unique", call = call) |
| 123 |
} |
| 124 |
|
| 125 |
pos |
| 126 |
} |
| 127 |
|
| 128 |
check_empty <- function(x, allow_empty = TRUE, call = caller_env()) { |
| 129 |
if (!allow_empty && length(x) == 0) { |
| 130 |
cli::cli_abort("Must select at least one item.", call = call) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
# `walk_data_tree()` is a recursive interpreter that implements a |
| 135 |
# clear separation between data expressions (calls to `-`, `:`, `c`, |
| 136 |
# and `(`) and context expressions (selection helpers and any other |
| 137 |
# calls). It recursively traverses the AST across data expressions. |
| 138 |
# The leaves of the data expression tree are either symbols (evaluated |
| 139 |
# with `eval_sym()`) or context expressions (evaluated with |
| 140 |
# `eval_context()`). |
| 141 |
|
| 142 |
walk_data_tree <- function(expr, data_mask, context_mask, colon = FALSE) { |
| 143 |
# Unwrap quosures to make it easier to inspect expressions. We save |
| 144 |
# a reference to the current quosure environment in the context |
| 145 |
# mask, so we can evaluate the expression in the correct context |
| 146 |
# later on. |
| 147 |
if (is_quosure(expr)) { |
| 148 |
local_bindings(.__current__. = quo_get_env(expr), .env = context_mask) |
| 149 |
expr <- quo_get_expr2(expr, expr) |
| 150 |
} |
| 151 |
|
| 152 |
error_call <- data_mask$.__tidyselect__.$internal$error_call |
| 153 |
|
| 154 |
out <- switch( |
| 155 |
expr_kind(expr, context_mask, error_call), |
| 156 |
literal = expr, |
| 157 |
symbol = eval_sym(expr, data_mask, context_mask), |
| 158 |
`(` = walk_data_tree(expr[[2]], data_mask, context_mask, colon = colon), |
| 159 |
`!` = eval_bang(expr, data_mask, context_mask), |
| 160 |
`-` = eval_minus(expr, data_mask, context_mask, error_call), |
| 161 |
`:` = eval_colon(expr, data_mask, context_mask), |
| 162 |
`|` = eval_or(expr, data_mask, context_mask), |
| 163 |
`&` = eval_and(expr, data_mask, context_mask), |
| 164 |
`c` = eval_c(expr, data_mask, context_mask), |
| 165 |
`||` = stop_bad_bool_op("||", "|", call = error_call), |
| 166 |
`&&` = stop_bad_bool_op("&&", "&", call = error_call), |
| 167 |
`*` = stop_bad_arith_op("*", call = error_call), |
| 168 |
`/` = eval_slash(expr, data_mask, context_mask), |
| 169 |
`^` = stop_bad_arith_op("^", call = error_call), |
| 170 |
`~` = stop_formula(expr, call = error_call), |
| 171 |
.data = eval(expr, data_mask), |
| 172 |
eval_context(expr, context_mask, call = error_call) |
| 173 |
) |
| 174 |
|
| 175 |
vars <- data_mask$.__tidyselect__.$internal$vars |
| 176 |
strict <- data_mask$.__tidyselect__.$internal$strict |
| 177 |
data <- data_mask$.__tidyselect__.$internal$data |
| 178 |
allow_predicates <- data_mask$.__tidyselect__.$internal$allow_predicates |
| 179 |
|
| 180 |
as_indices_sel_impl( |
| 181 |
out, |
| 182 |
vars = vars, |
| 183 |
strict = strict, |
| 184 |
data = data, |
| 185 |
allow_predicates = allow_predicates, |
| 186 |
call = error_call, |
| 187 |
arg = as_label(expr) |
| 188 |
) |
| 189 |
} |
| 190 |
|
| 191 |
as_indices_sel_impl <- function(x, |
| 192 |
vars, |
| 193 |
strict, |
| 194 |
data = NULL, |
| 195 |
allow_predicates = TRUE, |
| 196 |
call, |
| 197 |
arg = NULL) { |
| 198 |
if (is.function(x)) { |
| 199 |
if (!allow_predicates) { |
| 200 |
cli::cli_abort( |
| 201 |
"This tidyselect interface doesn't support predicates.", |
| 202 |
call = call, |
| 203 |
class = "tidyselect_error_predicates_unsupported" |
| 204 |
) |
| 205 |
} |
| 206 |
if (is_null(data)) { |
| 207 |
cli::cli_abort( |
| 208 |
c( |
| 209 |
"This tidyselect interface doesn't support predicates yet.", |
| 210 |
i = "Contact the package author and suggest using {.code eval_select()}." |
| 211 |
), |
| 212 |
call = call |
| 213 |
) |
| 214 |
} |
| 215 |
predicate <- x |
| 216 |
|
| 217 |
xs <- map(data, predicate) |
| 218 |
for (i in seq_along(xs)) { |
| 219 |
check_predicate_output(xs[[i]], call = call) |
| 220 |
} |
| 221 |
|
| 222 |
x <- which(as.logical(xs)) |
| 223 |
} |
| 224 |
|
| 225 |
as_indices_impl(x, vars, call = call, arg = arg, strict = strict) |
| 226 |
} |
| 227 |
|
| 228 |
check_predicate_output <- function(x, call) { |
| 229 |
if (!is_bool(x)) { |
| 230 |
cli::cli_abort( |
| 231 |
"Predicate must return `TRUE` or `FALSE`, not {obj_type_friendly(x)}.", |
| 232 |
call = call |
| 233 |
) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
as_indices_impl <- function(x, vars, strict, call = caller_env(), arg = NULL) { |
| 238 |
if (is_null(x)) { |
| 239 |
return(int()) |
| 240 |
} |
| 241 |
|
| 242 |
x <- vctrs::vec_as_subscript( |
| 243 |
x, |
| 244 |
logical = "error", |
| 245 |
call = call, |
| 246 |
arg = arg |
| 247 |
) |
| 248 |
|
| 249 |
if (!strict) { |
| 250 |
# Remove out-of-bounds elements if non-strict. We do this eagerly |
| 251 |
# because names vectors must be converted to locations here. |
| 252 |
x <- switch(typeof(x), |
| 253 |
character = vctrs::vec_set_intersect(x, c(vars, na_chr)), |
| 254 |
double = , |
| 255 |
integer = x[x <= length(vars)], |
| 256 |
x |
| 257 |
) |
| 258 |
} |
| 259 |
|
| 260 |
switch( |
| 261 |
typeof(x), |
| 262 |
character = chr_as_locations(x, vars, call = call, arg = arg), |
| 263 |
double = , |
| 264 |
integer = x, |
| 265 |
cli::cli_abort("Unexpected type.", .internal = TRUE) |
| 266 |
) |
| 267 |
} |
| 268 |
|
| 269 |
chr_as_locations <- function(x, vars, call = caller_env(), arg = NULL) { |
| 270 |
out <- vctrs::vec_as_location( |
| 271 |
x, |
| 272 |
n = length(vars), |
| 273 |
names = vars, |
| 274 |
call = call, |
| 275 |
arg = arg |
| 276 |
) |
| 277 |
set_names(out, names(x)) |
| 278 |
} |
| 279 |
|
| 280 |
as_indices <- function(x, vars, strict = TRUE, call) { |
| 281 |
inds <- with_subscript_errors(as_indices_impl(x, vars, strict, call)) |
| 282 |
vctrs::vec_as_location(inds, length(vars), vars, convert_values = NULL) |
| 283 |
} |
| 284 |
|
| 285 |
expr_kind <- function(expr, context_mask, error_call) { |
| 286 |
switch( |
| 287 |
typeof(expr), |
| 288 |
symbol = "symbol", |
| 289 |
language = call_kind(expr, context_mask, error_call), |
| 290 |
"literal" |
| 291 |
) |
| 292 |
} |
| 293 |
call_kind <- function(expr, context_mask, error_call) { |
| 294 |
head <- node_car(expr) |
| 295 |
if (!is_symbol(head)) { |
| 296 |
return("call") |
| 297 |
} |
| 298 |
|
| 299 |
env <- context_mask$.__current__. |
| 300 |
|
| 301 |
fn <- as_string(head) |
| 302 |
|
| 303 |
if (fn %in% c("$", "[[") && identical(expr[[2]], quote(.data))) { |
| 304 |
validate_dot_data(expr, error_call) |
| 305 |
|
| 306 |
what <- I("Use of .data in tidyselect expressions") |
| 307 |
if (fn == "$") { |
| 308 |
var <- as_string(expr[[3]]) |
| 309 |
str <- encodeString(var, quote = '"') |
| 310 |
|
| 311 |
lifecycle::deprecate_soft("1.2.0", what, |
| 312 |
details = cli::format_inline("Please use {.code {str}} instead of `.data${var}`"), |
| 313 |
user_env = env |
| 314 |
) |
| 315 |
} else if (fn == "[[") { |
| 316 |
# .data[[ is an injection operator so can't give specific advice |
| 317 |
lifecycle::deprecate_soft("1.2.0", what, |
| 318 |
details = cli::format_inline("Please use {.code all_of(var)} (or {.code any_of(var)}) instead of {.code .data[[var]]}"), |
| 319 |
user_env = env |
| 320 |
) |
| 321 |
} |
| 322 |
|
| 323 |
return(".data") |
| 324 |
} |
| 325 |
|
| 326 |
switch(fn, |
| 327 |
`(` = , |
| 328 |
`-` = , |
| 329 |
`:` = , |
| 330 |
`|` = , |
| 331 |
`&` = , |
| 332 |
`||` = , |
| 333 |
`&&` = , |
| 334 |
`!` = , |
| 335 |
`+` = , |
| 336 |
`*` = , |
| 337 |
`/` = , |
| 338 |
`^` = , |
| 339 |
`~` = , |
| 340 |
`c` = fn, |
| 341 |
"call" |
| 342 |
) |
| 343 |
} |
| 344 |
|
| 345 |
eval_colon <- function(expr, data_mask, context_mask) { |
| 346 |
if (is_negated_colon(expr)) { |
| 347 |
# Compatibility syntax for `-1:-2`. We interpret it as `-(1:2)`. |
| 348 |
out <- eval_colon(unnegate_colon(expr), data_mask, context_mask) |
| 349 |
vars <- data_mask$.__tidyselect__.$internal$vars |
| 350 |
error_call <- mask_error_call(data_mask) |
| 351 |
sel_complement(out, vars, error_call = error_call) |
| 352 |
} else { |
| 353 |
x <- walk_data_tree(expr[[2]], data_mask, context_mask, colon = TRUE) |
| 354 |
y <- walk_data_tree(expr[[3]], data_mask, context_mask, colon = TRUE) |
| 355 |
x:y |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
eval_minus <- function(expr, data_mask, context_mask, call = call) { |
| 360 |
if (length(expr) == 2) { |
| 361 |
eval_bang(expr, data_mask, context_mask) |
| 362 |
} else { |
| 363 |
eval_context(expr, context_mask, call = call) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
eval_slash <- function(expr, data_mask, context_mask) { |
| 368 |
lhs <- walk_data_tree(expr[[2]], data_mask, context_mask) |
| 369 |
rhs <- walk_data_tree(expr[[3]], data_mask, context_mask) |
| 370 |
|
| 371 |
vars <- data_mask$.__tidyselect__.$internal$vars |
| 372 |
error_call <- mask_error_call(data_mask) |
| 373 |
|
| 374 |
sel_diff(lhs, rhs, vars, error_call = error_call) |
| 375 |
} |
| 376 |
|
| 377 |
eval_context <- function(expr, context_mask, call) { |
| 378 |
env <- context_mask$.__current__. %||% base_env() |
| 379 |
with_chained_errors( |
| 380 |
eval_tidy(as_quosure(expr, env), context_mask), |
| 381 |
call = call, |
| 382 |
eval_expr = expr |
| 383 |
) |
| 384 |
} |
| 385 |
|
| 386 |
eval_sym <- function(expr, data_mask, context_mask, strict = FALSE) { |
| 387 |
name <- as_string(expr) |
| 388 |
|
| 389 |
top <- data_mask$.top_env |
| 390 |
cur <- data_mask |
| 391 |
value <- missing_arg() |
| 392 |
while (!is_reference(cur, top)) { |
| 393 |
if (env_has(cur, name)) { |
| 394 |
# TODO: Remove unnecessary `default` specification after |
| 395 |
# https://github.com/r-lib/rlang/issues/1582 |
| 396 |
value <- env_get(cur, name, default = NULL) |
| 397 |
break |
| 398 |
} |
| 399 |
cur <- env_parent(cur) |
| 400 |
} |
| 401 |
|
| 402 |
if (!missing(value)) { |
| 403 |
return(value) |
| 404 |
} |
| 405 |
|
| 406 |
env <- context_mask$.__current__. |
| 407 |
value <- env_get(env, name, default = missing_arg(), inherit = TRUE) |
| 408 |
|
| 409 |
# Cause OOB error |
| 410 |
if (is_missing(value)) { |
| 411 |
return(name) |
| 412 |
} |
| 413 |
|
| 414 |
# Predicate functions must now be wrapped in `where()`. We'll |
| 415 |
# support functions starting with `is` for compatibility for |
| 416 |
# compatibility. |
| 417 |
if (is_function(value)) { |
| 418 |
if (!grepl("^is", name)) { |
| 419 |
return(name) |
| 420 |
} |
| 421 |
|
| 422 |
# Formally deprecated in 1.2.0 |
| 423 |
lifecycle::deprecate_soft("1.1.0", |
| 424 |
what = I("Use of bare predicate functions"), |
| 425 |
with = I("wrap predicates in `where()`"), |
| 426 |
details = c( |
| 427 |
" " = "# Was:", |
| 428 |
" " = glue("data %>% select({name})"), |
| 429 |
"", |
| 430 |
" " = "# Now:", |
| 431 |
" " = glue("data %>% select(where({name}))") |
| 432 |
), |
| 433 |
user_env = env |
| 434 |
) |
| 435 |
|
| 436 |
return(value) |
| 437 |
} |
| 438 |
|
| 439 |
# Cause OOB error |
| 440 |
if (strict) { |
| 441 |
return(name) |
| 442 |
} |
| 443 |
|
| 444 |
# Formally deprecated in 1.2.0 |
| 445 |
lifecycle::deprecate_soft("1.1.0", |
| 446 |
I("Using an external vector in selections"), |
| 447 |
I("`all_of()` or `any_of()`"), |
| 448 |
details = c( |
| 449 |
" " = "# Was:", |
| 450 |
" " = glue("data %>% select({name})"), |
| 451 |
"", |
| 452 |
" " = "# Now:", |
| 453 |
" " = glue("data %>% select(all_of({name}))"), |
| 454 |
"", |
| 455 |
"See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>." |
| 456 |
), |
| 457 |
user_env = env |
| 458 |
) |
| 459 |
|
| 460 |
value |
| 461 |
} |
| 462 |
|
| 463 |
validate_dot_data <- function(expr, call) { |
| 464 |
if (is_call(expr, "$") && !is_symbol(expr[[3]])) { |
| 465 |
cli::cli_abort( |
| 466 |
"The RHS of {.code .data$rhs} must be a symbol.", |
| 467 |
call = call |
| 468 |
) |
| 469 |
} |
| 470 |
if (is_call(expr, "[[") && is_symbolic(expr[[3]])) { |
| 471 |
cli::cli_abort( |
| 472 |
"The subscript of {.code .data[[subscript]]} must be a constant.", |
| 473 |
call = call |
| 474 |
) |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
mark_data_dups <- function(x) { |
| 479 |
if (length(x) > 1L) { |
| 480 |
structure(x, tidyselect_data_dups = TRUE) |
| 481 |
} else { |
| 482 |
x |
| 483 |
} |
| 484 |
} |
| 485 |
is_data_dups <- function(x) { |
| 486 |
is_true(attr(x, "tidyselect_data_dups")) |
| 487 |
} |