Skip to content

Commit

Permalink
Merge pull request #127 from mlr-org/check_ops
Browse files Browse the repository at this point in the history
Move `%check&&%` and `%check||%` from `mlr3pipelines`
  • Loading branch information
advieser authored Dec 18, 2024
2 parents 5c4ab04 + ee257d0 commit a3aea69
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Collate:
'calculate_hash.R'
'capitalize.R'
'catn.R'
'check_operators.R'
'check_packages_installed.R'
'chunk.R'
'compose.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ S3method(remove_named,default)
S3method(remove_named,environment)
S3method(strip_srcrefs,"function")
S3method(strip_srcrefs,default)
export("%check&&%")
export("%check||%")
export("%nin%")
export("get_private<-")
export(Callback)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mlr3misc (development version)

* feat: `as_callbacks()` returns a list named by the callback ids now.
* feat: Added logical operators `%check&&%` and `%check||%` for `check_*`-functions from `checkmate` (moved here from `mlr3pipelines`).

# mlr3misc 0.16.0

Expand Down
35 changes: 35 additions & 0 deletions R/check_operators.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#' @title Logical Check Operators
#'
#' @description
#' Logical AND and OR operators for `check_*`-functions from [`checkmate`][`checkmate::checkmate`].
#'
#' @param lhs,rhs (`function()`)\cr
#' `check_*`-functions that return either `TRUE` or an error message as a `character(1)`.
#'
#' @return Either `TRUE` or a `character(1)`.
#'
#' @name check_operators
#' @examples
#' library(checkmate)
#'
#' x = c(0, 1, 2, 3)
#' check_numeric(x) %check&&% check_names(names(x), "unnamed") # is TRUE
#' check_numeric(x) %check&&% check_true(all(x < 0)) # fails
#'
#' check_numeric(x) %check||% check_character(x) # is TRUE
#' check_number(x) %check||% check_flag(x) # fails
NULL

#' @export
#' @rdname check_operators
`%check&&%` = function(lhs, rhs) {
if (!isTRUE(lhs) && !isTRUE(rhs)) return(paste0(lhs, ", and ", rhs))
if (isTRUE(lhs)) rhs else lhs
}

#' @export
#' @rdname check_operators
`%check||%` = function(lhs, rhs) {
if (!isTRUE(lhs) && !isTRUE(rhs)) return(paste0(lhs, ", or ", rhs))
TRUE
}
32 changes: 32 additions & 0 deletions man/check_operators.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test_check_operators.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("check operators", {
# AND operator
expect_true(TRUE %check&&% TRUE)
expect_equal(TRUE %check&&% "error", "error")
expect_equal("error" %check&&% TRUE, "error")
expect_equal("error1" %check&&% "error2", "error1, and error2")

# OR operator
expect_true(TRUE %check||% TRUE)
expect_true(TRUE %check||% "error")
expect_true("error" %check||% TRUE)
expect_equal("error1" %check||% "error2", "error1, or error2")
})

0 comments on commit a3aea69

Please sign in to comment.