From 8adef995dd37564bbf5a3338267f9068ceaf5e5c Mon Sep 17 00:00:00 2001 From: Kelly Sovacool Date: Fri, 22 Oct 2021 18:13:35 -0400 Subject: [PATCH 1/4] Check whether 2 numeric vectors are close_enough() Really just a shortcut to all(dplyr::near(x, y, tol=10^-3) for less precision. --- R/rmd_helpers.R | 15 ------------- R/utils.R | 35 +++++++++++++++++++++++++++++++ tests/testthat/test-rmd_helpers.R | 9 -------- tests/testthat/test-utils.R | 16 ++++++++++++++ 4 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 tests/testthat/test-utils.R 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 e563b6a..630852a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -7,6 +7,41 @@ dplyr::`%>%` ## See: \url{https://github.com/tidyverse/magrittr/issues/29} utils::globalVariables(c(".")) +#' 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 +} + +#' Check whether two numeric vectors are close enough for gov't work. +#' +#' This 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 +#' +#' @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/tests/testthat/test-rmd_helpers.R b/tests/testthat/test-rmd_helpers.R index 7efd5cd..0e8c61a 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..bf0cd2d --- /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) +}) From 91fbdb85a6c4ea6b473b231f011da3b4108c8eaa Mon Sep 17 00:00:00 2001 From: Kelly Sovacool Date: Mon, 25 Oct 2021 14:06:17 -0400 Subject: [PATCH 2/4] Add author --- R/utils.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/utils.R b/R/utils.R index 321d281..eae7e3a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -4,6 +4,7 @@ #' #' @return `TRUE` or `FALSE` #' @export +#' @author Kelly Sovacool \email{sovacool@@umich.edu} #' #' @examples #' is_nearly_whole(.Machine$double.eps^0.5) @@ -23,6 +24,7 @@ is_nearly_whole <- function(x) { #' #' @return `TRUE` if all numbers are near enough within the tolerance, otherwise `FALSE` #' @export +#' @author Kelly Sovacool \email{sovacool@@umich.edu} #' #' @examples #' From 3e7ee1e2f362d014f6d002dd440afaf251f166fb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 25 Oct 2021 18:09:40 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=8E=A8=20Style=20R=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-utils.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index bf0cd2d..e48f2f9 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -9,8 +9,8 @@ test_that("is_nearly_whole() works", { }) 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) + 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) }) From 7d96e40feb73ea2886824ada5ced3e06c3425d29 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 25 Oct 2021 18:10:06 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=91=20Build=20docs=20site?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/pkgdown.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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