| 1 |
#' \pkg{udunits2} utilities |
| 2 |
#' |
| 3 |
#' Some \pkg{udunits2} utilities are exposed to the user. These functions are |
| 4 |
#' useful for checking whether units are convertible or converting between units |
| 5 |
#' without having to create \pkg{units} objects. |
| 6 |
#' Arguments are recycled if necessary. |
| 7 |
#' |
| 8 |
#' @param from,to character vector or object of class \code{symbolic_units}, |
| 9 |
#' for the symbol(s) of the original unit(s) and the unit to convert to respectively. |
| 10 |
#' @param ... unused. |
| 11 |
#' |
| 12 |
#' @return \code{ud_are_convertible} |
| 13 |
#' returns \code{TRUE} if both units exist and are convertible, |
| 14 |
#' \code{FALSE} otherwise. |
| 15 |
#' |
| 16 |
#' @name udunits2 |
| 17 |
#' @export |
| 18 |
ud_are_convertible <- function(from, to, ...) { |
| 19 |
if (length(dots <- list(...))) { |
| 20 |
if (exists("x", dots)) from <- dots$x |
| 21 |
if (exists("y", dots)) to <- dots$y |
| 22 |
warning("variables `x` and `y` were unfortunate names, and are deprecated", |
| 23 |
"; please use `from` and `to` instead") |
| 24 |
} |
| 25 |
mapply(ud_convertible, ud_char(from), ud_char(to), USE.NAMES=FALSE) |
| 26 |
} |
| 27 |
|
| 28 |
#' @param x numeric vector |
| 29 |
#' |
| 30 |
#' @return \code{ud_convert} |
| 31 |
#' returns a numeric vector with \code{x} converted to new unit. |
| 32 |
#' |
| 33 |
#' @name udunits2 |
| 34 |
#' @export |
| 35 |
#' |
| 36 |
#' @examples |
| 37 |
#' ud_are_convertible(c("m", "mm"), "km") |
| 38 |
#' ud_convert(c(100, 100000), c("m", "mm"), "km") |
| 39 |
#' |
| 40 |
#' a <- set_units(1:3, m/s) |
| 41 |
#' ud_are_convertible(units(a), "km/h") |
| 42 |
#' ud_convert(1:3, units(a), "km/h") |
| 43 |
#' |
| 44 |
#' ud_are_convertible("degF", "degC") |
| 45 |
#' ud_convert(32, "degF", "degC") |
| 46 |
ud_convert <- function(x, from, to) { |
| 47 |
if (!length(x)) return(x) |
| 48 |
mapply(ud_convert_doubles, x, ud_char(from), ud_char(to)) |
| 49 |
} |
| 50 |
|
| 51 |
ud_char <- function(x) { |
| 52 |
if (is.character(x)) return(x) |
| 53 |
if (!inherits(x, "symbolic_units")) stop("not a unit") |
| 54 |
|
| 55 |
# using * instead of space to workaround #414 |
| 56 |
res <- if (length(x$numerator)) |
| 57 |
paste(x$numerator, collapse="*") else "1" |
| 58 |
if (length(x$denominator)) |
| 59 |
res <- paste0(res, "*(", paste(x$denominator, collapse="*"), ")-1") |
| 60 |
res |
| 61 |
} |
| 62 |
|
| 63 |
ud_are_same <- function(x, y) { |
| 64 |
get_base <- function(x) |
| 65 |
tail(strsplit(R_ut_format(R_ut_parse(x), definition=TRUE), " ")[[1]], 1) |
| 66 |
identical(get_base(x), get_base(y)) |
| 67 |
} |
| 68 |
|
| 69 |
ud_get_symbol = function(u) { |
| 70 |
u <- R_ut_parse(u) |
| 71 |
sym = R_ut_get_symbol(u) |
| 72 |
if (!length(sym)) |
| 73 |
sym = R_ut_get_name(u) |
| 74 |
sym |
| 75 |
} |
| 76 |
|
| 77 |
ud_is_parseable = function(u) { |
| 78 |
res <- try(R_ut_parse(u), silent = TRUE) |
| 79 |
! inherits(res, "try-error") |
| 80 |
} |
| 81 |
|
| 82 |
ud_parse <- function(u, names=FALSE, definition=FALSE, ascii=FALSE) { |
| 83 |
R_ut_format(R_ut_parse(u), names, definition, ascii) |
| 84 |
} |