Debian Codegraph @

man/with_options.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/options.R
3 \name{with_options}
4 \alias{with_options}
5 \alias{local_options}
6 \title{Options}
7 \usage{
8 with_options(new, code)
9
10 local_options(.new = list(), ..., .local_envir = parent.frame())
11 }
12 \arguments{
13 \item{new, .new}{\verb{[named list]}\cr New options and their values}
14
15 \item{code}{\code{[any]}\cr Code to execute in the temporary environment}
16
17 \item{...}{Additional options and their values}
18
19 \item{.local_envir}{\verb{[environment]}\cr The environment to use for scoping.}
20 }
21 \value{
22 \code{[any]}\cr The results of the evaluation of the \code{code}
23 argument.
24 }
25 \description{
26 Temporarily change global options.
27 }
28 \examples{
29 # number of significant digits to print
30 getOption("digits")
31 # modify temporarily the number of significant digits to print
32 with_options(list(digits = 3), getOption("digits"))
33 with_options(list(digits = 3), print(pi))
34
35 # modify temporarily the character to be used as the decimal point
36 getOption("digits")
37 with_options(list(OutDec = ","), print(pi))
38
39 # modify temporarily multiple options
40 with_options(list(OutDec = ",", digits = 3), print(pi))
41
42 # modify, within the scope of the function, the number of
43 # significant digits to print
44 print_3_digits <- function(x) {
45 # assign 3 to the option "digits" for the rest of this function
46 # after the function exits, the option will return to its previous
47 # value
48 local_options(list(digits = 3))
49 print(x)
50 }
51
52 print_3_digits(pi) # returns 3.14
53 print(pi) # returns 3.141593
54 }
55 \seealso{
56 \code{\link{withr}} for examples
57
58 \code{\link[=options]{options()}}
59 }