Skip to contents

check_empty() validates if the provided matrix is empty, raising an error if it is not. check_is_empty() determines whether the matrix is empty and returns a logical value.

check_not_empty() and check_is_not_empty() functions check whether the provided data is not empty. The check_not_empty() function raises an error message if the data is empty, while check_is_not_empty() returns TRUE if the data is not empty and FALSE if it is empty.

Usage

check_empty(dat, par_name = "dat")

check_is_empty(dat)

check_not_empty(dat, par_name = "dat")

check_is_not_empty(dat)

Arguments

dat

A data structure (data frame, tibble, matrix, or vector) to check for emptiness.

par_name

An optional name of the parameter to display in error messages.

Value

  • check_empty(): Returns the original matrix if it is empty or raises an error otherwise.

  • check_is_empty(): Logical value, TRUE if the matrix is empty, and FALSE otherwise.

  • check_not_empty(): Returns the original data entered if it is not empty. Raises an error message if the data is empty.

  • check_is_not_empty(): Logical value, TRUE if the data is not empty, and FALSE if it is empty.

Examples

# Examples for check_empty()
# --------------------------

mat1 <- matrix(nrow = 0, ncol = 0)
print(mat1)
#> <0 x 0 matrix>
check_empty(mat1)
#> <0 x 0 matrix>

mat2 <- matrix(nrow = 0, ncol = 5)
print(mat2)
#>      [,1] [,2] [,3] [,4] [,5]
check_empty(mat2)
#>      [,1] [,2] [,3] [,4] [,5]

mat3 <- matrix(nrow = 3, ncol = 0)
print(mat3)
#>     
#> [1,]
#> [2,]
#> [3,]
check_empty(mat3)
#>     
#> [1,]
#> [2,]
#> [3,]

mat4 <- matrix(1:4, nrow = 2)
print(mat4)
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
try(check_empty(mat4))
#> Error in check_empty(mat4) : Expected 'dat' to be empty

mat5 <- matrix(NA, nrow = 3, ncol = 3)
print(mat5)
#>      [,1] [,2] [,3]
#> [1,]   NA   NA   NA
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA
try(check_empty(mat5))
#> Error in check_empty(mat5) : Expected 'dat' to be empty

# Examples for check_is_empty()
# -----------------------------

check_is_empty(mat1)
#> [1] TRUE

check_is_empty(mat2)
#> [1] TRUE

check_is_empty(mat3)
#> [1] TRUE

check_is_empty(mat4)
#> [1] FALSE

check_is_empty(mat5)
#> [1] FALSE

# Examples for check_not_empty()
# ------------------------------

check_not_empty(c(1, 2, 3))
#> [1] 1 2 3

try(check_not_empty(c()))
#> Error in check_not_empty(c()) : 
#>   Expected 'dat' to be a DataFrame, tibble, matrix, or vector but got NULL

mat1 <- matrix(1:6, nrow = 2)
check_not_empty(mat1)
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]    2    4    6

try(check_not_empty(matrix(nrow = 0, ncol = 0)))
#> Error in check_not_empty(matrix(nrow = 0, ncol = 0)) : 
#>   Argument 'dat' cannot be empty

df1 <- data.frame(a = 1:3, b = c(4, 5, 6))
check_not_empty(df1)
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6

try(check_not_empty(data.frame()))
#> Error in check_not_empty(data.frame()) : Argument 'dat' cannot be empty

# Examples for check_is_not_empty()
# ---------------------------------

check_is_not_empty(c(1, 2, 3))
#> [1] TRUE

check_is_not_empty(c())
#> [1] TRUE

mat1 <- matrix(1:6, nrow = 2)
check_is_not_empty(mat1)
#> [1] TRUE

check_is_not_empty(matrix(nrow = 0, ncol = 0))
#> [1] TRUE

df1 <- data.frame(a = 1:3, b = c(4, 5, 6))
check_is_not_empty(df1)
#> [1] TRUE

check_is_not_empty(data.frame())
#> [1] TRUE