| 1 |
#' Select variable |
| 2 |
#' |
| 3 |
#' This function powers [dplyr::pull()] and various functions of the |
| 4 |
#' tidyr package. It is similar to [vars_select()] but returns only |
| 5 |
#' one column name and has slightly different semantics: it allows |
| 6 |
#' negative numbers to select columns from the end. |
| 7 |
#' |
| 8 |
#' @inheritParams vars_select |
| 9 |
#' @inheritParams rlang::args_error_context |
| 10 |
#' @param var A variable specified as: |
| 11 |
#' * a literal variable name |
| 12 |
#' * a positive integer, giving the position counting from the left |
| 13 |
#' * a negative integer, giving the position counting from the right. |
| 14 |
#' |
| 15 |
#' The default returns the last column (on the assumption that's the |
| 16 |
#' column you've created most recently). |
| 17 |
#' |
| 18 |
#' This argument is taken by expression and supports |
| 19 |
#' [quasiquotation][rlang::quasiquotation] (you can unquote column |
| 20 |
#' names and column locations). |
| 21 |
#' @return The selected column name as an unnamed string. |
| 22 |
#' @seealso [dplyr::pull()], [vars_select()] |
| 23 |
#' @export |
| 24 |
#' @keywords internal |
| 25 |
#' @examples |
| 26 |
#' # It takes its argument by expression: |
| 27 |
#' vars_pull(letters, c) |
| 28 |
#' |
| 29 |
#' # Negative numbers select from the end: |
| 30 |
#' vars_pull(letters, -3) |
| 31 |
#' |
| 32 |
#' # You can unquote variables: |
| 33 |
#' var <- 10 |
| 34 |
#' vars_pull(letters, !!var) |
| 35 |
vars_pull <- function(vars, var = -1, error_call = caller_env(), error_arg = caller_arg(var)) { |
| 36 |
expr <- enquo(var) |
| 37 |
if (quo_is_missing(expr)) { |
| 38 |
# No easy way to determine what var is in parent because it's likely |
| 39 |
# to be embraced; so don't try and use error_arg here |
| 40 |
cli::cli_abort( |
| 41 |
"{.arg var} is absent but must be supplied.", |
| 42 |
call = error_call |
| 43 |
) |
| 44 |
} |
| 45 |
|
| 46 |
local_vars(vars) |
| 47 |
n <- length(vars) |
| 48 |
|
| 49 |
with_chained_errors( |
| 50 |
loc <- eval_tidy(expr, set_names(seq_along(vars), vars)), |
| 51 |
call = error_call, |
| 52 |
eval_expr = expr |
| 53 |
) |
| 54 |
|
| 55 |
loc <- pull_as_location2(loc, n, vars, error_arg = error_arg, error_call = error_call) |
| 56 |
|
| 57 |
if (loc < 0L) { |
| 58 |
loc <- n + 1L + loc |
| 59 |
} |
| 60 |
vars[[loc]] |
| 61 |
} |
| 62 |
|
| 63 |
pull_as_location2 <- function(i, n, names, error_call = caller_env(), error_arg = "var") { |
| 64 |
with_subscript_errors(type = "pull", { |
| 65 |
i <- vctrs::vec_as_subscript2(i, |
| 66 |
logical = "error", |
| 67 |
arg = error_arg, |
| 68 |
call = error_call |
| 69 |
) |
| 70 |
|
| 71 |
if (length(i) != 1) { |
| 72 |
cli::cli_abort( |
| 73 |
"{.arg {error_arg}} must select exactly one column.", |
| 74 |
call = error_call |
| 75 |
) |
| 76 |
} |
| 77 |
|
| 78 |
if (is.numeric(i)) { |
| 79 |
vctrs::num_as_location2( |
| 80 |
i, |
| 81 |
n = n, |
| 82 |
negative = "ignore", |
| 83 |
arg = error_arg, |
| 84 |
call = error_call |
| 85 |
) |
| 86 |
} else { |
| 87 |
vctrs::vec_as_location2( |
| 88 |
i, |
| 89 |
n = n, |
| 90 |
names = names, |
| 91 |
arg = error_arg, |
| 92 |
call = error_call, |
| 93 |
) |
| 94 |
} |
| 95 |
}) |
| 96 |
} |