Debian Codegraph @

R/user_conversion.R

raw
1 #' Define or remove units
2 #'
3 #' Installing new symbols and/or names allows them to be used in \code{as_units},
4 #' \code{make_units} and \code{set_units}. Optionally, a relationship can be
5 #' defined between such symbols/names and existing ones (see details and examples).
6 #'
7 #' At least one symbol or name is expected, but multiple symbols and/or names
8 #' can be installed (and thus mapped to the same unit) or removed at the same
9 #' time. The \code{def} argument enables arbitrary relationships with existing
10 #' units using UDUNITS-2 syntax:
11 #' \tabular{llll}{
12 #' \strong{String Type} \tab \strong{Using Names} \tab \strong{Using Symbols}
13 #' \tab \strong{Comment}\cr
14 #' Simple \tab meter \tab m \tab \cr
15 #' Raised \tab meter^2 \tab m2 \tab
16 #' higher precedence than multiplying or dividing\cr
17 #' Product \tab newton meter \tab N.m \tab \cr
18 #' Quotient \tab meter per second \tab m/s \tab \cr
19 #' Scaled \tab 60 second \tab 60 s \tab \cr
20 #' Prefixed \tab kilometer \tab km \tab \cr
21 #' Offset \tab kelvin from 273.15 \tab K @ 273.15 \tab
22 #' lower precedence than multiplying or dividing\cr
23 #' Logarithmic \tab lg(re milliwatt) \tab lg(re mW) \tab
24 #' "lg" is base 10, "ln" is base e, and "lb" is base 2\cr
25 #' Grouped \tab (5 meter)/(30 second) \tab (5 m)/(30 s) \tab
26 #' }
27 #' The above may be combined, e.g., \code{"0.1 lg(re m/(5 s)^2) @ 50"}.
28 #' You may also look at the \code{<def>} elements in the units database to see
29 #' examples of string unit specifications.
30 #'
31 #' @param symbol a vector of symbols to be installed/removed.
32 #' @param def either \itemize{
33 #' \item an empty definition, which defines a new base unit;
34 #' \item \code{"unitless"}, which defines a new dimensionless unit;
35 #' \item a relationship with existing units (see details for the syntax).
36 #' }
37 #' @param name a vector of names to be installed/removed.
38 #'
39 #' @examples
40 #' # define a fortnight
41 #' install_unit("fn", "2 week", "fortnight")
42 #' year <- as_units("year")
43 #' set_units(year, fn) # by symbol
44 #' set_units(year, fortnight) # by name
45 #' # clean up
46 #' remove_unit("fn", "fortnight")
47 #'
48 #' # working with currencies
49 #' install_unit("dollar")
50 #' install_unit("euro", "1.22 dollar")
51 #' install_unit("yen", "0.0079 euro")
52 #' set_units(as_units("dollar"), yen)
53 #' # clean up
54 #' remove_unit(c("dollar", "euro", "yen"))
55 #'
56 #' # an example from microbiology
57 #' cfu_symbols <- c("CFU", "cfu")
58 #' cfu_names <- c("colony_forming_unit", "ColonyFormingUnit")
59 #' install_unit("cell")
60 #' install_unit(cfu_symbols, "3.4 cell", cfu_names)
61 #' cell <- set_units(2.5e5, cell)
62 #' vol <- set_units(500, ul)
63 #' set_units(cell/vol, "cfu/ml")
64 #' set_units(cell/vol, "CFU/ml")
65 #' set_units(cell/vol, "colony_forming_unit/ml")
66 #' set_units(cell/vol, "ColonyFormingUnit/ml")
67 #' # clean up
68 #' remove_unit(c("cell", cfu_symbols), cfu_names)
69 #'
70 #' @export
71 install_unit <- function(symbol=character(0), def=character(0), name=character(0)) {
72 stopifnot(is.character(def), length(def) < 2)
73 stopifnot(is.character(symbol), is.character(name))
74 if (!length(symbol) && !length(name))
75 stop("at least one symbol or name must be specified")
76
77 # workaround for https://github.com/r-quantities/units/issues/289
78 if (any(grepl("%", c(symbol, name), fixed=TRUE)))
79 stop("Symbols/names with a percentage character '%' are not allowed ",
80 "(see https://github.com/r-quantities/units/issues/289)")
81
82 if (!length(def)) {
83 ut_unit <- R_ut_new_base_unit()
84 } else if (identical(def, "unitless")) {
85 ut_unit <- R_ut_new_dimensionless_unit()
86 } else {
87 ut_unit <- R_ut_parse(def)
88 }
89
90 ud_map_symbols(symbol, ut_unit)
91 ud_map_names(name, ut_unit)
92 }
93
94 #' @rdname install_unit
95 #' @export
96 remove_unit <- function(symbol=character(0), name=character(0)) {
97 stopifnot(is.character(symbol), is.character(name))
98 if (!length(symbol) && !length(name))
99 stop("at least one symbol or name must be specified")
100
101 ud_unmap_symbols(symbol)
102 ud_unmap_names(name)
103 }