| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/vars.R |
| 3 |
\name{poke_vars} |
| 4 |
\alias{poke_vars} |
| 5 |
\alias{scoped_vars} |
| 6 |
\alias{with_vars} |
| 7 |
\alias{has_vars} |
| 8 |
\title{Replace or get current variables} |
| 9 |
\usage{ |
| 10 |
poke_vars(vars) |
| 11 |
|
| 12 |
scoped_vars(vars, frame = caller_env()) |
| 13 |
|
| 14 |
with_vars(vars, expr) |
| 15 |
|
| 16 |
has_vars() |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{vars}{A character vector of variable names.} |
| 20 |
|
| 21 |
\item{frame}{The frame environment where the exit hook for |
| 22 |
restoring the old variables should be registered.} |
| 23 |
|
| 24 |
\item{expr}{An expression to be evaluated within the variable |
| 25 |
context.} |
| 26 |
} |
| 27 |
\value{ |
| 28 |
For \code{poke_vars()} and \code{scoped_vars()}, the old variables |
| 29 |
invisibly. For \code{peek_vars()}, the variables currently |
| 30 |
registered. |
| 31 |
} |
| 32 |
\description{ |
| 33 |
Variables are made available to \link[=language]{select helpers} by |
| 34 |
registering them in a special placeholder. |
| 35 |
\itemize{ |
| 36 |
\item \code{scoped_vars()} changes the current variables and sets up a |
| 37 |
function exit hook that automatically restores the previous |
| 38 |
variables once the current function returns. |
| 39 |
\item \code{with_vars()} takes an expression to be evaluated in a variable |
| 40 |
context. |
| 41 |
\item \code{poke_vars()} changes the contents of the placeholder with a new |
| 42 |
set of variables. It returns the previous variables invisibly and |
| 43 |
it is your responsibility to restore them after you are |
| 44 |
done. This is for expert use only. |
| 45 |
\item \code{peek_vars()} returns the variables currently registered. |
| 46 |
\item \code{has_vars()} returns \code{TRUE} if a variable context has been set, |
| 47 |
\code{FALSE} otherwise. |
| 48 |
} |
| 49 |
} |
| 50 |
\examples{ |
| 51 |
poke_vars(letters) |
| 52 |
peek_vars() |
| 53 |
|
| 54 |
# Now that the variables are registered, the helpers can figure out |
| 55 |
# the locations of elements within the variable vector: |
| 56 |
all_of(c("d", "z")) |
| 57 |
|
| 58 |
# In a function be sure to restore the previous variables. An exit |
| 59 |
# hook is the best way to do it: |
| 60 |
fn <- function(vars) { |
| 61 |
old <- poke_vars(vars) |
| 62 |
on.exit(poke_vars(old)) |
| 63 |
|
| 64 |
all_of("d") |
| 65 |
} |
| 66 |
fn(letters) |
| 67 |
fn(letters[3:5]) |
| 68 |
|
| 69 |
# The previous variables are still registered after fn() was |
| 70 |
# called: |
| 71 |
peek_vars() |
| 72 |
|
| 73 |
|
| 74 |
# It is recommended to use the scoped variant as it restores the |
| 75 |
# state automatically when the function returns: |
| 76 |
fn <- function(vars) { |
| 77 |
scoped_vars(vars) |
| 78 |
starts_with("r") |
| 79 |
} |
| 80 |
fn(c("red", "blue", "rose")) |
| 81 |
|
| 82 |
# The with_vars() helper makes it easy to pass an expression that |
| 83 |
# should be evaluated in a variable context. Thanks to lazy |
| 84 |
# evaluation, you can just pass the expression argument from your |
| 85 |
# wrapper to with_vars(): |
| 86 |
fn <- function(expr) { |
| 87 |
vars <- c("red", "blue", "rose") |
| 88 |
with_vars(vars, expr) |
| 89 |
} |
| 90 |
fn(starts_with("r")) |
| 91 |
} |
| 92 |
\seealso{ |
| 93 |
peek_vars |
| 94 |
} |
| 95 |
\keyword{internal} |