| 1 |
#' @include with_.R |
| 2 |
|
| 3 |
# options -------------------------------------------------------------------- |
| 4 |
|
| 5 |
get_options <- function(new_options) { |
| 6 |
do.call(options, as.list(names(new_options))) |
| 7 |
} |
| 8 |
set_options <- function(new_options) { |
| 9 |
do.call(options, as.list(new_options)) |
| 10 |
} |
| 11 |
reset_options <- function(old_options) { |
| 12 |
options(old_options) |
| 13 |
} |
| 14 |
|
| 15 |
#' Options |
| 16 |
#' |
| 17 |
#' Temporarily change global options. |
| 18 |
#' |
| 19 |
#' @template with |
| 20 |
#' @param new,.new `[named list]`\cr New options and their values |
| 21 |
#' @param ... Additional options and their values |
| 22 |
#' @inheritParams with_collate |
| 23 |
#' @seealso [options()] |
| 24 |
#' @examples |
| 25 |
#' # number of significant digits to print |
| 26 |
#' getOption("digits") |
| 27 |
#' # modify temporarily the number of significant digits to print |
| 28 |
#' with_options(list(digits = 3), getOption("digits")) |
| 29 |
#' with_options(list(digits = 3), print(pi)) |
| 30 |
#' |
| 31 |
#' # modify temporarily the character to be used as the decimal point |
| 32 |
#' getOption("digits") |
| 33 |
#' with_options(list(OutDec = ","), print(pi)) |
| 34 |
#' |
| 35 |
#' # modify temporarily multiple options |
| 36 |
#' with_options(list(OutDec = ",", digits = 3), print(pi)) |
| 37 |
#' |
| 38 |
#' # modify, within the scope of the function, the number of |
| 39 |
#' # significant digits to print |
| 40 |
#' print_3_digits <- function(x) { |
| 41 |
#' # assign 3 to the option "digits" for the rest of this function |
| 42 |
#' # after the function exits, the option will return to its previous |
| 43 |
#' # value |
| 44 |
#' local_options(list(digits = 3)) |
| 45 |
#' print(x) |
| 46 |
#' } |
| 47 |
#' |
| 48 |
#' print_3_digits(pi) # returns 3.14 |
| 49 |
#' print(pi) # returns 3.141593 |
| 50 |
|
| 51 |
#' @export |
| 52 |
with_options <- with_( |
| 53 |
set_options, |
| 54 |
reset_options |
| 55 |
) |
| 56 |
|
| 57 |
#' @rdname with_options |
| 58 |
#' @export |
| 59 |
local_options <- local_( |
| 60 |
set_options, |
| 61 |
reset_options, |
| 62 |
get = get_options, |
| 63 |
dots = TRUE |
| 64 |
) |