| 1 |
.units_options <- new.env(FALSE, parent = globalenv()) |
| 2 |
|
| 3 |
.default_options <- list( |
| 4 |
sep = c("~", "~"), |
| 5 |
group = c("[", "]"), |
| 6 |
negative_power = FALSE, |
| 7 |
parse = TRUE, |
| 8 |
set_units_mode = "symbols", |
| 9 |
strict_tokenizer = FALSE, |
| 10 |
auto_convert_names_to_symbols = TRUE, |
| 11 |
simplify = NA, |
| 12 |
allow_mixed = FALSE, |
| 13 |
unitless_symbol = "1", |
| 14 |
define_bel = TRUE |
| 15 |
) |
| 16 |
|
| 17 |
.setopt <- function(option) { |
| 18 |
name <- paste0(".units.", substitute(option)) |
| 19 |
last <- mget(name, envir = .units_options, ifnotfound = NA)[[1]] |
| 20 |
assign(name, option, envir = .units_options) |
| 21 |
last |
| 22 |
} |
| 23 |
|
| 24 |
#' set one or more units global options |
| 25 |
#' |
| 26 |
#' set units global options, mostly related how units are printed and plotted |
| 27 |
#' @param ... named options (character) for which the value is queried |
| 28 |
#' @param sep character length two; default \code{c("~", "~")}; space separator between variable and units, and space separator between two different units |
| 29 |
#' @param group character length two; start and end group, may be two empty strings, a parenthesis pair, or square brackets; default: square brackets. |
| 30 |
#' @param negative_power logical, default \code{FALSE}; should denominators have negative power, or follow a division symbol? |
| 31 |
#' @param parse logical, default \code{TRUE}; should the units be made into an expression (so we get subscripts)? Setting to \code{FALSE} may be useful if \link{parse} fails, e.g. if the unit contains symbols that assume a particular encoding |
| 32 |
#' @param set_units_mode character; either \code{"symbols"} or \code{"standard"}; see \link{set_units}; default is \code{"symbols"} |
| 33 |
#' @param strict_tokenizer logical, default \code{FALSE}; non-strict tokenization attaches constants to the following symbol. |
| 34 |
#' @param auto_convert_names_to_symbols logical, default \code{TRUE}: should names, such as \code{degree_C} be converted to their usual symbol? |
| 35 |
#' @param simplify logical, default \code{NA}; simplify units in expressions? |
| 36 |
#' @param allow_mixed logical; if \code{TRUE}, combining mixed units creates a \code{mixed_units} object, if \code{FALSE} it generates an error |
| 37 |
#' @param unitless_symbol character; set the symbol to use for unitless (1) units |
| 38 |
#' @param define_bel logical; if \code{TRUE}, define the unit \code{B} (i.e., the \emph{bel}, widely used with the \emph{deci-} prefix as \code{dB}, \emph{decibel}) as an alias of \code{lg(re 1)}. \code{TRUE} by default, unless \code{B} is already defined in the existing XML database. |
| 39 |
#' @details This sets or gets units options. Set them by using named arguments, get them by passing the option name. |
| 40 |
#' |
| 41 |
#' The default \code{NA} value for \code{simplify} means units are not simplified in \link{set_units} or \link{as_units}, but are simplified in arithmetical expressions. |
| 42 |
#' @return in case options are set, invisibly a named list with the option values that are being set; if an option is queried, the current option value. |
| 43 |
#' @examples |
| 44 |
#' old = units_options(sep = c("~~~", "~"), group = c("", "")) # more space, parenthesis |
| 45 |
#' old |
| 46 |
#' ## set back to defaults: |
| 47 |
#' units_options(sep = c("~", "~"), group = c("[", "]"), negative_power = FALSE, parse = TRUE) |
| 48 |
#' units_options("group") |
| 49 |
#' @export |
| 50 |
units_options = function(..., sep, group, negative_power, parse, set_units_mode, |
| 51 |
strict_tokenizer, auto_convert_names_to_symbols, simplify, |
| 52 |
allow_mixed, unitless_symbol, define_bel) |
| 53 |
{ |
| 54 |
# op = as.list(units:::.units_options) |
| 55 |
ret = list() |
| 56 |
if (!missing(sep)) { |
| 57 |
stopifnot(is.character(sep) && length(sep) == 2) |
| 58 |
ret$sep = .setopt(sep) |
| 59 |
} |
| 60 |
if (!missing(group)) { |
| 61 |
stopifnot(is.character(group) && length(group) == 2) |
| 62 |
ret$group = .setopt(group) |
| 63 |
} |
| 64 |
if (!missing(negative_power)) { |
| 65 |
stopifnot(is.logical(negative_power)) |
| 66 |
ret$negative_power = .setopt(negative_power) |
| 67 |
} |
| 68 |
if (!missing(parse)) { |
| 69 |
stopifnot(is.logical(parse)) |
| 70 |
ret$parse = .setopt(parse) |
| 71 |
} |
| 72 |
if (!missing(set_units_mode)) { |
| 73 |
stopifnot(is.character(set_units_mode) && length(set_units_mode) == 1) |
| 74 |
ret$set_units_mode = .setopt(set_units_mode) |
| 75 |
} |
| 76 |
if (!missing(strict_tokenizer)) { |
| 77 |
stopifnot(is.logical(strict_tokenizer)) |
| 78 |
ret$strict_tokenizer = .setopt(strict_tokenizer) |
| 79 |
} |
| 80 |
if (!missing(auto_convert_names_to_symbols)) { |
| 81 |
stopifnot(is.logical(auto_convert_names_to_symbols)) |
| 82 |
ret$auto_convert_names_to_symbols = .setopt(auto_convert_names_to_symbols) |
| 83 |
} |
| 84 |
if (!missing(simplify)) { |
| 85 |
stopifnot(is.logical(simplify)) |
| 86 |
ret$simplify = .setopt(simplify) |
| 87 |
} |
| 88 |
if (!missing(allow_mixed)) { |
| 89 |
stopifnot(is.logical(allow_mixed)) |
| 90 |
ret$allow_mixed = .setopt(allow_mixed) |
| 91 |
} |
| 92 |
if (!missing(unitless_symbol)) { |
| 93 |
stopifnot(is.character(unitless_symbol), length(unitless_symbol) == 1) |
| 94 |
ret$unitless_symbol = .setopt(unitless_symbol) |
| 95 |
} |
| 96 |
if (!missing(define_bel)) { |
| 97 |
stopifnot(is.logical(define_bel)) |
| 98 |
ret$define_bel = .setopt(define_bel) |
| 99 |
if (!identical(ret$define_bel, define_bel)) { |
| 100 |
if (!is.na(ret$define_bel)) |
| 101 |
remove_unit("B", "bel") |
| 102 |
if (define_bel) |
| 103 |
install_unit("B", "lg(re 1)", "bel") |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
dots = list(...) |
| 108 |
if (length(dots)) { |
| 109 |
if (length(ret) > 0) |
| 110 |
stop("either set, or get units_option, but don't try to do both in one function call") |
| 111 |
if (is.list(dots[[1]])) |
| 112 |
do.call(units_options, dots[[1]]) |
| 113 |
else |
| 114 |
get(paste0(".units.", dots[[1]]), envir = .units_options) |
| 115 |
} else |
| 116 |
invisible(ret) |
| 117 |
} |
| 118 |
|
| 119 |
.units.simplify = function() { |
| 120 |
get(".units.simplify", envir = .units_options) |
| 121 |
} |