diff --git a/R/helpers.R b/R/helpers.R index 67963a6..5c0556f 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -19,7 +19,12 @@ #' #' @export keep_units <- function(FUN, x, ..., unit=units(x)) { - set_units(do.call(FUN, list(x, ...)), unit, mode="standard") + if (inherits(try(unit, silent = TRUE), "symbolic_units")) { + set_units(do.call(FUN, list(x, ...)), unit, mode = "standard") + } else { + warning("`x` does not have units.") + do.call(FUN, list(x, ...)) + } } dfapply <- function(X, FUN, ...) { diff --git a/tests/testthat/test_helpers.R b/tests/testthat/test_helpers.R index 37b1d33..4bfff58 100644 --- a/tests/testthat/test_helpers.R +++ b/tests/testthat/test_helpers.R @@ -3,3 +3,18 @@ test_that("keep_units restores units", { expect_identical(x, keep_units(drop_units, x)) }) + +test_that("keep_units warns when no units are provided", { + x <- 1:5 + + expect_warning(keep_units(sum, x)) + + expect_identical(suppressWarnings(keep_units(sum, x)), sum(x)) +}) + +test_that("keep_units sets user-provided units", { + rate <- set_units(3, "1/min") + x <- keep_units(rexp, 3, rate, unit=units(1/rate)) + expect_identical(units(x), units(1/rate)) + +})