Skip to contents

check_character() and check_is_character() functions verify whether an input is a character vector and that it satisfies the specified criteria check_character() raises an error if the vector does not meet the criteria, while check_is_character() returns a logical value indicating the result.

Usage

check_character(
  char,
  n = NULL,
  inequality = c("==", ">", ">=", "<", "<="),
  par_name = "char"
)

check_is_character(char, n = NULL, inequality = c("==", ">", ">=", "<", "<="))

Arguments

char

A character vector to check.

n

An optional length to compare against.

inequality

A character string specifying the inequality for length comparison. Options include "==", ">", ">=", "<", "<=".

par_name

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

Value

  • check_character(): Returns the original character vector or raises an error message if an error occurs or any of the conditions is not met.

  • check_is_character(): Logical value, TRUE if the character vector meets the specified conditions, and FALSE otherwise.

Examples

# Examples for check_character()
# ------------------------------

check_character("Hello")
#> [1] "Hello"

try(check_character("Hello", n = 10))
#> Error in check_character("Hello", n = 10) : 
#>   Expected 'char' to have exactly 10 characters but got 5 characters

check_character("Hello", n = 10, inequality = "<=")
#> [1] "Hello"

try(check_character(c("a", "b", "c")))
#> Error in check_character(c("a", "b", "c")) : 
#>   Expected 'char' to be a character vector of length 1 but got character with 3 elements

# Examples for check_is_character()
# ---------------------------------

check_is_character("Hello")
#> [1] TRUE

check_is_character("Hello", n = 10)
#> [1] FALSE

check_is_character("Hello", n = 10, inequality = "<=")
#> [1] TRUE

check_is_character(c("a", "b", "c"))
#> [1] FALSE