Debian Codegraph @

man/withr.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/with.R
3 \docType{package}
4 \name{withr}
5 \alias{withr-package}
6 \alias{withr}
7 \title{Execute code in temporarily altered environment}
8 \description{
9 All functions prefixed by \code{with_} work as follows. First, a particular
10 aspect of the global environment is modified (see below for a list).
11 Then, custom code (passed via the \code{code} argument) is executed.
12 Upon completion or error, the global environment is restored to the previous
13 state. Each \code{with_} function has a \code{local_} variant, which instead resets
14 the state when the current evaluation context ends (such as the end of a
15 function).
16 }
17 \section{Arguments pattern}{
18
19 \tabular{lll}{
20 \code{new} \tab \verb{[various]} \tab Values for setting \cr
21 \code{code} \tab \verb{[any]} \tab Code to execute in the temporary environment \cr
22 \code{...} \tab \tab Further arguments \cr
23 }
24 }
25
26 \section{Usage pattern}{
27
28 \code{with_...(new, code, ...)}
29 }
30
31 \section{withr functions}{
32
33 \itemize{
34 \item \code{\link[=with_collate]{with_collate()}}: collation order
35 \item \code{\link[=with_dir]{with_dir()}}: working directory
36 \item \code{\link[=with_envvar]{with_envvar()}}: environment variables
37 \item \code{\link[=with_libpaths]{with_libpaths()}}: library paths, replacing current libpaths
38 \item \code{\link[=with_locale]{with_locale()}}: any locale setting
39 \item \code{\link[=with_makevars]{with_makevars()}}: Makevars variables
40 \item \code{\link[=with_options]{with_options()}}: options
41 \item \code{\link[=with_par]{with_par()}}: graphics parameters
42 \item \code{\link[=with_path]{with_path()}}: \code{PATH} environment variable
43 \item \code{\link[=with_sink]{with_sink()}}: output redirection
44 }
45 }
46
47 \section{Creating new "with" functions}{
48
49 All \code{with_} functions are created by a helper function,
50 \code{\link[=with_]{with_()}}. This functions accepts two arguments:
51 a setter function and an optional resetter function. The setter function is
52 expected to change the global state and return an "undo instruction".
53 This undo instruction is then passed to the resetter function, which changes
54 back the global state. In many cases, the setter function can be used
55 naturally as resetter.
56 }
57
58 \examples{
59 getwd()
60 with_dir(tempdir(), getwd())
61 getwd()
62
63 Sys.getenv("WITHR")
64 with_envvar(c("WITHR" = 2), Sys.getenv("WITHR"))
65 Sys.getenv("WITHR")
66
67 with_envvar(c("A" = 1),
68 with_envvar(c("A" = 2), action = "suffix", Sys.getenv("A"))
69 )
70
71 # local variants are best used within other functions
72 f <- function(x) {
73 local_envvar(c("WITHR" = 2))
74 Sys.getenv("WITHR")
75 }
76 Sys.getenv("WITHR")
77 }
78 \seealso{
79 Useful links:
80 \itemize{
81 \item \url{https://withr.r-lib.org}
82 \item \url{https://github.com/r-lib/withr#readme}
83 \item Report bugs at \url{https://github.com/r-lib/withr/issues}
84 }
85
86 }
87 \author{
88 \strong{Maintainer}: Lionel Henry \email{[email protected]}
89
90 Authors:
91 \itemize{
92 \item Jim Hester
93 \item Kirill Müller \email{[email protected]}
94 \item Kevin Ushey \email{[email protected]}
95 \item Hadley Wickham \email{[email protected]}
96 \item Winston Chang
97 }
98
99 Other contributors:
100 \itemize{
101 \item Jennifer Bryan [contributor]
102 \item Richard Cotton [contributor]
103 \item Posit Software, PBC [copyright holder, funder]
104 }
105
106 }