Debian Codegraph @

man/eval_select.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/eval-rename.R, R/eval-select.R
3 \name{eval_rename}
4 \alias{eval_rename}
5 \alias{eval_select}
6 \title{Evaluate an expression with tidyselect semantics}
7 \usage{
8 eval_rename(
9 expr,
10 data,
11 env = caller_env(),
12 ...,
13 strict = TRUE,
14 name_spec = NULL,
15 allow_predicates = TRUE,
16 error_call = caller_env()
17 )
18
19 eval_select(
20 expr,
21 data,
22 env = caller_env(),
23 ...,
24 include = NULL,
25 exclude = NULL,
26 strict = TRUE,
27 name_spec = NULL,
28 allow_rename = TRUE,
29 allow_empty = TRUE,
30 allow_predicates = TRUE,
31 error_call = caller_env()
32 )
33 }
34 \arguments{
35 \item{expr}{Defused R code describing a selection according to the
36 tidyselect syntax.}
37
38 \item{data}{A named list, data frame, or atomic vector.
39 Technically, \code{data} can be any vector with \code{names()} and \code{"[["}
40 implementations.}
41
42 \item{env}{The environment in which to evaluate \code{expr}. Discarded
43 if \code{expr} is a \link[rlang:enquo]{quosure}.}
44
45 \item{...}{These dots are for future extensions and must be empty.}
46
47 \item{strict}{If \code{TRUE}, out-of-bounds errors are thrown if \code{expr}
48 attempts to select or rename a variable that doesn't exist. If
49 \code{FALSE}, failed selections or renamings are ignored.}
50
51 \item{name_spec}{A name specification describing how to combine or
52 propagate names. This is used only in case nested \code{c()}
53 expressions like \code{c(foo = c(bar = starts_with("foo")))}. See the
54 \code{name_spec} argument of \code{\link[vctrs:vec_c]{vctrs::vec_c()}} for a description of
55 valid name specs.}
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{error_call}{The execution environment of a currently
63 running function, e.g. \code{caller_env()}. The function will be
64 mentioned in error messages as the source of the error. See the
65 \code{call} argument of \code{\link[rlang:abort]{abort()}} for more information.}
66
67 \item{include, exclude}{Character vector of column names to always
68 include or exclude from the selection.}
69
70 \item{allow_rename}{If \code{TRUE} (the default), the renaming syntax
71 \code{c(foo = bar)} is allowed. If \code{FALSE}, it causes an error. This
72 is useful to implement purely selective behaviour.}
73
74 \item{allow_empty}{If \code{TRUE} (the default), it is ok for \code{expr} to result
75 in an empty selection. If \code{FALSE}, will error if \code{expr} yields an empty
76 selection.}
77 }
78 \value{
79 A named vector of numeric locations, one for each of the
80 selected elements.
81
82 The names are normally the same as in the input data, except when
83 the user supplied named selections with \code{c()}. In the latter
84 case, the names reflect the new names chosen by the user.
85
86 A given element may be selected multiple times under different
87 names, in which case the vector might contain duplicate
88 locations.
89 }
90 \description{
91 \code{eval_select()} and \code{eval_rename()} evaluate defused R code
92 (i.e. quoted expressions) according to the special rules of the
93 \href{https://tidyselect.r-lib.org/articles/syntax.html}{tidyselect syntax}. They
94 power functions like \code{dplyr::select()}, \code{dplyr::rename()}, or
95 \code{tidyr::pivot_longer()}.
96
97 See the \href{https://tidyselect.r-lib.org/articles/tidyselect.html}{Get started}
98 vignette to learn how to use \code{eval_select()} and \code{eval_rename()} in
99 your packages.
100 }
101 \details{
102 The select and rename variants take the same types of inputs and
103 have the same type of return value. However \code{eval_rename()} has a
104 few extra constraints. It requires named inputs, and will fail if a
105 data frame column is renamed to another existing column name. See
106 the \href{https://tidyselect.r-lib.org/articles/syntax.html}{selecting versus renaming}
107 section in the syntax vignette for a description of the
108 differences.
109 }
110 \examples{
111 library(rlang)
112
113 # Interpret defused code as selection:
114 x <- expr(mpg:cyl)
115 eval_select(x, mtcars)
116
117 # Interpret defused code as a renaming selection. All inputs must
118 # be named within `c()`:
119 try(eval_rename(expr(mpg), mtcars))
120 eval_rename(expr(c(foo = mpg)), mtcars)
121
122
123 # Within a function, use `enquo()` to defuse one argument:
124 my_function <- function(x, expr) {
125 eval_select(enquo(expr), x)
126 }
127
128 # If your function takes dots, evaluate a defused call to `c(...)`
129 # with `expr(c(...))`:
130 my_function <- function(.x, ...) {
131 eval_select(expr(c(...)), .x)
132 }
133
134 # If your function takes dots and a named argument, use `{{ }}`
135 # inside the defused expression to tunnel it inside the tidyselect DSL:
136 my_function <- function(.x, .expr, ...) {
137 eval_select(expr(c({{ .expr }}, ...)), .x)
138 }
139
140 # Note that the trick above works because `expr({{ arg }})` is the
141 # same as `enquo(arg)`.
142
143
144 # The evaluators return a named vector of locations. Here are
145 # examples of using these location vectors to implement `select()`
146 # and `rename()`:
147 select <- function(.x, ...) {
148 pos <- eval_select(expr(c(...)), .x)
149 set_names(.x[pos], names(pos))
150 }
151 rename <- function(.x, ...) {
152 pos <- eval_rename(expr(c(...)), .x)
153 names(.x)[pos] <- names(pos)
154 .x
155 }
156
157 select(mtcars, mpg:cyl)
158 rename(mtcars, foo = mpg)
159 }
160 \seealso{
161 \url{https://tidyselect.r-lib.org/articles/syntax.html} or
162 \code{vignette("syntax", package = "tidyselect")} for a technical
163 description of the rules of evaluation.
164 }