Skip to content

Commit a3aea69

Browse files
authored
Merge pull request #127 from mlr-org/check_ops
Move `%check&&%` and `%check||%` from `mlr3pipelines`
2 parents 5c4ab04 + ee257d0 commit a3aea69

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Collate:
4848
'calculate_hash.R'
4949
'capitalize.R'
5050
'catn.R'
51+
'check_operators.R'
5152
'check_packages_installed.R'
5253
'chunk.R'
5354
'compose.R'

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ S3method(remove_named,default)
3737
S3method(remove_named,environment)
3838
S3method(strip_srcrefs,"function")
3939
S3method(strip_srcrefs,default)
40+
export("%check&&%")
41+
export("%check||%")
4042
export("%nin%")
4143
export("get_private<-")
4244
export(Callback)

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# mlr3misc (development version)
22

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

56
# mlr3misc 0.16.0
67

R/check_operators.R

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#' @title Logical Check Operators
2+
#'
3+
#' @description
4+
#' Logical AND and OR operators for `check_*`-functions from [`checkmate`][`checkmate::checkmate`].
5+
#'
6+
#' @param lhs,rhs (`function()`)\cr
7+
#' `check_*`-functions that return either `TRUE` or an error message as a `character(1)`.
8+
#'
9+
#' @return Either `TRUE` or a `character(1)`.
10+
#'
11+
#' @name check_operators
12+
#' @examples
13+
#' library(checkmate)
14+
#'
15+
#' x = c(0, 1, 2, 3)
16+
#' check_numeric(x) %check&&% check_names(names(x), "unnamed") # is TRUE
17+
#' check_numeric(x) %check&&% check_true(all(x < 0)) # fails
18+
#'
19+
#' check_numeric(x) %check||% check_character(x) # is TRUE
20+
#' check_number(x) %check||% check_flag(x) # fails
21+
NULL
22+
23+
#' @export
24+
#' @rdname check_operators
25+
`%check&&%` = function(lhs, rhs) {
26+
if (!isTRUE(lhs) && !isTRUE(rhs)) return(paste0(lhs, ", and ", rhs))
27+
if (isTRUE(lhs)) rhs else lhs
28+
}
29+
30+
#' @export
31+
#' @rdname check_operators
32+
`%check||%` = function(lhs, rhs) {
33+
if (!isTRUE(lhs) && !isTRUE(rhs)) return(paste0(lhs, ", or ", rhs))
34+
TRUE
35+
}

man/check_operators.Rd

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_check_operators.R

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
test_that("check operators", {
2+
# AND operator
3+
expect_true(TRUE %check&&% TRUE)
4+
expect_equal(TRUE %check&&% "error", "error")
5+
expect_equal("error" %check&&% TRUE, "error")
6+
expect_equal("error1" %check&&% "error2", "error1, and error2")
7+
8+
# OR operator
9+
expect_true(TRUE %check||% TRUE)
10+
expect_true(TRUE %check||% "error")
11+
expect_true("error" %check||% TRUE)
12+
expect_equal("error1" %check||% "error2", "error1, or error2")
13+
})

0 commit comments

Comments
 (0)