diff --git a/R/rmd_helpers.R b/R/rmd_helpers.R index e791c7a..e2456e0 100644 --- a/R/rmd_helpers.R +++ b/R/rmd_helpers.R @@ -43,21 +43,6 @@ paste_oxford_list <- function(x) { return(prose) } -#' Checks whether a number is near to a whole number -#' -#' @param x a numeric -#' -#' @return `TRUE` or `FALSE` -#' @export -#' -#' @examples -#' is_nearly_whole(.Machine$double.eps^0.5) -#' is_nearly_whole(.Machine$double.eps^0.6) -#' is_nearly_whole(1) -is_nearly_whole <- function(x) { - abs(x - round(x)) < .Machine$double.eps^0.5 -} - #' Format human-readable numbers. #' #' Pastes formatted `x` if numeric, otherwise `x` unmodified. diff --git a/R/utils.R b/R/utils.R index 6a813a5..eae7e3a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,40 @@ +#' Checks whether a number is near to a whole number +#' +#' @param x a numeric +#' +#' @return `TRUE` or `FALSE` +#' @export +#' @author Kelly Sovacool \email{sovacool@@umich.edu} +#' +#' @examples +#' is_nearly_whole(.Machine$double.eps^0.5) +#' is_nearly_whole(.Machine$double.eps^0.6) +#' is_nearly_whole(1) +is_nearly_whole <- function(x) { + abs(x - round(x)) < .Machine$double.eps^0.5 +} + +#' Check whether two numeric vectors are close enough for gov't work. +#' +#' This is like `dplyr::near()` except with much less precision. +#' +#' @param x a numeric vector +#' @param y another numeric vector +#' @param tol tolerance (default: `10^-3`.) +#' +#' @return `TRUE` if all numbers are near enough within the tolerance, otherwise `FALSE` +#' @export +#' @author Kelly Sovacool \email{sovacool@@umich.edu} +#' +#' @examples +#' +#' close_enough(0.0004, 0) +#' close_enough(0.8887, 0.8884) +#' close_enough(1, 2) +close_enough <- function(x, y, tol = 10^-3) { + all(dplyr::near(x, y, tol = tol)) +} + #' Install & load packages #' @param ... package names to install & load #' @export diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 785e1ce..23817c1 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,5 +3,5 @@ pkgdown: 1.6.1 pkgdown_sha: ~ articles: introduction: introduction.html -last_built: 2021-10-25T17:27Z +last_built: 2021-10-25T18:09Z diff --git a/tests/testthat/test-rmd_helpers.R b/tests/testthat/test-rmd_helpers.R index ed394ee..536d256 100644 --- a/tests/testthat/test-rmd_helpers.R +++ b/tests/testthat/test-rmd_helpers.R @@ -5,15 +5,6 @@ test_that("paste_oxford_list() works for vectors & lists", { expect_equal(paste_oxford_list(1), "1") }) -test_that("is_nearly_whole() works", { - expect_true(is_nearly_whole(.Machine$double.eps)) - expect_true(is_nearly_whole(0)) - expect_true(is_nearly_whole(1)) - expect_false(is_nearly_whole(.Machine$double.eps^0.5)) - expect_false(is_nearly_whole(2100.05)) - expect_equal(is_nearly_whole(NA), NA) -}) - test_that("format_number() works for numbers & strings", { expect_equal(format_number(0.02), "0.02") expect_equal(format_number(.Machine$double.eps^0.5), "0.000000015") diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..e48f2f9 --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,16 @@ + +test_that("is_nearly_whole() works", { + expect_true(is_nearly_whole(.Machine$double.eps)) + expect_true(is_nearly_whole(0)) + expect_true(is_nearly_whole(1)) + expect_false(is_nearly_whole(.Machine$double.eps^0.5)) + expect_false(is_nearly_whole(2100.05)) + expect_equal(is_nearly_whole(NA), NA) +}) + +test_that("close_enough() works", { + expect_true(close_enough(0.0004, 0)) + expect_true(close_enough(0.8887, 0.8884)) + expect_false(close_enough(1, 2)) + expect_equal(close_enough(1, NA), NA) +})