Skip to content

Commit

Permalink
u() / du(): allow creation and dropping of dimensionless "units".
Browse files Browse the repository at this point in the history
  • Loading branch information
lschneiderbauer committed Dec 28, 2024
1 parent 2b6784e commit 3952618
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions R/units.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' u(100, "m") > u(1, "km")
#'
#' @export
u <- function(x, unit) {
u <- function(x, unit = "1") {
stopifnot(is.numeric(x))
stopifnot(is.character(unit))
# x must be a pure number
Expand Down Expand Up @@ -46,9 +46,12 @@ u <- function(x, unit) {
#' # from x in units of "meter"
#' du(x, "m")
#'
#' # drop the unit of a dimensionaless "unit" quantity
#' du(x / x)
#'
#' @seealso [units::drop_units()]
#' @export
du <- function(x, unit) {
du <- function(x, unit = "1") {
stopifnot(inherits(x, "units"))

if (has_comp_unit(x, unit)) {
Expand Down
5 changes: 4 additions & 1 deletion man/du.Rd

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

2 changes: 1 addition & 1 deletion man/u.Rd

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

2 changes: 2 additions & 0 deletions tests/testthat/test-units.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ test_that("du() works", {

expect_equal(du(x, "m"), 1000)
expect_error(du(x, "Hz"))

expect_equal(du(x/x), 1)
})

0 comments on commit 3952618

Please sign in to comment.