# NOT RUN {
library(rlang)
# Interpret defused code as selection:
x <- expr(mpg:cyl)
eval_select(x, mtcars)
# Interpret defused code as a renaming selection. All inputs must
# be named within `c()`:
try(eval_rename(expr(mpg), mtcars))
eval_rename(expr(c(foo = mpg)), mtcars)
# The evaluators return a named vector of locations. Here are
# examples of using these location vectors to implement `select()`
# and `rename()`:
select <- function(.x, ...) {
pos <- eval_select(expr(c(...)), .x)
set_names(.x[pos], names(pos))
}
rename <- function(.x, ...) {
pos <- eval_rename(expr(c(...)), .x)
names(.x)[pos] <- names(pos)
.x
}
select(mtcars, mpg:cyl)
rename(mtcars, foo = mpg)
# }
Run the code above in your browser using DataLab