| 1 |
#' Evaluate an expression to relocate variables |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `eval_relocate()` is a variant of [eval_select()] that moves a selection to |
| 5 |
#' a new location. Either `before` or `after` can be provided to specify where |
| 6 |
#' to move the selection to. This powers `dplyr::relocate()`. |
| 7 |
#' |
| 8 |
#' @inheritParams eval_select |
| 9 |
#' |
| 10 |
#' @param before,after Defused R code describing a selection according to the |
| 11 |
#' tidyselect syntax. The selection represents the destination of the |
| 12 |
#' selection provided through `expr`. Supplying neither of these will move the |
| 13 |
#' selection to the left-hand side. Supplying both of these is an error. |
| 14 |
#' |
| 15 |
#' @param before_arg,after_arg Argument names for `before` and `after`. These |
| 16 |
#' are used in error messages. |
| 17 |
#' |
| 18 |
#' @return |
| 19 |
#' A named vector of numeric locations with length equal to `length(data)`. |
| 20 |
#' Each position in `data` will be represented exactly once. |
| 21 |
#' |
| 22 |
#' The names are normally the same as in the input data, except when the user |
| 23 |
#' supplied named selections with `c()`. In the latter case, the names reflect |
| 24 |
#' the new names chosen by the user. |
| 25 |
#' |
| 26 |
#' @export |
| 27 |
#' @examples |
| 28 |
#' library(rlang) |
| 29 |
#' |
| 30 |
#' # Interpret defused code as a request to relocate |
| 31 |
#' x <- expr(c(mpg, disp)) |
| 32 |
#' after <- expr(wt) |
| 33 |
#' eval_relocate(x, mtcars, after = after) |
| 34 |
#' |
| 35 |
#' # Supplying neither `before` nor `after` will move the selection to the |
| 36 |
#' # left-hand side |
| 37 |
#' eval_relocate(x, mtcars) |
| 38 |
#' |
| 39 |
#' # Within a function, use `enquo()` to defuse a single argument. |
| 40 |
#' # Note that `before` and `after` must also be defused with `enquo()`. |
| 41 |
#' my_relocator <- function(x, expr, before = NULL, after = NULL) { |
| 42 |
#' eval_relocate(enquo(expr), x, before = enquo(before), after = enquo(after)) |
| 43 |
#' } |
| 44 |
#' |
| 45 |
#' my_relocator(mtcars, vs, before = hp) |
| 46 |
#' |
| 47 |
#' # Here is an example of using `eval_relocate()` to implement `relocate()`. |
| 48 |
#' # Note that the dots are passed on as a defused call to `c(...)`. |
| 49 |
#' relocate <- function(.x, ..., .before = NULL, .after = NULL) { |
| 50 |
#' pos <- eval_relocate( |
| 51 |
#' expr(c(...)), |
| 52 |
#' .x, |
| 53 |
#' before = enquo(.before), |
| 54 |
#' after = enquo(.after) |
| 55 |
#' ) |
| 56 |
#' set_names(.x[pos], names(pos)) |
| 57 |
#' } |
| 58 |
#' |
| 59 |
#' relocate(mtcars, vs, .before = hp) |
| 60 |
#' relocate(mtcars, starts_with("d"), .after = last_col()) |
| 61 |
eval_relocate <- function(expr, |
| 62 |
data, |
| 63 |
..., |
| 64 |
before = NULL, |
| 65 |
after = NULL, |
| 66 |
strict = TRUE, |
| 67 |
name_spec = NULL, |
| 68 |
allow_rename = TRUE, |
| 69 |
allow_empty = TRUE, |
| 70 |
allow_predicates = TRUE, |
| 71 |
before_arg = "before", |
| 72 |
after_arg = "after", |
| 73 |
env = caller_env(), |
| 74 |
error_call = caller_env()) { |
| 75 |
check_dots_empty() |
| 76 |
|
| 77 |
allow_predicates <- allow_predicates && tidyselect_data_has_predicates(data) |
| 78 |
data <- tidyselect_data_proxy(data) |
| 79 |
|
| 80 |
expr <- as_quosure(expr, env = env) |
| 81 |
|
| 82 |
sel <- eval_select_impl( |
| 83 |
x = data, |
| 84 |
names = names(data), |
| 85 |
expr = expr, |
| 86 |
strict = strict, |
| 87 |
name_spec = name_spec, |
| 88 |
allow_rename = allow_rename, |
| 89 |
allow_empty = allow_empty, |
| 90 |
allow_predicates = allow_predicates, |
| 91 |
type = "relocate", |
| 92 |
error_call = error_call |
| 93 |
) |
| 94 |
|
| 95 |
# Enforce the invariant that relocating can't change the number of columns by |
| 96 |
# retaining only the last instance of a column that is renamed multiple times |
| 97 |
# TODO: https://github.com/r-lib/vctrs/issues/1442 |
| 98 |
# `sel <- vctrs::vec_unique(sel, which = "last")` |
| 99 |
loc_last <- which(!duplicated(sel, fromLast = TRUE)) |
| 100 |
sel <- vctrs::vec_slice(sel, loc_last) |
| 101 |
|
| 102 |
n <- length(data) |
| 103 |
|
| 104 |
before <- as_quosure(before, env = env) |
| 105 |
after <- as_quosure(after, env = env) |
| 106 |
|
| 107 |
has_before <- !quo_is_null(before) |
| 108 |
has_after <- !quo_is_null(after) |
| 109 |
|
| 110 |
if (has_before && has_after) { |
| 111 |
cli::cli_abort( |
| 112 |
"Can't supply both {.arg {before_arg}} and {.arg {after_arg}}.", |
| 113 |
call = error_call |
| 114 |
) |
| 115 |
} |
| 116 |
|
| 117 |
if (has_before) { |
| 118 |
where <- with_rename_errors( |
| 119 |
eval_select( |
| 120 |
expr = before, |
| 121 |
data = data, |
| 122 |
env = env, |
| 123 |
error_call = error_call, |
| 124 |
allow_predicates = allow_predicates, |
| 125 |
allow_rename = FALSE |
| 126 |
), |
| 127 |
arg = before_arg, |
| 128 |
error_call = error_call |
| 129 |
) |
| 130 |
where <- unname(where) |
| 131 |
|
| 132 |
if (length(where) == 0L) { |
| 133 |
# Empty `before` selection pushes `sel` to the front |
| 134 |
where <- 1L |
| 135 |
} else { |
| 136 |
where <- min(where) |
| 137 |
} |
| 138 |
} else if (has_after) { |
| 139 |
where <- with_rename_errors( |
| 140 |
eval_select( |
| 141 |
expr = after, |
| 142 |
data = data, |
| 143 |
env = env, |
| 144 |
error_call = error_call, |
| 145 |
allow_predicates = allow_predicates, |
| 146 |
allow_rename = FALSE |
| 147 |
), |
| 148 |
arg = after_arg, |
| 149 |
error_call = error_call |
| 150 |
) |
| 151 |
where <- unname(where) |
| 152 |
|
| 153 |
if (length(where) == 0L) { |
| 154 |
# Empty `after` selection pushes `sel` to the back |
| 155 |
where <- n |
| 156 |
} else { |
| 157 |
where <- max(where) |
| 158 |
} |
| 159 |
|
| 160 |
where <- where + 1L |
| 161 |
} else { |
| 162 |
# Defaults to `before = everything()` if neither |
| 163 |
# `before` nor `after` are supplied |
| 164 |
where <- 1L |
| 165 |
} |
| 166 |
|
| 167 |
lhs <- seq2(1L, where - 1L) |
| 168 |
rhs <- seq2(where, n) |
| 169 |
|
| 170 |
lhs <- setdiff(lhs, sel) |
| 171 |
rhs <- setdiff(rhs, sel) |
| 172 |
|
| 173 |
names <- names(data) |
| 174 |
|
| 175 |
names(lhs) <- names[lhs] |
| 176 |
names(rhs) <- names[rhs] |
| 177 |
|
| 178 |
sel <- vctrs::vec_c(lhs, sel, rhs) |
| 179 |
|
| 180 |
sel |
| 181 |
} |
| 182 |
|
| 183 |
with_rename_errors <- function(expr, arg, error_call) { |
| 184 |
withCallingHandlers( |
| 185 |
expr, |
| 186 |
`tidyselect:::error_disallowed_rename` = function(cnd) { |
| 187 |
cli::cli_abort( |
| 188 |
"Can't rename variables when {.arg {arg}} is supplied.", |
| 189 |
call = error_call |
| 190 |
) |
| 191 |
} |
| 192 |
) |
| 193 |
} |