This function checks whether a string or character vector (of length 1), a list or any vector (numeric, atomic) is empty or not.

is_empty(x, first.only = TRUE, all.na.empty = TRUE)

Arguments

x

String, character vector, list, data.frame or numeric vector or factor.

first.only

Logical, if FALSE and x is a character vector, each element of x will be checked if empty. If TRUE, only the first element of x will be checked.

all.na.empty

Logical, if x is a vector with NA-values only, is_empty will return FALSE if all.na.empty = FALSE, and will return TRUE if all.na.empty = TRUE (default).

Value

Logical, TRUE if x is a character vector or string and is empty, TRUE if x is a vector or list and of length 0, FALSE otherwise.

Note

NULL- or NA-values are also considered as "empty" (see 'Examples') and will return TRUE, unless all.na.empty==FALSE.

Examples

is_empty("test")
#> [1] FALSE
is_empty("")
#> [1] TRUE
is_empty(NA)
#> [1] TRUE
is_empty(NULL)
#> [1] TRUE

# string is not empty
is_empty(" ")
#> [1] FALSE

# however, this trimmed string is
is_empty(trim(" "))
#> [1] TRUE

# numeric vector
x <- 1
is_empty(x)
#> [1] FALSE
x <- x[-1]
is_empty(x)
#> [1] TRUE

# check multiple elements of character vectors
is_empty(c("", "a"))
#> [1] TRUE
is_empty(c("", "a"), first.only = FALSE)
#> [1]  TRUE FALSE

# empty data frame
d <- data.frame()
is_empty(d)
#> [1] TRUE

# empty list
is_empty(list(NULL))
#> [1] TRUE

# NA vector
x <- rep(NA,5)
is_empty(x)
#> [1] TRUE
is_empty(x, all.na.empty = FALSE)
#> [1] FALSE