Check if an object is a vector or matrix
Source:R/check_vector_or_matrix.R
check-vector-or-matrix.Rd
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)
Value
check_vector_or_matrix()
: Returns the originalvmat
if it is a valid vector or matrix. Raises an error message ifvmat
is neither a valid vector nor a matrix.check_is_vector_or_matrix()
: Logical value,TRUE
ifvmat
is a valid vector or matrix, andFALSE
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