withOptions.Rd
Evaluate an R expression with options set temporarily.
withOptions(expr, ..., args=list(), substitute=TRUE, envir=parent.frame())
The R expression to be evaluated.
Named options to be used.
(optional) Additional named options specified as a named list
.
If TRUE
, argument expr
is
substitute()
:ed, otherwise not.
The environment
in which the expression should be evaluated.
Returns the results of the expression evaluated.
Upon exit (also on errors), this function will reset all
options to the state of options available upon entry. This means
any options modified but also those added when
evaluating expr
will also be undone upon exit.
print(pi)
#> [1] 3.141593
# Same, i.e. using default
withOptions({
print(pi)
})
#> [1] 3.141593
# Printing with two digits
withOptions({
print(pi)
}, digits=2)
#> [1] 3.1
# Printing with two digits then with three more
withOptions({
print(pi)
withOptions({
print(pi)
}, digits=getOption("digits")+3)
}, digits=2)
#> [1] 3.1
#> [1] 3.1416
# Still printing with the default
print(pi)
#> [1] 3.141593