Skip to contents

The check_strings() is a wrapper over three functions. You can choose the test you want to perform with the type argument:

  • type = "needed" calls the function check_needed_strings(),

  • type = "allowed" calls the function check_allowed_strings(),

  • type = "exact" calls the function check_exact_strings().

For more information on the difference between these three, check the details section.

Usage

check_strings(.args, expected_strings, type = c("needed", "allowed", "exact"))

check_allowed_strings(argument_strings, allowed_strings)

check_needed_strings(argument_strings, needed_strings)

check_exact_strings(argument_strings, match_strings)

Arguments

.args

Arguments that will be tested against expected_strings. They need to be called with [check_args()] function. The value will be passed to argument_strings argument of underlying function. See details section for more information.

expected_strings

Character vector of the expected values. Depending on choice of type this will be passed to needed_strings, allowed_strings or match_strings argument of the underlying function. See details section for more information.

type

One of "allowed" (default), "needed", "exact". See details section for more information.

argument_strings

Name of the variable containing character vector.

allowed_strings

Character vector of allowed values.

needed_strings

Character vector of needed values.

match_strings

Character vector of expected values.

Value

If the check_arg is missing any element of the string vector it will throw an error. If exact = TRUE it will also check if there are any additional elements in the check_arg vector compared to the string vector and will throw an error. Otherwise it returns invisible NULL.

Details

check_needed_strings()

The purpose of the check_needed_strings() function is to check if all of the needed_strings are present in the tested vector. For instance, you would use this check if you want to assure that the tested vector has AT LEAST the expected strings values.

A good example is checking if there are some crucial columns in data frame. Usually we need to assure that our data frame argument has at least response_id and weight columns, but we do not really care about other column names.

check_allowed_strings()

The purpose of the check_allowed_strings() is to check if a vector of the tested strings contains only the allowed_strings. For instance, you would use this check if you want to assert that your tested vector of strings does not contain unwanted strings. Simultaneously you do not need the tested vector to be exactly the same as the expected values.

This check is a good substitution for match.arg() function, with better display of the error message.

check_exact_strings()

The purpose of the check_exact_strings() is to check if the tested vector of strings and the expected vector of strings have EXACTLY same values.

Examples

if (FALSE) { # \dontrun{
string_3 <- c("one", "two", "three")
string_2 <- c("one", "two")
string_1 <- c("one")
string_3_rev <- c("three", "two", "one")
string_45 <- c("four", "five")
string_56 <- c("five", "six")

#---- Check for allowed strings  

## Won't throw error since all elements of string_1 are present in string_2
check_allowed_strings(string_1, string_2)
check_strings(check_args(string_1), string_2, type = "allowed")

## Will throw error since "three" is missing from string_2
check_allowed_strings(string_3, string_2)
check_strings(check_args(string_3), string_2, type = "allowed")

#---- Check for needed strings

## Will throw error since "two" is missing from string_1
check_needed_strings(string_1, string_2)
check_strings(check_args(string_1), string_2, type = "needed")

## Won't throw error since all of string_2 are present in string_3
check_needed_strings(string_3, string_2)
check_strings(check_args(string_3), string_2, type = "needed")

#---- Check for exact strings

## Won't throw error since both strings has same values
check_exact_strings(string_3, string_3_rev)
check_exact_strings(string_2, string_2)

check_strings(check_args(string_3), string_3_rev, type = "exact")
check_strings(check_args(string_1), string_1, type = "exact") 

## Will throw an error since string_1 is missing "two"
check_exact_strings(string_1, string_2)
check_strings(check_args(string_1), string_2, type = "exact")

## Will throw an error since string_2 is missing "three"
check_exact_strings(string_3, string_2)
check_strings(check_args(string_3), string_2, type = "exact")

## Will throw error since string_45 is missing "six" and string_56 is missing
## four
check_exact_strings(string_56, string_45)
check_strings(check_args(string_56), string_45, type = "exact")
} # }