Debian Codegraph @

R/eval-bool.R

raw
1
2 eval_bang <- function(expr, data_mask, context_mask) {
3 x <- walk_data_tree(expr[[2]], data_mask, context_mask)
4
5 vars <- data_mask$.__tidyselect__.$internal$vars
6 error_call <- mask_error_call(data_mask)
7 sel_complement(x, vars, error_call = error_call)
8 }
9
10 eval_or <- function(expr, data_mask, context_mask) {
11 x <- walk_operand(expr[[2]], data_mask, context_mask)
12 y <- walk_operand(expr[[3]], data_mask, context_mask)
13
14 sel_union(x, y)
15 }
16
17 eval_and <- function(expr, data_mask, context_mask) {
18 x <- expr[[2]]
19 y <- expr[[3]]
20
21 if (is_symbol(x) && is_symbol(y)) {
22 x_name <- as_string(x)
23 y_name <- as_string(y)
24
25 x <- eval_sym(x, data_mask, context_mask, strict = TRUE)
26 y <- eval_sym(y, data_mask, context_mask, strict = TRUE)
27
28 if (!is_function(x) && !is_function(y)) {
29 cli::cli_abort(
30 c(
31 "Can't take the intersection of two columns.",
32 # can't use {.code}: https://github.com/r-lib/cli/issues/422
33 i = "`{x_name} & {y_name}` is always an empty selection."
34 ),
35 call = mask_error_call(data_mask)
36 )
37 }
38 }
39
40 x <- walk_operand(x, data_mask, context_mask)
41 y <- walk_operand(y, data_mask, context_mask)
42
43 sel_intersect(x, y)
44 }
45
46 walk_operand <- function(expr, data_mask, context_mask) {
47 if (is_symbol(expr)) {
48 expr <- eval_sym(expr, data_mask, context_mask, strict = TRUE)
49 }
50 walk_data_tree(expr, data_mask, context_mask)
51 }
52
53 stop_bad_bool_op <- function(bad, ok, call) {
54 cli::cli_abort(
55 c(
56 "Can't use scalar {.code {bad}} in selections.",
57 i = "Do you need {.arg {ok}} instead?"
58 ),
59 call = call
60 )
61 }
62
63 stop_bad_arith_op <- function(op, call) {
64 cli::cli_abort(
65 "Can't use arithmetic operator `{op}` in selection context.",
66 call = call
67 )
68 }
69
70 stop_formula <- function(expr, call) {
71 f <- as_label(expr)
72 cli::cli_abort(
73 c(
74 "Formula shorthand must be wrapped in `where()`.",
75 "",
76 " " = " # Bad",
77 " " = " data %>% select({f})",
78 "",
79 " " = " # Good",
80 " " = " data %>% select(where({f}))"
81 ),
82 call = call
83 )
84 }