| 1 |
|
| 2 |
eval_c <- function(expr, data_mask, context_mask) { |
| 3 |
expr <- call_expand_dots(expr, context_mask$.__current__.) |
| 4 |
expr <- node_compact_missing(expr) |
| 5 |
node <- node_cdr(expr) |
| 6 |
|
| 7 |
# If the first selector is exclusive (negative), start with all |
| 8 |
# columns. `-foo` is syntax for `everything() - foo`. |
| 9 |
if (c_arg_kind(node_car(node)) %in% c("diff", "diff_colon")) { |
| 10 |
init <- quote(everything()) |
| 11 |
} else { |
| 12 |
init <- named(int()) |
| 13 |
} |
| 14 |
|
| 15 |
reduce_sels(node, data_mask, context_mask, init = init) |
| 16 |
} |
| 17 |
|
| 18 |
reduce_sels <- function(node, data_mask, context_mask, init) { |
| 19 |
out <- walk_data_tree(init, data_mask, context_mask) |
| 20 |
|
| 21 |
while (!is_null(node)) { |
| 22 |
tag <- node_tag(node) |
| 23 |
car <- node_car(node) |
| 24 |
cdr <- node_cdr(node) |
| 25 |
|
| 26 |
kind <- c_arg_kind(car) |
| 27 |
new <- switch(kind, |
| 28 |
diff = unnegate(car), |
| 29 |
diff_colon = unnegate_colon(car), |
| 30 |
car |
| 31 |
) |
| 32 |
|
| 33 |
new <- walk_data_tree(new, data_mask, context_mask) |
| 34 |
if (!is_null(tag)) { |
| 35 |
internal <- data_mask$.__tidyselect__.$internal |
| 36 |
new <- combine_names( |
| 37 |
new, |
| 38 |
tag, |
| 39 |
internal$name_spec, |
| 40 |
internal$strict, |
| 41 |
error_call = internal$error_call |
| 42 |
) |
| 43 |
} |
| 44 |
|
| 45 |
if (kind == "union") { |
| 46 |
out <- sel_union(out, new) |
| 47 |
} else { |
| 48 |
vars <- data_mask$.__tidyselect__.$internal$vars |
| 49 |
error_call <- mask_error_call(data_mask) |
| 50 |
out <- sel_diff(out, new, vars, error_call = error_call) |
| 51 |
} |
| 52 |
|
| 53 |
node <- cdr |
| 54 |
} |
| 55 |
|
| 56 |
out |
| 57 |
} |
| 58 |
|
| 59 |
c_arg_kind <- function(x) { |
| 60 |
expr <- quo_get_expr2(x, x) |
| 61 |
|
| 62 |
if (is_negated(x)) { |
| 63 |
"diff" |
| 64 |
} else if (is_negated_colon(x)) { |
| 65 |
"diff_colon" |
| 66 |
} else { |
| 67 |
"union" |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
unnegate <- function(x) { |
| 72 |
expr <- quo_get_expr2(x, x) |
| 73 |
expr <- node_cadr(expr) |
| 74 |
|
| 75 |
if (is_quosure(expr)) { |
| 76 |
expr |
| 77 |
} else if (is_quosure(x)) { |
| 78 |
quo_set_expr(x, expr) |
| 79 |
} else { |
| 80 |
expr |
| 81 |
} |
| 82 |
} |
| 83 |
unnegate_colon <- function(x) { |
| 84 |
expr <- quo_get_expr2(x, x) |
| 85 |
|
| 86 |
expr[[2]] <- unnegate(expr[[2]]) |
| 87 |
expr[[3]] <- unnegate(expr[[3]]) |
| 88 |
|
| 89 |
quo_set_expr2(x, expr, expr) |
| 90 |
} |
| 91 |
|
| 92 |
is_negated <- function(x) { |
| 93 |
expr <- quo_get_expr2(x, x) |
| 94 |
is_call(expr, "-", n = 1) |
| 95 |
} |
| 96 |
is_negated_colon <- function(x) { |
| 97 |
expr <- quo_get_expr2(x, x) |
| 98 |
is_call(expr, ":") && is_negated(expr[[2]]) && is_negated(expr[[3]]) |
| 99 |
} |
| 100 |
|
| 101 |
combine_names <- function(x, |
| 102 |
tag, |
| 103 |
name_spec, |
| 104 |
uniquely_named, |
| 105 |
error_call) { |
| 106 |
if (uniquely_named && is_data_dups(x)) { |
| 107 |
name <- as_string(tag) |
| 108 |
cli::cli_abort("Can't rename duplicate variables to `{name}`.", call = error_call) |
| 109 |
} |
| 110 |
|
| 111 |
vctrs::vec_c(!!tag := x, .name_spec = name_spec) |
| 112 |
} |
| 113 |
unique_name_spec <- function(outer, inner) { |
| 114 |
# For compatibily, we enumerate as "foo1", "foo2", rather than |
| 115 |
# "foo...1", "foo...2" |
| 116 |
sep <- if (is_character(inner)) "..." else "" |
| 117 |
paste(outer, inner, sep = sep) |
| 118 |
} |
| 119 |
minimal_name_spec <- function(outer, inner) { |
| 120 |
if (is_character(inner)) { |
| 121 |
paste(outer, inner, sep = "...") |
| 122 |
} else { |
| 123 |
outer |
| 124 |
} |
| 125 |
} |