Skip to content

Commit

Permalink
export ud are convertible (#263)
Browse files Browse the repository at this point in the history
* export ud_are_convertible

* fix review remarks

* minor changes

Co-authored-by: Christophe_Regouby <[email protected]>
Co-authored-by: Iñaki Úcar <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2020
1 parent 3648766 commit 67c608f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export(make_units)
export(mixed_units)
export(remove_symbolic_unit)
export(set_units)
export(ud_are_convertible)
export(unitless)
export(units_options)
export(valid_udunits)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* add `load_units_xml` to enable database reloading as well as loading
user-provided unit systems; #254 addressing #243, #244

* export `ud_are_convertible`; #263 addressing #258 @cregouby

* remove deprecations: `as.units`, `as_cf`, `make_unit`, `parse_unit`; #259

* remove deprecated pre-computed `ud_units` database; #259
Expand Down
25 changes: 22 additions & 3 deletions R/udunits.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
ud_are_convertible = function(u1, u2) {
res <- try(R_ut_are_convertible(R_ut_parse(as.character(u1)), R_ut_parse(as.character(u2))),
silent = TRUE)
#' Test if two units are convertible
#'
#' Parses and checks whether units can be converted by UDUNITS-2. Units may not
#' be convertible either because they are different magnitudes or because one
#' (or both) units are not defined in the database.
#'
#' @param x character or object of class \code{symbolic_units}, for the symbol
#' of the first unit.
#' @param y character or object of class \code{symbolic_units}, for the symbol
#' of the second unit.
#'
#' @return boolean, \code{TRUE} if both units exist and are convertible.
#' @export
#'
#' @examples
#' ud_are_convertible("m", "km")
#' a <- set_units(1:3, m/s)
#' ud_are_convertible(units(a), "km/h")
#' ud_are_convertible("s", "kg")
ud_are_convertible = function(x, y) {
res <- try(R_ut_are_convertible(
R_ut_parse(as.character(x)), R_ut_parse(as.character(y))), silent = TRUE)
! inherits(res, "try-error") && res
}

Expand Down
29 changes: 29 additions & 0 deletions man/ud_are_convertible.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,13 @@ test_that("NA as units generate warnings", {
expect_error(set_units(NA_real_, NA_character_, mode="standard"), "a missing value for units is not allowed")
expect_error(set_units(NA_real_, NA, mode="standard"), "a missing value for units is not allowed")
})

test_that("ud_are_convertible return the expected value", {
x <- 1:10 * as_units("m")
expect_is(ud_are_convertible("m", "km"), "logical")
expect_true(ud_are_convertible("m", "km"))
expect_true(ud_are_convertible(units(x), "km"))
expect_false(ud_are_convertible("s", "kg"))

})

0 comments on commit 67c608f

Please sign in to comment.