| 1 |
#' @rdname eval_select |
| 2 |
#' @export |
| 3 |
eval_rename <- function(expr, |
| 4 |
data, |
| 5 |
env = caller_env(), |
| 6 |
..., |
| 7 |
strict = TRUE, |
| 8 |
name_spec = NULL, |
| 9 |
allow_predicates = TRUE, |
| 10 |
error_call = caller_env()) { |
| 11 |
check_dots_empty() |
| 12 |
|
| 13 |
allow_predicates <- allow_predicates && tidyselect_data_has_predicates(data) |
| 14 |
data <- tidyselect_data_proxy(data) |
| 15 |
|
| 16 |
rename_impl( |
| 17 |
data, |
| 18 |
names(data), |
| 19 |
as_quosure(expr, env), |
| 20 |
strict = strict, |
| 21 |
name_spec = name_spec, |
| 22 |
allow_predicates = allow_predicates, |
| 23 |
error_call = error_call |
| 24 |
) |
| 25 |
} |
| 26 |
|
| 27 |
# Caller must put vars in scope |
| 28 |
rename_impl <- function(x, |
| 29 |
names, |
| 30 |
sel, |
| 31 |
strict = TRUE, |
| 32 |
name_spec = NULL, |
| 33 |
allow_predicates = TRUE, |
| 34 |
error_call) { |
| 35 |
if (is_null(names)) { |
| 36 |
cli::cli_abort("Can't rename an unnamed vector.", call = error_call) |
| 37 |
} |
| 38 |
|
| 39 |
pos <- eval_select_impl( |
| 40 |
x, |
| 41 |
names, |
| 42 |
{{ sel }}, |
| 43 |
strict = strict, |
| 44 |
name_spec = name_spec, |
| 45 |
type = "rename", |
| 46 |
allow_predicates = allow_predicates, |
| 47 |
error_call = error_call |
| 48 |
) |
| 49 |
|
| 50 |
# Check for unique names only if input is a data frame |
| 51 |
if (is.data.frame(x) || is_null(x)) { |
| 52 |
names[pos] <- names(pos) |
| 53 |
with_subscript_errors( |
| 54 |
vctrs::vec_as_names( |
| 55 |
names, |
| 56 |
repair = "check_unique", |
| 57 |
call = error_call |
| 58 |
) |
| 59 |
) |
| 60 |
} |
| 61 |
|
| 62 |
pos |
| 63 |
} |
| 64 |
|
| 65 |
# Example implementation mainly used for unit tests |
| 66 |
rename <- function(.x, ..., .strict = TRUE) { |
| 67 |
pos <- eval_rename(expr(c(...)), .x, strict = .strict) |
| 68 |
names(.x)[pos] <- names(pos) |
| 69 |
.x |
| 70 |
} |