| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/eval-relocate.R |
| 3 |
\name{eval_relocate} |
| 4 |
\alias{eval_relocate} |
| 5 |
\title{Evaluate an expression to relocate variables} |
| 6 |
\usage{ |
| 7 |
eval_relocate( |
| 8 |
expr, |
| 9 |
data, |
| 10 |
..., |
| 11 |
before = NULL, |
| 12 |
after = NULL, |
| 13 |
strict = TRUE, |
| 14 |
name_spec = NULL, |
| 15 |
allow_rename = TRUE, |
| 16 |
allow_empty = TRUE, |
| 17 |
allow_predicates = TRUE, |
| 18 |
before_arg = "before", |
| 19 |
after_arg = "after", |
| 20 |
env = caller_env(), |
| 21 |
error_call = caller_env() |
| 22 |
) |
| 23 |
} |
| 24 |
\arguments{ |
| 25 |
\item{expr}{Defused R code describing a selection according to the |
| 26 |
tidyselect syntax.} |
| 27 |
|
| 28 |
\item{data}{A named list, data frame, or atomic vector. |
| 29 |
Technically, \code{data} can be any vector with \code{names()} and \code{"[["} |
| 30 |
implementations.} |
| 31 |
|
| 32 |
\item{...}{These dots are for future extensions and must be empty.} |
| 33 |
|
| 34 |
\item{before, after}{Defused R code describing a selection according to the |
| 35 |
tidyselect syntax. The selection represents the destination of the |
| 36 |
selection provided through \code{expr}. Supplying neither of these will move the |
| 37 |
selection to the left-hand side. Supplying both of these is an error.} |
| 38 |
|
| 39 |
\item{strict}{If \code{TRUE}, out-of-bounds errors are thrown if \code{expr} |
| 40 |
attempts to select or rename a variable that doesn't exist. If |
| 41 |
\code{FALSE}, failed selections or renamings are ignored.} |
| 42 |
|
| 43 |
\item{name_spec}{A name specification describing how to combine or |
| 44 |
propagate names. This is used only in case nested \code{c()} |
| 45 |
expressions like \code{c(foo = c(bar = starts_with("foo")))}. See the |
| 46 |
\code{name_spec} argument of \code{\link[vctrs:vec_c]{vctrs::vec_c()}} for a description of |
| 47 |
valid name specs.} |
| 48 |
|
| 49 |
\item{allow_rename}{If \code{TRUE} (the default), the renaming syntax |
| 50 |
\code{c(foo = bar)} is allowed. If \code{FALSE}, it causes an error. This |
| 51 |
is useful to implement purely selective behaviour.} |
| 52 |
|
| 53 |
\item{allow_empty}{If \code{TRUE} (the default), it is ok for \code{expr} to result |
| 54 |
in an empty selection. If \code{FALSE}, will error if \code{expr} yields an empty |
| 55 |
selection.} |
| 56 |
|
| 57 |
\item{allow_predicates}{If \code{TRUE} (the default), it is ok for \code{expr} to |
| 58 |
use predicates (i.e. in \code{where()}). If \code{FALSE}, will error if \code{expr} uses a |
| 59 |
predicate. Will automatically be set to \code{FALSE} if \code{data} does not |
| 60 |
support predicates (as determined by \code{\link[=tidyselect_data_has_predicates]{tidyselect_data_has_predicates()}}).} |
| 61 |
|
| 62 |
\item{before_arg, after_arg}{Argument names for \code{before} and \code{after}. These |
| 63 |
are used in error messages.} |
| 64 |
|
| 65 |
\item{env}{The environment in which to evaluate \code{expr}. Discarded |
| 66 |
if \code{expr} is a \link[rlang:enquo]{quosure}.} |
| 67 |
|
| 68 |
\item{error_call}{The execution environment of a currently |
| 69 |
running function, e.g. \code{caller_env()}. The function will be |
| 70 |
mentioned in error messages as the source of the error. See the |
| 71 |
\code{call} argument of \code{\link[rlang:abort]{abort()}} for more information.} |
| 72 |
} |
| 73 |
\value{ |
| 74 |
A named vector of numeric locations with length equal to \code{length(data)}. |
| 75 |
Each position in \code{data} will be represented exactly once. |
| 76 |
|
| 77 |
The names are normally the same as in the input data, except when the user |
| 78 |
supplied named selections with \code{c()}. In the latter case, the names reflect |
| 79 |
the new names chosen by the user. |
| 80 |
} |
| 81 |
\description{ |
| 82 |
\code{eval_relocate()} is a variant of \code{\link[=eval_select]{eval_select()}} that moves a selection to |
| 83 |
a new location. Either \code{before} or \code{after} can be provided to specify where |
| 84 |
to move the selection to. This powers \code{dplyr::relocate()}. |
| 85 |
} |
| 86 |
\examples{ |
| 87 |
library(rlang) |
| 88 |
|
| 89 |
# Interpret defused code as a request to relocate |
| 90 |
x <- expr(c(mpg, disp)) |
| 91 |
after <- expr(wt) |
| 92 |
eval_relocate(x, mtcars, after = after) |
| 93 |
|
| 94 |
# Supplying neither `before` nor `after` will move the selection to the |
| 95 |
# left-hand side |
| 96 |
eval_relocate(x, mtcars) |
| 97 |
|
| 98 |
# Within a function, use `enquo()` to defuse a single argument. |
| 99 |
# Note that `before` and `after` must also be defused with `enquo()`. |
| 100 |
my_relocator <- function(x, expr, before = NULL, after = NULL) { |
| 101 |
eval_relocate(enquo(expr), x, before = enquo(before), after = enquo(after)) |
| 102 |
} |
| 103 |
|
| 104 |
my_relocator(mtcars, vs, before = hp) |
| 105 |
|
| 106 |
# Here is an example of using `eval_relocate()` to implement `relocate()`. |
| 107 |
# Note that the dots are passed on as a defused call to `c(...)`. |
| 108 |
relocate <- function(.x, ..., .before = NULL, .after = NULL) { |
| 109 |
pos <- eval_relocate( |
| 110 |
expr(c(...)), |
| 111 |
.x, |
| 112 |
before = enquo(.before), |
| 113 |
after = enquo(.after) |
| 114 |
) |
| 115 |
set_names(.x[pos], names(pos)) |
| 116 |
} |
| 117 |
|
| 118 |
relocate(mtcars, vs, .before = hp) |
| 119 |
relocate(mtcars, starts_with("d"), .after = last_col()) |
| 120 |
} |