From fb8f21dd25eea7866826ee0107e6d6e77af78efe Mon Sep 17 00:00:00 2001 From: Duncan Kennedy Date: Wed, 20 Nov 2024 10:50:17 -0700 Subject: [PATCH 1/3] - added `error = FALSE` argument to `if_else2()` --- added more `if_else2()` tests resolves #26 --- R/if-else2.R | 5 +++-- man/if_else2.Rd | 4 +++- tests/testthat/test-if-else2.R | 20 ++++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/R/if-else2.R b/R/if-else2.R index da39494..0e73767 100644 --- a/R/if-else2.R +++ b/R/if-else2.R @@ -6,6 +6,7 @@ #' [`str_detect2()`]. #' #' @inherit dplyr::if_else +#' @param error A logical value. If `TRUE`, provides an informative error message if no matches are found. #' @return Where condition is `TRUE`, the matching value from `true`, where it's `FALSE` or `NA`, the matching value from `false`. #' @seealso [`ifelse()`] and [`dplyr::if_else()`]. #' @export @@ -34,8 +35,8 @@ #' dplyr::mutate(data, #' x3 = dplyr::if_else(str_detect2(y, "x is false"), FALSE, x) #' ) -if_else2 <- function(condition, true, false) { - if (!any(condition)) { +if_else2 <- function(condition, true, false, error = FALSE) { + if (!any(condition) & error == TRUE) { stop("No matches found. Did not make any replacements.") } dplyr::if_else(condition, true, false, missing = false) diff --git a/man/if_else2.Rd b/man/if_else2.Rd index bda04e8..96b92f3 100644 --- a/man/if_else2.Rd +++ b/man/if_else2.Rd @@ -4,7 +4,7 @@ \alias{if_else2} \title{Vectorised if else.} \usage{ -if_else2(condition, true, false) +if_else2(condition, true, false, error = FALSE) } \arguments{ \item{condition}{A logical vector} @@ -16,6 +16,8 @@ Both \code{true} and \code{false} will be \link[vctrs:theory-faq-recycling]{recy to the size of \code{condition}. \code{true}, \code{false}, and \code{missing} (if used) will be cast to their common type.} + +\item{error}{A logical value. If \code{TRUE}, provides an informative error message if no matches are found.} } \value{ Where condition is \code{TRUE}, the matching value from \code{true}, where it's \code{FALSE} or \code{NA}, the matching value from \code{false}. diff --git a/tests/testthat/test-if-else2.R b/tests/testthat/test-if-else2.R index bb995f4..f13e282 100644 --- a/tests/testthat/test-if-else2.R +++ b/tests/testthat/test-if-else2.R @@ -28,10 +28,26 @@ test_that("works with lists", { ) }) -test_that("throws an informative error when no matches are found", { +test_that("throws an informative error when error = TRUE and no matches are found", { x <- c(1, 2, 3, 4, 5) expect_error( - if_else2(x < 0, 0, x), + if_else2(x < 0, 0, x, error = TRUE), "No matches found. Did not make any replacements." ) }) + +test_that("doesn't throws an informative error when error = TRUE and matches are found", { + x <- c(-1, 2, 3, 4, 5) + expect_equal( + if_else2(x < 0, 0, x, error = TRUE), + c(0, 2, 3, 4, 5) + ) +}) + +test_that("doesn't throw an informative error when error = FALSE (the default) and no matches are found", { + x <- c(1, 2, 3, 4, 5) + expect_equal( + if_else2(x < 0, 0, x), + x + ) +}) From 6804554bd3be89a25456c1542e3dcdabf8f33aed Mon Sep 17 00:00:00 2001 From: Duncan Kennedy Date: Wed, 20 Nov 2024 12:42:22 -0700 Subject: [PATCH 2/3] Added chk for `error` in `if_else2()` Co-authored-by: Joe Thorley --- R/if-else2.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/if-else2.R b/R/if-else2.R index 0e73767..6cb1aee 100644 --- a/R/if-else2.R +++ b/R/if-else2.R @@ -36,6 +36,7 @@ #' x3 = dplyr::if_else(str_detect2(y, "x is false"), FALSE, x) #' ) if_else2 <- function(condition, true, false, error = FALSE) { + chk_flag(error) if (!any(condition) & error == TRUE) { stop("No matches found. Did not make any replacements.") } From 04fe63d01f06501797f8e4fd22fada7ac17704aa Mon Sep 17 00:00:00 2001 From: Ayla Pearson Date: Mon, 2 Dec 2024 10:47:35 -0800 Subject: [PATCH 3/3] fledge: Bump version to 0.1.0.9003 --- DESCRIPTION | 2 +- NEWS.md | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6b2e841..9ff11a3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tidyplus Title: Additional 'tidyverse' Functions -Version: 0.1.0.9002 +Version: 0.1.0.9003 Authors@R: c( person("Joe", "Thorley", , "joe@poissonconsulting.ca", role = "aut", comment = c(ORCID = "0000-0002-7683-4592")), diff --git a/NEWS.md b/NEWS.md index 6f12c3d..dc579b1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,10 +1,13 @@ +# tidyplus 0.1.0.9003 + +- Added `error = FALSE` argument to `if_else2()`. + # tidyplus 0.1.0.9002 - Added informative error message to `if_else2()` if no matches are found - # tidyplus 0.1.0.9001 - Internal changes.