Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 7 additions & 8 deletions R/udunits.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions man/udunits2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion tests/testthat/test_udunits.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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))
})

Expand Down
Loading