diff --git a/DESCRIPTION b/DESCRIPTION index cab3d0d..3babe95 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: units -Version: 0.8-7.0 +Version: 0.8-7.1 Title: Measurement Units for R Vectors Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.pebesma@uni-muenster.de", comment = c(ORCID = "0000-0001-8049-7069")), person("Thomas", "Mailund", role = "aut", email = "mailund@birc.au.dk"), diff --git a/NEWS.md b/NEWS.md index 8a91336..93544e8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # version devel +* Vectorize `ud_*()` helpers; #405 addressing #404 + # version 0.8-7 * Deep copy of `ud_convert()` input to avoid side effects; #403 diff --git a/R/udunits.R b/R/udunits.R index 9729b0c..3fd09a3 100644 --- a/R/udunits.R +++ b/R/udunits.R @@ -3,11 +3,10 @@ #' Some \pkg{udunits2} utilities are exposed to the user. These functions are #' useful for checking whether units are convertible or converting between units #' without having to create \pkg{units} objects. +#' Arguments are recycled if necessary. #' -#' @param from character or object of class \code{symbolic_units}, -#' for the symbol of the original unit. -#' @param to character or object of class \code{symbolic_units}, -#' for the symbol of the unit to convert. +#' @param from,to character vector or object of class \code{symbolic_units}, +#' for the symbol(s) of the original unit(s) and the unit to convert to respectively. #' @param ... unused. #' #' @return \code{ud_are_convertible} @@ -23,7 +22,7 @@ ud_are_convertible <- function(from, to, ...) { warning("variables `x` and `y` were unfortunate names, and are deprecated", "; please use `from` and `to` instead") } - ud_convertible(ud_char(from), ud_char(to)) + mapply(ud_convertible, ud_char(from), ud_char(to), USE.NAMES=FALSE) } #' @param x numeric vector @@ -35,8 +34,8 @@ ud_are_convertible <- function(from, to, ...) { #' @export #' #' @examples -#' ud_are_convertible("m", "km") -#' ud_convert(100, "m", "km") +#' ud_are_convertible(c("m", "mm"), "km") +#' ud_convert(c(100, 100000), c("m", "mm"), "km") #' #' a <- set_units(1:3, m/s) #' ud_are_convertible(units(a), "km/h") @@ -45,7 +44,7 @@ ud_are_convertible <- function(from, to, ...) { #' ud_are_convertible("degF", "degC") #' ud_convert(32, "degF", "degC") ud_convert <- function(x, from, to) { - ud_convert_doubles(x, ud_char(from), ud_char(to)) + mapply(ud_convert_doubles, x, ud_char(from), ud_char(to)) } ud_char <- function(x) { diff --git a/man/udunits2.Rd b/man/udunits2.Rd index 4a31e66..a1f0e37 100644 --- a/man/udunits2.Rd +++ b/man/udunits2.Rd @@ -11,11 +11,8 @@ ud_are_convertible(from, to, ...) ud_convert(x, from, to) } \arguments{ -\item{from}{character or object of class \code{symbolic_units}, -for the symbol of the original unit.} - -\item{to}{character or object of class \code{symbolic_units}, -for the symbol of the unit to convert.} +\item{from, to}{character vector or object of class \code{symbolic_units}, +for the symbol(s) of the original unit(s) and the unit to convert to respectively.} \item{...}{unused.} @@ -33,10 +30,11 @@ returns a numeric vector with \code{x} converted to new unit. Some \pkg{udunits2} utilities are exposed to the user. These functions are useful for checking whether units are convertible or converting between units without having to create \pkg{units} objects. +Arguments are recycled if necessary. } \examples{ -ud_are_convertible("m", "km") -ud_convert(100, "m", "km") +ud_are_convertible(c("m", "mm"), "km") +ud_convert(c(100, 100000), c("m", "mm"), "km") a <- set_units(1:3, m/s) ud_are_convertible(units(a), "km/h") diff --git a/tests/testthat/test_udunits.R b/tests/testthat/test_udunits.R index 3b5a9a0..4c6fe3d 100644 --- a/tests/testthat/test_udunits.R +++ b/tests/testthat/test_udunits.R @@ -4,6 +4,12 @@ test_that("ud_are_convertible return the expected value", { expect_true(ud_are_convertible("m", "km")) expect_true(ud_are_convertible(units(x), "km")) expect_false(ud_are_convertible("s", "kg")) + + x <- c("m", "l") + y <- c("km", "ml", "cm", "kg") + conv <- c(TRUE, TRUE, TRUE, FALSE) + expect_equal(ud_are_convertible(x, y), conv) + expect_equal(ud_are_convertible(y, x), conv) }) test_that("ud_convert works with simple conversions", { @@ -16,7 +22,7 @@ test_that("ud_convert works with simple conversions", { }) test_that("ud_convert works with vectors", { - expect_equal(ud_convert(1:2, "m", "km"), 1:2/1000) + expect_equal(ud_convert(1:2, c("m", "mm"), "km"), 1:2/c(1e3,1e6)) expect_equal(ud_convert(c(32, 212), "degF", "degC"), c(0, 100)) })