Skip to content

Move %check&&% and %check||% from mlr3pipelines #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
Loading