File: eval-bool.R

package info (click to toggle)
r-cran-tidyselect 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 616 kB
  • sloc: sh: 13; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download | duplicates (2)
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

eval_bang <- function(expr, data_mask, context_mask) {
  x <- walk_data_tree(expr[[2]], data_mask, context_mask)

  vars <- data_mask$.__tidyselect__.$internal$vars
  error_call <- mask_error_call(data_mask)
  sel_complement(x, vars, error_call = error_call)
}

eval_or <- function(expr, data_mask, context_mask) {
  x <- walk_operand(expr[[2]], data_mask, context_mask)
  y <- walk_operand(expr[[3]], data_mask, context_mask)

  sel_union(x, y)
}

eval_and <- function(expr, data_mask, context_mask) {
  x <- expr[[2]]
  y <- expr[[3]]

  if (is_symbol(x) && is_symbol(y)) {
    x_name <- as_string(x)
    y_name <- as_string(y)

    x <- eval_sym(x, data_mask, context_mask, strict = TRUE)
    y <- eval_sym(y, data_mask, context_mask, strict = TRUE)

    if (!is_function(x) && !is_function(y)) {
      cli::cli_abort(
        c(
          "Can't take the intersection of two columns.",
          # can't use {.code}: https://github.com/r-lib/cli/issues/422
          i = "`{x_name} & {y_name}` is always an empty selection."
        ),
        call = mask_error_call(data_mask)
      )
    }
  }

  x <- walk_operand(x, data_mask, context_mask)
  y <- walk_operand(y, data_mask, context_mask)

  sel_intersect(x, y)
}

walk_operand <- function(expr, data_mask, context_mask) {
  if (is_symbol(expr)) {
    expr <- eval_sym(expr, data_mask, context_mask, strict = TRUE)
  }
  walk_data_tree(expr, data_mask, context_mask)
}

stop_bad_bool_op <- function(bad, ok, call) {
  cli::cli_abort(
    c(
      "Can't use scalar {.code {bad}} in selections.",
      i = "Do you need {.arg {ok}} instead?"
    ),
    call = call
  )
}

stop_bad_arith_op <- function(op, call) {
  cli::cli_abort(
    "Can't use arithmetic operator `{op}` in selection context.",
    call = call
  )
}

stop_formula <- function(expr, call) {
  f <- as_label(expr)
  cli::cli_abort(
    c(
      "Formula shorthand must be wrapped in `where()`.",
      "",
      " " = "  # Bad",
      " " = "  data %>% select({f})",
      "",
      " " = "  # Good",
      " " = "  data %>% select(where({f}))"
    ),
    call = call
  )
}