Skip to content

Commit

Permalink
Add 'is_na' criterion. 'when' now accepts any values and try to conve…
Browse files Browse the repository at this point in the history
…rt it to criterion
  • Loading branch information
gdemin committed Nov 15, 2020
1 parent 106af9b commit 849bc4b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ export(is.labelled)
export(is.with_caption)
export(is_max)
export(is_min)
export(is_na)
export(items)
export(keep)
export(key)
Expand Down
15 changes: 13 additions & 2 deletions R/criteria_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ thru = function(lower, upper){
#' @rdname criteria
when = function(x) {
if(is.criterion(x)) return(x)
if(is.function(x)) return(as.criterion(x))
is.logical(x) || stop("'when' - argument should be logical, criterion or function.")
if(!is.logical(x)) return(as.criterion(x))

x = x & !is.na(x) # always FALSE when NA
as.criterion(function(y) {
Expand Down Expand Up @@ -439,6 +438,18 @@ not_na = function(x){

class(not_na) = union("criterion", class(not_na))

#' @export
#' @rdname criteria
is_na = function(x){
if(missing(x)){
is_na
} else {
is.na(x)
}
}

class(is_na) = union("criterion", class(is_na))

#' @export
#' @rdname criteria
other = function(x){
Expand Down
3 changes: 3 additions & 0 deletions man/criteria.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test_criteria_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ logi_crit = when(c(TRUE, FALSE, FALSE, TRUE))
expect_identical(logi_crit(1:4), c(TRUE, FALSE, FALSE, TRUE))
expect_identical(when(is.numeric)(c(1,2,3)), TRUE)
expect_identical(when(is.numeric)(TRUE), FALSE)
expect_identical(when(1:2)(1:3), c(TRUE, TRUE, FALSE))
expect_identical(when(not_na)(1), TRUE)
expect_identical(when(not_na)(NA), FALSE)

expect_identical(when(is_na)(1), FALSE)
expect_identical(when(is_na)(NA), TRUE)
expect_identical(when(is_na())(1), FALSE)
expect_identical(when(is_na())(NA), TRUE)
expect_identical(is_na(1), FALSE)
expect_identical(is_na(NA), TRUE)

fun_crit = as.criterion(function(x) x>2)

Expand Down

0 comments on commit 849bc4b

Please sign in to comment.