Skip to content
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(chk_all)
export(chk_all_equal)
export(chk_all_equivalent)
export(chk_all_identical)
export(chk_all_na)
export(chk_array)
export(chk_atomic)
export(chk_character)
Expand Down Expand Up @@ -113,6 +114,7 @@ export(vld_all)
export(vld_all_equal)
export(vld_all_equivalent)
export(vld_all_identical)
export(vld_all_na)
export(vld_array)
export(vld_atomic)
export(vld_character)
Expand Down
50 changes: 50 additions & 0 deletions R/chk-all-na.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' Check All Missing Values
#'
#' @description
#' Checks if all missing values using
#'
#' `all(is.na(x))`
#'
#' **Pass**: `NA`, `c(NA, NA)`, `logical(0)`.
#'
#' **Fail**: `1`, `1:2`, `"1"`, `c(1, NA)`.
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family misc_checkers
#'
#' @seealso For more details about the use of this function,
#' please read the article
#' `vignette("chk-families")`.
#'
#' @examples
#' # chk_all_na
#' try(chk_all_na(1))
#' try(chk_all_na(c(1, NA)))
#' chk_all_na(NA)
#' chk_all_na(c(NA, NA_character_, NA_real_))
#' @export
chk_all_na <- function(x, x_name = NULL) {
if (vld_all_na(x)) {
return(invisible(x))
}
if (is.null(x_name)) {
x_name <- deparse_backtick_chk((substitute(x)))
}
abort_chk(x_name, " must only have missing values", x = x)
}

#' @describeIn chk_all_na Validate All Missing Values
#'
#' @examples
#' # vld_all_na
#' vld_all_na(1)
#' vld_all_na(1:2)
#' vld_all_na(NA_real_)
#' vld_all_na(integer(0))
#' vld_all_na(c(NA, 1))
#' vld_all_na(TRUE)
#' vld_all_na(c(NA_real_, NA_character_))
#' @export
vld_all_na <- function(x) all(is.na(x))
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ reference:
- title: Quality Checkers (Miscellaneous)
desc: Check if the function input meet some user defined quality criteria
contents:
- chk_all_na
- chk_not_any_na
- chk_not_empty
- chk_unique
Expand Down
63 changes: 63 additions & 0 deletions man/chk_all_na.Rd

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

1 change: 1 addition & 0 deletions man/chk_join.Rd

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

1 change: 1 addition & 0 deletions man/chk_not_any_na.Rd

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

1 change: 1 addition & 0 deletions man/chk_not_empty.Rd

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

1 change: 1 addition & 0 deletions man/chk_unique.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-chk-all-na.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("vld_not_any_na", {
expect_false(vld_all_na(1))
expect_false(vld_all_na(matrix(1:3)))
expect_false(vld_all_na(c(1, NA)))
expect_true(vld_all_na(character(0)))
expect_true(vld_all_na(NULL))
expect_true(vld_all_na(c()))
expect_true(vld_all_na(NA))
expect_true(vld_all_na(c(NA, NA_real_, NA_character_)))
})

test_that("chk_not_any_na", {
expect_chk_error(chk_all_na(1), "`1` must only have missing values\\.")
expect_invisible(chk_all_na(NA_real_))
expect_no_error(chk_all_na(NA))
expect_chk_error(
chk_all_na("NA", x_name = 1),
"^1 must only have missing values[.]$"
)
})
7 changes: 7 additions & 0 deletions vignettes/chk-families.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,13 @@ while for a vector or list, it corresponds to the number of elements.

`chk_not_any_na` function checks if there are no NA values present in the entire object.

`chk_all_na` function checks if all values are NA in the entire object.

Function | Code
:- | :--
`chk_not_empty(x)` | `length(x) != 0L`
`chk_not_any_na(x)` | `!anyNA(x)`
`chk_all_na(x)` | `all(is.na(x))`

```{r}
vld_not_empty(c()) # FALSE
Expand All @@ -533,6 +536,10 @@ vld_not_empty(data.frame(a = 1:3, b = 4:6)) # TRUE

vld_not_any_na(data.frame(a = 1:3, b = 4:6)) # TRUE
vld_not_any_na(data.frame(a = c(1, NA, 3), b = c(4, 5, 6))) # FALSE


vld_all_na(data.frame(a = c(NA_character_, NA_character_), b = NA_real_)) # TRUE
vld_all_na(data.frame(a = c(1, NA, 3), b = c(4, 5, 6))) # FALSE
```
The `chk_unique()` function is designed to verify that there are no duplicates elements in a vector.

Expand Down
Loading