Debian Codegraph @

R/mixed.R

raw
1 .as.mixed_units = function(x) {
2 stopifnot(is.list(unclass(x)))
3 structure(x, class = c("mixed_units", "list"))
4 }
5
6 # constructor function:
7 #' Create or convert to a mixed units list-column
8 #' @param x numeric, or vector of class \code{units}
9 #' @param values character vector with units encodings, or list with symbolic units of class \code{mixed_symbolic_units}
10 #' @param value see values
11 #' @param ... ignored
12 #' @details if \code{x} is of class \code{units}, \code{values} should be missing or of class \code{mixed_symbolic_units}; if \code{x} is numeric, \code{values} should be a character vector the length of \code{x}.
13 #' @examples
14 #' a <- 1:4
15 #' u <- c("m/s", "km/h", "mg/L", "g")
16 #' mixed_units(a, u)
17 #' units(a) = as_units("m/s")
18 #' mixed_units(a) # converts to mixed representation
19 #' @export
20 mixed_units <- function(x, values, ...) UseMethod("mixed_units")
21
22
23 #' @export
24 mixed_units.units = function(x, values, ...) {
25 stopifnot(missing(values))
26 u = as.character(units(x))
27 mixed_units(unclass(x), rep(u, length(x)))
28 }
29
30
31 #' @export
32 mixed_units.numeric = function(x, values, ...) {
33 #stopifnot(length(x) == length(values), is.character(values), is.numeric(x))
34 stopifnot(is.character(values), is.numeric(x))
35 .as.mixed_units(mapply(set_units, x, values, mode = "standard", SIMPLIFY = FALSE))
36 }
37
38 #' @export
39 #' @name mixed_units
40 `units<-.mixed_units` = function(x, value) {
41 set_units(x, value)
42 }
43
44
45 #' @export
46 format.mixed_units = function(x, ...) {
47 sapply(x, format, ...)
48 }
49
50 #' @export
51 `[.mixed_units` = function(x, i, ...) {
52 .as.mixed_units(unclass(x)[i])
53 }
54
55 #' @export
56 c.mixed_units = function(...) {
57 args = list(...)
58 .as.mixed_units(do.call(c, lapply(args, unclass)))
59 }
60
61 #' @export
62 set_units.mixed_units = function(x, value, ..., mode = "standard") {
63 if (! is.character(value))
64 stop("use character string to denote target unit") # FIXME: rlang::quo stuff needed here?
65
66 # conversion data frame and split
67 cv <- data.frame(
68 val = as.numeric(x), from = I(units(x)), to = value, idx = seq_along(x),
69 stringsAsFactors = FALSE)
70 sp <- paste(cv$from, cv$to, sep=".")
71 sp <- factor(sp, levels=unique(sp))
72
73 # grouped conversion
74 cv <- do.call(rbind, unname(by(cv, sp, function(x) {
75 x$val <- set_units(x$val, x$from[[1]], mode="standard")
76 x$val <- mixed_units(set_units(x$val, x$to[1], mode="standard"))
77 x
78 }, simplify=FALSE)))
79
80 # reordering
81 cv[order(cv$idx), ]$val
82 }
83
84 #' @export
85 as_units.mixed_units = function(x, ...) {
86 set_units(do.call(c, x), value = units(x[[1]]), mode = "standard")
87 }
88
89
90 #' @export
91 units.mixed_units = function(x) {
92 u = lapply(x, function(i) if (inherits(i, "units")) units(i) else NULL)
93 structure(u, class = "mixed_symbolic_units")
94 }
95
96 #' @export
97 as.character.mixed_symbolic_units = function(x, ...) {
98 sapply(x, function(i) if (!is.null(i)) as.character(i) else "NULL")
99 }
100
101 .cat_units_table <- function(x) {
102 cat("Mixed units: ")
103 if (!length(x)) return()
104 x <- as.character(units(x))
105 u <- unique(x)
106 n <- tabulate(factor(x, u))
107 cat(paste(u, " (", n, ")", sep = "", collapse = ", "), "\n")
108 }
109
110 #' @export
111 print.mixed_units = function(x, ...) {
112 .cat_units_table(x)
113 cat(paste(format(x, ...), collapse = ", "), "\n")
114 invisible(x)
115 }
116
117 #' @export
118 str.mixed_units = function(object, ...) {
119 .cat_units_table(object)
120 cat(capture.output(str(unclass(object), ...))[-1], sep="\n")
121 }
122
123 #' @name drop_units
124 #' @export
125 drop_units.mixed_units = function(x) {
126 sapply(x, drop_units)
127 }
128
129 #' @export
130 Ops.mixed_units = function(e1, e2) {
131 if (inherits(e2, "units"))
132 e2 = mixed_units(e2)
133 ret = switch(.Generic,
134 "==" = mapply(function(x, y) { x == y }, e1, e2, SIMPLIFY = TRUE),
135 "!=" = mapply(function(x, y) { x != y }, e1, e2, SIMPLIFY = TRUE),
136 "*" = mapply(function(x, y) { x * y }, e1, e2, SIMPLIFY = FALSE),
137 "/" = mapply(function(x, y) { x / y }, e1, e2, SIMPLIFY = FALSE),
138 "+" = mapply(function(x, y) { x + y }, e1, e2, SIMPLIFY = FALSE),
139 "-" = mapply(function(x, y) { x - y }, e1, e2, SIMPLIFY = FALSE),
140 stop(paste("operation", .Generic, "not supported"))
141 )
142 if (is.list(ret))
143 ret = .as.mixed_units(ret)
144 ret
145 }
146
147 #' @export
148 unique.mixed_units <- function(x, incomparables = FALSE, ...) {
149 .as.mixed_units(NextMethod())
150 }
151
152 #' @export
153 as.data.frame.mixed_units <- as.data.frame.AsIs