Skip to content

Commit

Permalink
Merge pull request #27 from poissonconsulting/ifelse
Browse files Browse the repository at this point in the history
Added `error = FALSE` argument to `if_else2()`
  • Loading branch information
aylapear authored Dec 2, 2024
2 parents 277cede + 04fe63d commit e7bfde6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<!-- NEWS.md is maintained by https://fledge.cynkra.com, contributors should not edit this file -->

# 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.
Expand Down
6 changes: 4 additions & 2 deletions R/if-else2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,8 +35,9 @@
#' 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) {
chk_flag(error)
if (!any(condition) & error == TRUE) {
stop("No matches found. Did not make any replacements.")
}
dplyr::if_else(condition, true, false, missing = false)
Expand Down
4 changes: 3 additions & 1 deletion man/if_else2.Rd

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

20 changes: 18 additions & 2 deletions tests/testthat/test-if-else2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})

0 comments on commit e7bfde6

Please sign in to comment.