File: options.R

package info (click to toggle)
r-cran-withr 3.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 13; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#' @include with_.R

# options --------------------------------------------------------------------

get_options <- function(new_options) {
  do.call(options, as.list(names(new_options)))
}
set_options <- function(new_options) {
  do.call(options, as.list(new_options))
}
reset_options <- function(old_options) {
  options(old_options)
}

#' Options
#'
#' Temporarily change global options.
#'
#' @template with
#' @param new,.new `[named list]`\cr New options and their values
#' @param ... Additional options and their values
#' @inheritParams with_collate
#' @seealso [options()]
#' @examples
#' # number of significant digits to print
#' getOption("digits")
#' # modify temporarily the number of significant digits to print
#' with_options(list(digits = 3), getOption("digits"))
#' with_options(list(digits = 3), print(pi))
#'
#' # modify temporarily the character to be used as the decimal point
#' getOption("digits")
#' with_options(list(OutDec = ","), print(pi))
#'
#' # modify temporarily multiple options
#' with_options(list(OutDec = ",", digits = 3), print(pi))
#'
#' # modify, within the scope of the function, the number of
#' # significant digits to print
#' print_3_digits <- function(x) {
#'   # assign 3 to the option "digits" for the rest of this function
#'   # after the function exits, the option will return to its previous
#'   # value
#'   local_options(list(digits = 3))
#'   print(x)
#' }
#'
#' print_3_digits(pi)  # returns 3.14
#' print(pi)           # returns 3.141593

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

#' @rdname with_options
#' @export
local_options <- local_(
  set_options,
  reset_options,
  get = get_options,
  dots = TRUE
)