Skip to contents

check_vector_or_matrix() and check_is_vector_or_matrix() are functions that check if an input is a valid vector (with at least one or two elements) or matrix. The check_vector_or_matrix() function raises an error if the input is neither a vector nor a matrix. The check_is_vector_or_matrix() returns TRUE if the input is a valid vector or matrix and FALSE otherwise.

Usage

check_vector_or_matrix(vmat, allow_scalar = TRUE, par_name = "vmat")

check_is_vector_or_matrix(vmat, allow_scalar = TRUE)

Arguments

vmat

The object to check.

allow_scalar

Boolean, indicating if a scalar value is allowed. Default is TRUE.

par_name

The name of the parameter to display in error messages.

Value

  • check_vector_or_matrix(): Returns the original vmat if it is a valid vector or matrix. Raises an error message if vmat is neither a valid vector nor a matrix.

  • check_is_vector_or_matrix(): Logical value, TRUE if vmat is a valid vector or matrix, and FALSE otherwise.

Examples

# Examples for check_vector_or_matrix()
# -------------------------------------
v <- c(1, 2, 3)
check_vector_or_matrix(v)
#> [1] 1 2 3

A <- matrix(1:9, nrow = 3, byrow = TRUE)
check_vector_or_matrix(A)
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    4    5    6
#> [3,]    7    8    9

k <- 5
check_vector_or_matrix(k)
#> [1] 5

try(check_vector_or_matrix(k, allow_scalar = FALSE))
#> Error in check_vector_or_matrix(k, allow_scalar = FALSE) : 
#>   Expected 'vmat' to be a vector with at least 2 elements or a matrix but got numeric

# Examples for check_is_vector_or_matrix()
# ----------------------------------------

check_is_vector_or_matrix(v)
#> [1] TRUE

check_is_vector_or_matrix(A)
#> [1] TRUE

check_is_vector_or_matrix(k)
#> [1] TRUE

check_is_vector_or_matrix(k, allow_scalar = FALSE)
#> [1] FALSE