Debian Codegraph @

R/misc.R

raw
1 #' @export
2 `[<-.units` <- function(x, ..., value) {
3 units(value) <- units(x)
4 NextMethod()
5 }
6
7 #' @export
8 `[[<-.units` <- `[<-.units`
9
10 #' @export
11 c.units <- function(..., recursive = FALSE, allow_mixed = units_options("allow_mixed")) {
12 args <- list(...)
13 args[sapply(args, is.null)] <- NULL # remove NULLs
14 u <- units(args[[1]])
15
16 if (length(args) == 1)
17 return(.as.units(NextMethod(), u))
18
19 dup <- vapply(args, function(x) identical(units(x), u), TRUE)
20
21 if (all(dup))
22 return(.as.units(do.call(c, lapply(args, drop_units)), u))
23
24 if (.units_are_convertible(args[!dup], u)) {
25 args[!dup] <- lapply(args[!dup], set_units, u, mode = "standard")
26 return(.as.units(do.call(c, lapply(args, drop_units)), u))
27 }
28
29 if (allow_mixed)
30 return(do.call(c, lapply(args, mixed_units)))
31
32 stop("units are not convertible, and cannot be mixed; try setting units_options(allow_mixed = TRUE)?")
33 }
34
35 .units_are_convertible = function(x, u) {
36 u <- ud_char(u)
37 for (i in seq_along(x))
38 if (! ud_are_convertible(units(x[[i]]), u))
39 return(FALSE)
40 TRUE
41 }
42
43 #' @export
44 diff.units = function(x, ...) {
45 u = units(x)
46 # units(x) = u will not work here, as units(x) is NULL!
47 .as.units(NextMethod(), u)
48 }
49
50 #' @export
51 rep.units = function(x, ...) {
52 u = units(x)
53 .as.units(NextMethod(), u)
54 }
55
56
57 #' deparse unit to string in product power form (e.g. km m-2 s-1)
58 #'
59 #' deparse unit to string in product power form (e.g. km m-2 s-1)
60 #' @param x object of class units
61 #' @return length one character vector
62 #' @examples
63 #' u = as_units("kg m-2 s-1")
64 #' u
65 #' deparse_unit(u)
66 #' @export
67 deparse_unit = function(x) {
68 stopifnot(inherits(x, "units"))
69 as.character(units(x), neg_power=TRUE, prod_sep=" ")
70 }
71 # This should perhaps be an option in format.symbolic_units
72
73 #' @method all.equal units
74 #' @export
75 all.equal.units = function(target, current, ...) {
76 if (inherits(current, "units")) {
77 units(current) <- units(target)
78 target <- drop_units(target)
79 current <- drop_units(current)
80 }
81 NextMethod()
82 }
83
84 #' seq method for units objects
85 #' @param from see \link[base]{seq}
86 #' @param to see \link[base]{seq}
87 #' @param by see \link[base]{seq}
88 #' @param length.out see \link[base]{seq}
89 #' @param along.with see \link[base]{seq}
90 #' @param ... see \link[base]{seq}
91 #' @details arguments with units are converted to have units of the first argument (which is either \code{from} or \code{to})
92 #' @export
93 #' @examples
94 #' seq(to = set_units(10, m), by = set_units(1, m), length.out = 5)
95 #' seq(set_units(10, m), by = set_units(1, m), length.out = 5)
96 #' seq(set_units(10, m), set_units(19, m))
97 #' seq(set_units(10, m), set_units(.1, km), set_units(10000, mm))
98 seq.units = function(from, to, by = ((to - from)/(length.out - 1)),
99 length.out = NULL, along.with = NULL, ...) {
100 mf = missing(from)
101 mt = missing(to)
102 uuu = if (mf)
103 units(to)
104 else
105 units(from)
106 if (! mf)
107 from = as.numeric(from)
108 if (! mt)
109 to = as.numeric(set_units(to, uuu, mode = "standard"))
110 if (! missing(by))
111 by = as.numeric(set_units(by, uuu, mode = "standard"))
112 set_units(NextMethod(), uuu, mode = "standard")
113 }
114
115 #' @export
116 str.units = function(object, ...) {
117 gr <- units_options("group")
118 unit_string <- paste0(gr[1], as.character(attr(object, "units")), gr[2])
119 cat(paste0(" Units: ", unit_string))
120 str(drop_units(object), ...)
121 }
122
123 #' @export
124 duplicated.units <- function(x, incomparables=FALSE, ...) {
125 if (is.null(dim(x)))
126 NextMethod() else duplicated.array(x, incomparables, ...)
127 }
128
129 #' @export
130 anyDuplicated.units <- function(x, incomparables=FALSE, ...) {
131 if (is.null(dim(x)))
132 NextMethod() else anyDuplicated.array(x, incomparables, ...)
133 }
134
135 #' @export
136 unique.units <- function(x, incomparables = FALSE, ...) {
137 xx <- if (is.null(dim(x)))
138 NextMethod() else unique.array(x, incomparables, ...)
139 .as.units(xx, units(x))
140 }
141
142 #' Combine R Objects by Rows or Columns
143 #'
144 #' S3 methods for \code{units} objects (see \code{\link[base]{cbind}}).
145 #'
146 #' @inheritParams base::cbind
147 #' @name cbind.units
148 #'
149 #' @examples
150 #' x <- set_units(1, m/s)
151 #' y <- set_units(1:3, m/s)
152 #' z <- set_units(8:10, m/s)
153 #' (m <- cbind(x, y)) # the '1' (= shorter vector) is recycled
154 #' (m <- cbind(m, z)[, c(1, 3, 2)]) # insert a column
155 #' (m <- rbind(m, z)) # insert a row
156 #' @export
157 cbind.units <- function(..., deparse.level = 1) {
158 dots <- .deparse(list(...), substitute(list(...)), deparse.level)
159 units_first_arg <- units(dots[[1]])
160 class_first_arg <- class(dots[[1]])
161 dots <- lapply(dots, function(x) {
162 dots_unified <- set_units(x, units_first_arg, mode = "standard")
163 ret <- drop_units(dots_unified)
164 return(ret)
165 })
166 call <- as.character(match.call()[[1]])
167 value <- do.call(call, c(dots, deparse.level=deparse.level))
168 attr(value, "units") <- units_first_arg
169 class(value) <- class_first_arg
170 return(value)
171 }
172
173 .deparse <- function(dots, symarg, deparse.level) {
174 deparse.level <- as.integer(deparse.level)
175 if (identical(deparse.level, -1L)) deparse.level <- 0L # R Core's hack
176 stopifnot(0 <= deparse.level, deparse.level <= 2)
177
178 nm <- c( ## 0:
179 function(i) NULL,
180 ## 1:
181 function(i) if(is.symbol(s <- symarg[[i]])) deparse(s) else NULL,
182 ## 2:
183 function(i) deparse(symarg[[i]])[[1L]])[[ 1L + deparse.level ]]
184 Nms <- function(i) { if(!is.null(s <- names(symarg)[i]) && nzchar(s)) s else nm(i) }
185
186 symarg <- as.list(symarg)[-1L]
187 dnames <- sapply(seq_along(dots), Nms)
188 if (!all(sapply(dnames, is.null)))
189 names(dots) <- dnames
190 dots
191 }
192
193 #' @rdname cbind.units
194 #' @export
195 rbind.units <- cbind.units