-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathis.empty.R
83 lines (82 loc) · 2.92 KB
/
is.empty.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# DIZtools - Utilities for 'DIZ' R Package Development
# Copyright (C) 2020-2023 Universitätsklinikum Erlangen, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' @title Check for empty/`NA`/`NULL`/`NaN`/etc. values.
#'
#' @description Rails-inspired helper that checks if vector values are "empty",
#' i.e. if it's: \code{NULL}, zero-length, \code{NA}, \code{NaN},
#' \code{FALSE}, an empty string or \code{0}.
#' Note that unlike its native R \code{is.<something>} sibling functions,
#' \code{is.empty} is vectorised (hence the "values").
#' @param x an object to check its emptiness
#' @param trim trim whitespace? (\code{TRUE} by default)
#' @param all return overall result over list/vector instead of vector of
#' results? \code{is.empty(x, all = TRUE)} is the same like
#' \code{all(unlist(is.empty(x)))}
#' @param ... additional arguments for \code{\link{sapply}}
#' @source \href{https://github.com/Rapporter/rapportools/blob/master/R/utils.R}{Copied from `rapportools::is.empty()`}
#' @examples \dontrun{
#' is.empty(NULL) # [1] TRUE
#' is.empty(c()) # [1] TRUE
#' is.empty(NA) # [1] TRUE
#' is.empty(NaN) # [1] TRUE
#' is.empty("") # [1] TRUE
#' is.empty(0) # [1] TRUE
#' is.empty(0.00) # [1] TRUE
#' is.empty(" ") # [1] TRUE
#' is.empty("foobar") # [1] FALSE
#' is.empty(" ", trim = FALSE) # [1] FALSE
#' ## is.empty is vectorised!
#' all(is.empty(rep("", 10))) # [1] TRUE
#' all(is.empty(matrix(NA, 10, 10))) # [1] TRUE
#' is.empty(matrix(NA, 10, 10), all = TRUE)) # [1] TRUE
#' }
#' @export
is.empty <- function(x, trim = TRUE, all = FALSE, ...) {
if (length(x) <= 1) {
if (is.list(x)) {
## otherwise `x <- list(a = NULL)` causes an error:
x <- unlist(x)
}
if (is.null(x)) {
return(TRUE)
}
if (length(x) == 0) {
return(TRUE)
}
if (is.na(x) || is.nan(x)) {
return(TRUE)
}
if (is.character(x) &&
nchar(ifelse(trim, trim.space(x), x)) == 0) {
return(TRUE)
}
if (is.logical(x) && !isTRUE(x)) {
return(TRUE)
}
if (is.numeric(x) && x == 0) {
return(TRUE)
}
return(FALSE)
} else{
if (isTRUE(all)) {
all(unlist(sapply(
x, is.empty, trim = trim, all = all, ...
)))
} else {
sapply(x, is.empty, trim = trim, all = all, ...)
}
}
}