1
#' @include with_.R
2

3
# options --------------------------------------------------------------------
4

5
set_options <- function(new_options) {
6 1
  do.call(options, as.list(new_options))
7
}
8

9
reset_options <- function(old_options) {
10 1
    options(old_options)
11
}
12

13
#' Options
14
#'
15
#' Temporarily change global options.
16
#'
17
#' @template with
18
#' @param new,.new `[named list]`\cr New options and their values
19
#' @param ... Additional options and their values
20
#' @inheritParams with_collate
21
#' @seealso [options()]
22
#' @examples
23
#' # number of significant digits to print
24
#' getOption("digits")
25
#' # modify temporarily the number of significant digits to print
26
#' with_options(list(digits = 3), getOption("digits"))
27
#' with_options(list(digits = 3), print(pi))
28
#'
29
#' # modify temporarily the character to be used as the decimal point
30
#' getOption("digits")
31
#' with_options(list(OutDec = ","), print(pi))
32
#'
33
#' # modify temporarily multiple options
34
#' with_options(list(OutDec = ",", digits = 3), print(pi))
35
#'
36
#' # modify, within the scope of the function, the number of
37
#' # significant digits to print
38
#' print_3_digits <- function(x) {
39
#'   # assign 3 to the option "digits" for the rest of this function
40
#'   # after the function exits, the option will return to its previous
41
#'   # value
42
#'   local_options(list(digits = 3))
43
#'   print(x)
44
#' }
45
#'
46
#' print_3_digits(pi)  # returns 3.14
47
#' print(pi)           # returns 3.141593
48

49
#' @export
50
with_options <- with_(set_options, reset_options)
51

52
#' @rdname with_options
53
#' @export
54
local_options <- local_(set_options, reset_options, dots = TRUE)

Read our documentation on viewing source code .

Loading