| eval_relocate {tidyselect} | R Documentation |
Evaluate an expression to relocate variables
Description
eval_relocate() is a variant of eval_select() that moves a selection to
a new location. Either before or after can be provided to specify where
to move the selection to. This powers dplyr::relocate().
Usage
eval_relocate(
expr,
data,
...,
before = NULL,
after = NULL,
strict = TRUE,
name_spec = NULL,
allow_rename = TRUE,
allow_empty = TRUE,
allow_predicates = TRUE,
before_arg = "before",
after_arg = "after",
env = caller_env(),
error_call = caller_env()
)
Arguments
expr |
Defused R code describing a selection according to the tidyselect syntax. |
data |
A named list, data frame, or atomic vector.
Technically, |
... |
These dots are for future extensions and must be empty. |
before, after |
Defused R code describing a selection according to the
tidyselect syntax. The selection represents the destination of the
selection provided through |
strict |
If |
name_spec |
A name specification describing how to combine or
propagate names. This is used only in case nested |
allow_rename |
If |
allow_empty |
If |
allow_predicates |
If |
before_arg, after_arg |
Argument names for |
env |
The environment in which to evaluate |
error_call |
The execution environment of a currently
running function, e.g. |
Value
A named vector of numeric locations with length equal to length(data).
Each position in data will be represented exactly once.
The names are normally the same as in the input data, except when the user
supplied named selections with c(). In the latter case, the names reflect
the new names chosen by the user.
Examples
library(rlang)
# Interpret defused code as a request to relocate
x <- expr(c(mpg, disp))
after <- expr(wt)
eval_relocate(x, mtcars, after = after)
# Supplying neither `before` nor `after` will move the selection to the
# left-hand side
eval_relocate(x, mtcars)
# Within a function, use `enquo()` to defuse a single argument.
# Note that `before` and `after` must also be defused with `enquo()`.
my_relocator <- function(x, expr, before = NULL, after = NULL) {
eval_relocate(enquo(expr), x, before = enquo(before), after = enquo(after))
}
my_relocator(mtcars, vs, before = hp)
# Here is an example of using `eval_relocate()` to implement `relocate()`.
# Note that the dots are passed on as a defused call to `c(...)`.
relocate <- function(.x, ..., .before = NULL, .after = NULL) {
pos <- eval_relocate(
expr(c(...)),
.x,
before = enquo(.before),
after = enquo(.after)
)
set_names(.x[pos], names(pos))
}
relocate(mtcars, vs, .before = hp)
relocate(mtcars, starts_with("d"), .after = last_col())