| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/local_.R, R/with_.R |
| 3 |
\name{local_} |
| 4 |
\alias{local_} |
| 5 |
\alias{with_} |
| 6 |
\title{Create a new "with" or "local" function} |
| 7 |
\usage{ |
| 8 |
local_( |
| 9 |
set, |
| 10 |
reset = set, |
| 11 |
get = NULL, |
| 12 |
..., |
| 13 |
envir = parent.frame(), |
| 14 |
new = TRUE, |
| 15 |
dots = FALSE |
| 16 |
) |
| 17 |
|
| 18 |
with_(set, reset = set, get = NULL, ..., envir = parent.frame(), new = TRUE) |
| 19 |
} |
| 20 |
\arguments{ |
| 21 |
\item{set}{\verb{[function(...)]}\cr Function used to set the state. |
| 22 |
The return value from this function should be the old state, which will |
| 23 |
then be passed back into the \code{reset()} function to clean up the state. |
| 24 |
The function can have arbitrarily many arguments, they will be replicated |
| 25 |
in the formals of the returned function.} |
| 26 |
|
| 27 |
\item{reset}{\verb{[function(x)]}\cr Function used to reset the state. |
| 28 |
The first argument can be named arbitrarily, further arguments with default |
| 29 |
values, or a "dots" argument, are supported but not used: The function will |
| 30 |
be called as \code{reset(old)}.} |
| 31 |
|
| 32 |
\item{get}{\verb{[function(...)]}\cr Optionally, a getter function. If |
| 33 |
supplied, the \code{on.exit()} restoration is set up \emph{before} calling |
| 34 |
\code{set}. This is more robust in edge cases. |
| 35 |
|
| 36 |
For technical reasons, this getter function must have the same |
| 37 |
interface as \code{set}, which means it is passed the new values as |
| 38 |
well. These can be safely ignored.} |
| 39 |
|
| 40 |
\item{...}{These dots are for future extensions and must be empty.} |
| 41 |
|
| 42 |
\item{envir}{\verb{[environment]}\cr Environment of the returned function.} |
| 43 |
|
| 44 |
\item{new}{\verb{[logical(1)]}\cr Replace the first argument of the \code{set} function |
| 45 |
by \code{new}? Set to \code{FALSE} if the \code{set} function only has optional arguments.} |
| 46 |
} |
| 47 |
\value{ |
| 48 |
\verb{[function(new, code, ...)]} A function with at least two arguments, |
| 49 |
\itemize{ |
| 50 |
\item \code{new}: New state to use |
| 51 |
\item \code{code}: Code to run in that state. |
| 52 |
} |
| 53 |
If there are more arguments to the function passed in \code{set} they are |
| 54 |
added to the returned function. If \code{set} does not have arguments, |
| 55 |
or \code{new} is \code{FALSE}, the returned function does not have a \code{code} argument. |
| 56 |
} |
| 57 |
\description{ |
| 58 |
These are constructors for \code{with_...} or \code{local_...} functions. |
| 59 |
They are only needed if you want to alter some global state which is not |
| 60 |
covered by the existing \code{with_...} functions, see \link{withr} |
| 61 |
for an overview. |
| 62 |
} |
| 63 |
\details{ |
| 64 |
The \code{with_...} functions reset the state immediately after the |
| 65 |
\code{code} argument has been evaluated. The \code{local_...} functions |
| 66 |
reset their arguments after they go out of scope, usually at the end of the |
| 67 |
function body. |
| 68 |
} |
| 69 |
\examples{ |
| 70 |
with_(setwd) |
| 71 |
|
| 72 |
global_stack <- list() |
| 73 |
set_global_state <- function(state, msg = "Changing global state.") { |
| 74 |
global_stack <- c(list(state), global_stack) |
| 75 |
message(msg) |
| 76 |
state |
| 77 |
} |
| 78 |
reset_global_state <- function(state) { |
| 79 |
old_state <- global_stack[[1]] |
| 80 |
global_stack <- global_stack[-1] |
| 81 |
stopifnot(identical(state, old_state)) |
| 82 |
} |
| 83 |
with_(set_global_state, reset_global_state) |
| 84 |
} |
| 85 |
\keyword{internal} |