Title: | Simple Assertions for Beautiful and Customisable Error Messages |
---|---|
Description: | Provides simple assertions with sensible defaults and customisable error messages. It offers convenient assertion call wrappers and a general assert function that can handle any condition. Default error messages are user friendly and easily customized with inline code evaluation and styling powered by the 'cli' package. |
Authors: | Sam El-Kamand [aut, cre, cph] |
Maintainer: | Sam El-Kamand <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.0 |
Built: | 2024-11-19 11:20:07 UTC |
Source: | https://github.com/selkamand/assertions |
Assert that conditions are met
assert(..., msg = NULL, call = rlang::caller_env())
assert(..., msg = NULL, call = rlang::caller_env())
... |
a list of conditions to check |
msg |
A character string containing the error message to display if any of the conditions are not met. The string can include the placeholder {failed_expressions} to insert a list of the failed expressions. The string can also include {?s} and {?is/are} to insert the correct pluralization for the list of failed expressions. |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
invisible(TRUE) if all conditions are met, otherwise aborts with the error message specified by msg
try({ assert(1 == 1) # Passes assert(2 == 2, 3 == 3) # Passes assert(2 == 1, 3 == 3) # Throws default error assert(2 == 1, 3 == 3, msg = "Custom error message") # Throws custom error })
try({ assert(1 == 1) # Passes assert(2 == 2, 3 == 3) # Passes assert(2 == 1, 3 == 3) # Throws default error assert(2 == 1, 3 == 3, msg = "Custom error message") # Throws custom error })
Assert that all paths supplied exist and are directories.
To assert a single directory exists, see assert_directory_exists()
assert_all_directories_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_directories_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
Paths to directories (character) |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is exists and is a directory, otherwise aborts with the error message specified by msg
try({ assert_directory(system.file("package = assertions")) # PASSES assert_directory("foo") # Throws Error })
try({ assert_directory(system.file("package = assertions")) # PASSES assert_directory("foo") # Throws Error })
Assert all files in vector exist. To assert a single file exists, see assert_file_exists()
assert_all_files_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_files_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
Paths to files (character) |
msg |
A character string containing the error message if any files in |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if all files in x
exist, otherwise aborts with the error message specified by msg
real_file <- system.file("DESCRIPTION", package = "assertions") try({ assert_all_files_exist(c(real_file, real_file)) assert_all_files_exist(c("foo", "bar")) # Throws Error })
real_file <- system.file("DESCRIPTION", package = "assertions") try({ assert_all_files_exist(c(real_file, real_file)) assert_all_files_exist(c("foo", "bar")) # Throws Error })
Assert that all filepaths supplied have one of the selected extensions. Does not require file to actually exist.
assert_all_files_have_extension( x, extensions, compression = FALSE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_files_have_extension( x, extensions, compression = FALSE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
extensions |
valid extensions (character vector). Do not include the '.', e.g. supply |
compression |
should compression extension ‘.gz’, ‘.bz2’ or ‘.xz’ be removed first? |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
has any of the specified extensions, otherwise aborts with the error message specified by msg
try({ assert_all_files_have_extension(c("foo.txt", "bar.txt"), extensions = "txt") # Passes assert_all_files_have_extension(c("foo.txt", "bar.csv"), extensions = "csv") # Throws Error })
try({ assert_all_files_have_extension(c("foo.txt", "bar.txt"), extensions = "txt") # Passes assert_all_files_have_extension(c("foo.txt", "bar.csv"), extensions = "csv") # Throws Error })
Assert all elements in a numeric vector/matrix are above some minimum value.
assert_all_greater_than( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_greater_than( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
minimum |
The minimum value to compare against (number) |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is greater than the specified minimum value, otherwise aborts with the error message specified by msg
try({ assert_all_greater_than(3, 2) # Passes assert_all_greater_than(c(2,3,4), 1) # Passes assert_all_greater_than(c(2,3,4), 2) # Passes assert_all_greater_than(c(2,3,1), 3) # Throws default error assert_all_greater_than(c(2,3,1), 3, msg = "custom error message") # Throws custom error })
try({ assert_all_greater_than(3, 2) # Passes assert_all_greater_than(c(2,3,4), 1) # Passes assert_all_greater_than(c(2,3,4), 2) # Passes assert_all_greater_than(c(2,3,1), 3) # Throws default error assert_all_greater_than(c(2,3,1), 3, msg = "custom error message") # Throws custom error })
Assert all elements in a numeric vector/matrix are above some minimum value.
assert_all_greater_than_or_equal_to( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_greater_than_or_equal_to( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
minimum |
The minimum value to compare against |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is greater than or equal to the specified minimum value, otherwise aborts with the error message specified by msg
try({ assert_greater_than_or_equal_to(3, 2) # Passes assert_greater_than_or_equal_to(c(3, 4, 5), 2) # Passes assert_greater_than_or_equal_to(2, 3) # Throws error })
try({ assert_greater_than_or_equal_to(3, 2) # Passes assert_greater_than_or_equal_to(c(3, 4, 5), 2) # Passes assert_greater_than_or_equal_to(2, 3) # Throws error })
Assert all elements in a numeric vector/matrix are below some maximum value.
assert_all_less_than( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_less_than( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
maximum |
The maximum value to compare against (number) |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is less than the specified maximum value, otherwise aborts with the error message specified by msg
try({ assert_all_less_than(1, 2) # Passes assert_all_less_than(c(1,2,3), 4) # Passes assert_all_less_than(c(1,2,3), 2) # Throws default error assert_all_less_than(c(1,2,3), 2, msg = "custom error message") # Throws custom error })
try({ assert_all_less_than(1, 2) # Passes assert_all_less_than(c(1,2,3), 4) # Passes assert_all_less_than(c(1,2,3), 2) # Throws default error assert_all_less_than(c(1,2,3), 2, msg = "custom error message") # Throws custom error })
Assert all elements in a numeric vector/matrix are below or equal to some maximum value.
assert_all_less_than_or_equal_to( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_all_less_than_or_equal_to( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
maximum |
The maximum value to compare against |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is less than or equal to the specified maximum value, otherwise aborts with the error message specified by msg
try({ assert_less_than_or_equal_to(1, 2) # Passes assert_less_than_or_equal_to(c(1, 2, 3), 3) # Passes assert_less_than_or_equal_to(3, 2) # Throws error })
try({ assert_less_than_or_equal_to(1, 2) # Passes assert_less_than_or_equal_to(c(1, 2, 3), 3) # Passes assert_less_than_or_equal_to(3, 2) # Throws error })
Assert an R object is a 'character' type.
Works for vector and matrix objects.
To assert an object is specifically a character vector see assert_character_vector()
assert_character(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_character(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a character vector, otherwise aborts with the error message specified by msg
try({ assert_character("a") # Passes assert_character("a") # Passes assert_character(c("a", "b", "c")) # Passes assert_character(matrix(c('A', 'B', 'C', 'D'))) # Passes assert_character(1:3) # Throws default error assert_character(c("a", 1, "b"), "Custom error message") # Throws custom error })
try({ assert_character("a") # Passes assert_character("a") # Passes assert_character(c("a", "b", "c")) # Passes assert_character(matrix(c('A', 'B', 'C', 'D'))) # Passes assert_character(1:3) # Throws default error assert_character(c("a", 1, "b"), "Custom error message") # Throws custom error })
Assert an object is a character vector. Length 1 character vectors (strings) are considered vectors.
assert_character_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_character_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a character vector, otherwise aborts with the error message specified by msg
try({ assert_character_vector(c("a", "b", "c")) # Passes assert_character_vector(c("a", 1, "b")) # Throws default error assert_character_vector(matrix(c('A', 'B', 'C', 'D'))) # Throws error since type = matrix assert_character_vector(c("a", 1, "b"), "Custom error message") # Throws custom error assert_character_vector(glue::glue('A')) # Throws error })
try({ assert_character_vector(c("a", "b", "c")) # Passes assert_character_vector(c("a", 1, "b")) # Throws default error assert_character_vector(matrix(c('A', 'B', 'C', 'D'))) # Throws error since type = matrix assert_character_vector(c("a", 1, "b"), "Custom error message") # Throws custom error assert_character_vector(glue::glue('A')) # Throws error })
Assert an object is a character vector (or a glue vector). Length 1 character vectors (strings) are considered vectors.
assert_character_vector_or_glue( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_character_vector_or_glue( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a character vector, otherwise aborts with the error message specified by msg
try({ assert_character_vector_or_glue(c("a", "b", "c")) # Passes assert_character_vector_or_glue(glue::glue('A')) # Passes assert_character_vector_or_glue(c("a", 1, "b")) # Throws default error assert_character_vector_or_glue(matrix(c('A', 'B', 'C', 'D'))) # Throws error since type = matrix assert_character_vector_or_glue(c("a", 1, "b"), "Custom error message") # Throws custom error })
try({ assert_character_vector_or_glue(c("a", "b", "c")) # Passes assert_character_vector_or_glue(glue::glue('A')) # Passes assert_character_vector_or_glue(c("a", 1, "b")) # Throws default error assert_character_vector_or_glue(matrix(c('A', 'B', 'C', 'D'))) # Throws error since type = matrix assert_character_vector_or_glue(c("a", 1, "b"), "Custom error message") # Throws custom error })
This function asserts that the input object belongs to class
assert_class(x, class, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_class(x, class, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An input object |
class |
checks if |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
belongs to class
, otherwise aborts with the error message specified by msg
try({ assert_has_class(1, "numeric") # Passes assert_has_class(1, "character") # Throws default error })
try({ assert_has_class(1, "numeric") # Passes assert_has_class(1, "character") # Throws default error })
Assert the input object is a database connection, specifically of the "DBIConnection" class, which is the standard virtual class used by the DBI package for database connections. Note this assertion does not test if the database connection is valid and/or active.
assert_connection(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_connection(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object to assert is a database connection |
msg |
A custom error message displayed if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
This function is designed for use with objects inheriting from the "DBIConnection" class, which is used widely across database connection implementations in R. As other database interface packages are introduced, additional checks may be added to support other connection classes.
invisible(TRUE)
if x
is a valid database connection, otherwise aborts with an error message.
try({ # Assuming a valid DBI connection `conn`: assert_connection(conn) # Passes if `conn` is a DBI connection assert_connection(42) # Fails with error message })
try({ # Assuming a valid DBI connection `conn`: assert_connection(conn) # Passes if `conn` is a DBI connection assert_connection(42) # Fails with error message })
This function creates an assertion function that can be used to check the
validity of an input.
All assertions provided with this package are created using either assert_create()
or assert_create_chain()
assert_create(func, default_error_msg = NULL)
assert_create(func, default_error_msg = NULL)
func |
A function defining the assertion criteria. This function should
return a logical value ( |
default_error_msg |
A character string providing an error message in case
the assertion fails. Must be supplied if function
|
An assertion function.
#' # Create an assertion function that checks that a character string is all # lower case assert_character <- assert_create( is.character, "{arg_name} must be a character vector, not a {class(arg_value)}" ) # Use the assertion function try({ is_lower("hello") # Returns invisible TRUE is_lower("Hello") # Aborts the function with the error message })
#' # Create an assertion function that checks that a character string is all # lower case assert_character <- assert_create( is.character, "{arg_name} must be a character vector, not a {class(arg_value)}" ) # Use the assertion function try({ is_lower("hello") # Returns invisible TRUE is_lower("Hello") # Aborts the function with the error message })
Combine multiple assertion functions created by assert_create()
into a single assertion function with diverse failure modes and error messages.
assert_create_chain(...)
assert_create_chain(...)
... |
assertion functions created by |
A single assertion function that calls each of the input functions in the order they are supplied.
# Create an assertion function that checks for both positive integers and even values assert_string <- assert_create_chain( assert_create(is.character, '{{arg_name}} must be a character'), assert_create(function(x){{ length(x)==1 }}, '{{arg_name}} must be length 1') ) # Use the assertion function to check a valid value assert_string("String") # Use the assertion function to check an invalid value try({ assert_string(3) # Output: Error: '3' must be a character })
# Create an assertion function that checks for both positive integers and even values assert_string <- assert_create_chain( assert_create(is.character, '{{arg_name}} must be a character'), assert_create(function(x){{ length(x)==1 }}, '{{arg_name}} must be length 1') ) # Use the assertion function to check a valid value assert_string("String") # Use the assertion function to check an invalid value try({ assert_string(3) # Output: Error: '3' must be a character })
Assert input is a data frame
assert_dataframe(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_dataframe(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a data frame, otherwise aborts with the error message specified by msg
try({ assert_dataframe(mtcars) # Passes assert_dataframe(data.frame()) # Passes assert_dataframe(1:10) # Throws default error assert_dataframe(matrix(1:6, 2, 3)) # Throws default error assert_dataframe(c(1, 2, 3)) # Throws default error: "Error assert_dataframe(list(a = 1, b = 2)) # Throws default error assert_dataframe(factor(c(1, 2, 3))) # Throws default error assert_dataframe(1:10, msg = "Custom error message") # Throws custom error })
try({ assert_dataframe(mtcars) # Passes assert_dataframe(data.frame()) # Passes assert_dataframe(1:10) # Throws default error assert_dataframe(matrix(1:6, 2, 3)) # Throws default error assert_dataframe(c(1, 2, 3)) # Throws default error: "Error assert_dataframe(list(a = 1, b = 2)) # Throws default error assert_dataframe(factor(c(1, 2, 3))) # Throws default error assert_dataframe(1:10, msg = "Custom error message") # Throws custom error })
Assert that a directory does not already exist. Useful for avoiding overwriting.
This function is an exact copy of assert_file_does_not_exist()
and included to make assertion code more readable.
assert_directory_does_not_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_directory_does_not_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
Path to a file (string) |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if directory x
does not already exist, otherwise aborts with the error message specified by msg
real_dir <- system.file("tests", package = "assertions") try({ assert_directory_does_not_exist("foo") # Passes assert_directory_does_not_exist(real_dir) # Throws error assert_directory_does_not_exist(c("foo", "bar")) # Throws Error (single file only) })
real_dir <- system.file("tests", package = "assertions") try({ assert_directory_does_not_exist("foo") # Passes assert_directory_does_not_exist(real_dir) # Throws error assert_directory_does_not_exist(c("foo", "bar")) # Throws Error (single file only) })
Assert a directory exists.
To assert all directories in a vector exist, see assert_all_directories_exist()
assert_directory_exists( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_directory_exists( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
Path to a directory (string) |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is exists and is a directory, otherwise aborts with the error message specified by msg
try({ assert_directory_exists(system.file("package = assertions")) # PASS assert_all_directories_exist("foo") # Throws Error })
try({ assert_directory_exists(system.file("package = assertions")) # PASS assert_all_directories_exist("foo") # Throws Error })
Is x
equal to y
. powered by the all.equal()
function.
assert_equal( x, y, tolerance = sqrt(.Machine$double.eps), check_names = TRUE, check_environment = TRUE, check_tzone = TRUE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_equal( x, y, tolerance = sqrt(.Machine$double.eps), check_names = TRUE, check_environment = TRUE, check_tzone = TRUE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
y |
The value to compare against |
tolerance |
Differences smaller than tolerance are not reported. The default value is close to 1.5e-8 (numeric >= 0). |
check_names |
should the names(.) of target and current should be compare (flag) |
check_environment |
should the environments of functions should be compared? You may need to set check.environment=FALSE in unexpected cases, such as when comparing two nls() fits. (flag) |
check_tzone |
should "tzone" attributes be compared. Important for comparing POSIXt objects. (flag) |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is equal to the specified value, otherwise aborts with the error message specified by msg
try({ assert_equal(3, 3) # Passes assert_equal(c(3, 3, 3), 3, ) # Fails assert_equal(2, 3) # Throws error })
try({ assert_equal(3, 3) # Passes assert_equal(c(3, 3, 3), 3, ) # Fails assert_equal(2, 3) # Throws error })
Assert x does not include illegal elements
assert_excludes( x, illegal, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_excludes( x, illegal, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
illegal |
The prohibited elements to check for |
msg |
A character string describing the error message if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
includes any illegal
elements, otherwise aborts with the error message specified by msg
try({ assert_directory(system.file("package = assertions")) assert_directory("foo") # Throws Error })
try({ assert_directory(system.file("package = assertions")) assert_directory("foo") # Throws Error })
Assert an R object is a factor. Note that no assert_factor function exists since in R factors are always vector quantities (never scalar / in matrices)
assert_factor_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_factor_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
Technically this function name is misleading, since is.vector(factor(1)) == FALSE
but since they act exactly like vectors to end users, I think this name is the most suitable
invisible(TRUE) if x
is a factor, otherwise aborts with the error message specified by msg
try({ assert_factor_vector(factor(c("a", "b", "c"))) # Passes assert_factor_vector(c("a", "b", "c")) # Throws default error assert_factor_vector(factor(c("a", "b", "c")), "Custom error message") # Passes assert_factor_vector(1:3, "Custom error message") # Throws custom error })
try({ assert_factor_vector(factor(c("a", "b", "c"))) # Passes assert_factor_vector(c("a", "b", "c")) # Throws default error assert_factor_vector(factor(c("a", "b", "c")), "Custom error message") # Passes assert_factor_vector(1:3, "Custom error message") # Throws custom error })
Assert that a file does not exist. Useful for avoiding overwriting.
assert_file_does_not_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_file_does_not_exist( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
Path to a file (string) |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if file x
does not exist, otherwise aborts with the error message specified by msg
real_file <- system.file("DESCRIPTION", package = "assertions") try({ assert_file_does_not_exist("foo") # Passes assert_file_does_not_exist(real_file) # Throws error assert_file_does_not_exist(c("foo", "bar")) # Throws Error (single file only) })
real_file <- system.file("DESCRIPTION", package = "assertions") try({ assert_file_does_not_exist("foo") # Passes assert_file_does_not_exist(real_file) # Throws error assert_file_does_not_exist(c("foo", "bar")) # Throws Error (single file only) })
Assert that a file exists.
To assert all files in a vector exist, see assert_all_files_exist()
assert_file_exists(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_file_exists(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
Path to a file (string) |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if file x
exists, otherwise aborts with the error message specified by msg
real_file <- system.file("DESCRIPTION", package = "assertions") try({ assert_file_exists(real_file) # PASSES assert_file_exists("foo") # Throws Error assert_file_exists(c(real_file, real_file)) # Throws Error (should use assert_all_files_exist) })
real_file <- system.file("DESCRIPTION", package = "assertions") try({ assert_file_exists(real_file) # PASSES assert_file_exists("foo") # Throws Error assert_file_exists(c(real_file, real_file)) # Throws Error (should use assert_all_files_exist) })
Assert that a filepath includes one of the selected extensions. Does not require file to actually exist.
assert_file_has_extension( x, extensions, compression = FALSE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_file_has_extension( x, extensions, compression = FALSE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
extensions |
valid extensions (character vector). Do not include the '.', e.g. supply |
compression |
should compression extension ‘.gz’, ‘.bz2’ or ‘.xz’ be removed first? |
msg |
A character string containing the error message if file |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
has any of the specified extensions, otherwise aborts with the error message specified by msg
try({ assert_file_has_extension("foo.txt", extensions = "txt") # Passes assert_file_has_extension("file.txt", extensions = "csv") # Throws Error })
try({ assert_file_has_extension("foo.txt", extensions = "txt") # Passes assert_file_has_extension("file.txt", extensions = "csv") # Throws Error })
Assert input is a flag (a logical of length 1: TRUE
or FALSE
)
assert_flag(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_flag(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a scalar logical, otherwise aborts with the error message specified by msg
try({ assert_flag(TRUE) # Passes assert_flag(FALSE) # Passes assert_flag(c(TRUE, FALSE)) # Throws default error assert_flag(1, "Custom error message") # Throws custom error })
try({ assert_flag(TRUE) # Passes assert_flag(FALSE) # Passes assert_flag(c(TRUE, FALSE)) # Throws default error assert_flag(1, "Custom error message") # Throws custom error })
Assert input is a function
assert_function(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_function(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a function, otherwise aborts with the error message specified by msg
try({ # Assert that a variable is a function x <- function(a, b) { a + b } assert_function(x) # does nothing # Assert that a variable is not a function x <- "not a function" assert_function(x) # stops execution and prints an error message })
try({ # Assert that a variable is a function x <- function(a, b) { a + b } assert_function(x) # does nothing # Assert that a variable is not a function x <- "not a function" assert_function(x) # stops execution and prints an error message })
Assert a function expects n arguments, with user control over how variable arguments (...) are counted (default throws error)
assert_function_expects_n_arguments( x, n, dots = c("throw_error", "count_as_0", "count_as_1", "count_as_inf"), msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_function_expects_n_arguments( x, n, dots = c("throw_error", "count_as_0", "count_as_1", "count_as_inf"), msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
a function to check has exactly N arguments |
n |
number of arguments that must be expected by function to pass assertion (integer) |
dots |
how to deal with '...' dots (a.k.a variable arguments). Should we count as 0, 1 or infinite arguments. Or, do we just throw an error when we see '...' (default) |
msg |
The error message thrown if the assertion fails (string) |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if function x
expects exactly n arguments, otherwise aborts with the error message specified by msg
Assert a number is greater than a specified minimum value.
To check all numbers in a vector / matrix are above a minimum value, see assert_all_greater_than()
assert_greater_than( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_greater_than( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
minimum |
The minimum value to compare against (number) |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is greater than the specified minimum value, otherwise aborts with the error message specified by msg
try({ assert_greater_than(3, 2) # Passes assert_greater_than(3, 2) # Passes assert_greater_than(c(2,3,4), 1) # Throws error (Must be a number) assert_greater_than('A', 1) # Throws error (Must be a number) assert_greater_than(2, 3, msg = "custom error message") # Throws custom error })
try({ assert_greater_than(3, 2) # Passes assert_greater_than(3, 2) # Passes assert_greater_than(c(2,3,4), 1) # Throws error (Must be a number) assert_greater_than('A', 1) # Throws error (Must be a number) assert_greater_than(2, 3, msg = "custom error message") # Throws custom error })
Assert all elements in a numeric vector/matrix are above or equal to some minimum value.
For vectorized version see assert_all_greater_than_or_equal_to()
assert_greater_than_or_equal_to( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_greater_than_or_equal_to( x, minimum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
minimum |
The minimum value to compare against |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is greater than or equal to the specified minimum value, otherwise aborts with the error message specified by msg
try({ assert_greater_than_or_equal_to(3, 2) # Passes assert_greater_than_or_equal_to(c(3, 4, 5), 2) # Throws error assert_greater_than_or_equal_to(2, 3) # Throws error })
try({ assert_greater_than_or_equal_to(3, 2) # Passes assert_greater_than_or_equal_to(c(3, 4, 5), 2) # Throws error assert_greater_than_or_equal_to(2, 3) # Throws error })
Assert that the input object is identical to a specified value
assert_identical(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_identical(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object to check |
y |
The value to compare against |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is identical to the specified value, otherwise aborts with the error message specified by msg
try({ assert_identical(3, 3) # Passes assert_identical(c(3, 3, 3), 3) # Throws error assert_identical(2, 3) # Throws error })
try({ assert_identical(3, 3) # Passes assert_identical(c(3, 3, 3), 3) # Throws error assert_identical(2, 3) # Throws error })
Assert x includes required elements
assert_includes( x, required, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_includes( x, required, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
required |
The required elements to check for |
msg |
A character string describing the error message if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
includes all required
elements, otherwise aborts with the error message specified by msg
try({ assert_directory(system.file("package = assertions")) assert_directory("foo") # Throws Error })
try({ assert_directory(system.file("package = assertions")) assert_directory("foo") # Throws Error })
Assert input is an integer
assert_int(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_int(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is an integer, otherwise aborts with the error message specified by msg
In R, integers are whole numbers.
Both integers and doubles (numbers with decimals) are considered numeric.
This function checks that x
specifically belong to the integer class.
try({ assert_int(1) # Passes assert_int(1:10) # Passes assert_int(c(1, 2, 3)) # Passes assert_int("a") # Throws default error assert_int(1.5, msg = "Custom error message") # Throws custom error })
try({ assert_int(1) # Passes assert_int(1:10) # Passes assert_int(c(1, 2, 3)) # Passes assert_int("a") # Throws default error assert_int(1.5, msg = "Custom error message") # Throws custom error })
Assert object has a specific length
assert_length( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_length( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
object to check length of |
length |
expected length (number) |
msg |
custom error message |
call |
(logical) whether to preserve call in error message |
arg_name |
(character) name of argument being tested |
invisible(TRUE)
Assert object length is greater than a threshold
assert_length_greater_than( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_length_greater_than( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
object to check length of |
length |
expected length (number) |
msg |
custom error message |
call |
(logical) whether to preserve call in error message |
arg_name |
(character) name of argument being tested |
invisible(TRUE)
Assert object length is greater than or equal to a threshold
assert_length_greater_than_or_equal_to( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_length_greater_than_or_equal_to( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
object to check length of |
length |
expected length (number) |
msg |
custom error message |
call |
(logical) whether to preserve call in error message |
arg_name |
(character) name of argument being tested |
invisible(TRUE)
Assert object length is less than a threshold
assert_length_less_than( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_length_less_than( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
object to check length of |
length |
expected length (number) |
msg |
custom error message |
call |
(logical) whether to preserve call in error message |
arg_name |
(character) name of argument being tested |
invisible(TRUE)
Assert object length is less than or equal to a threshold
assert_length_less_than_or_equal_to( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_length_less_than_or_equal_to( x, length, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
object to check length of |
length |
expected length (number) |
msg |
custom error message |
call |
(logical) whether to preserve call in error message |
arg_name |
(character) name of argument being tested |
invisible(TRUE)
Assert a number is less than a specified maximum value.
To check all numbers in a vector / matrix are below a maximum value, see assert_all_less_than()
assert_less_than( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_less_than( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
maximum |
The maximum value to compare against (number) |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is less than the specified maximum value, otherwise aborts with the error message specified by msg
try({ assert_less_than(1, 2) # Passes assert_less_than(1, 2) # Passes assert_less_than(c(1,2,3), 4) # Throws error (Must be a number) assert_less_than('A', 1) # Throws error (Must be a number) assert_less_than(3, 2, msg = "custom error message") # Throws custom error })
try({ assert_less_than(1, 2) # Passes assert_less_than(1, 2) # Passes assert_less_than(c(1,2,3), 4) # Throws error (Must be a number) assert_less_than('A', 1) # Throws error (Must be a number) assert_less_than(3, 2, msg = "custom error message") # Throws custom error })
Assert a number is less than or equal to a specified maximum value.
For vectorized version see assert_all_less_than_or_equal_to()
assert_less_than_or_equal_to( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_less_than_or_equal_to( x, maximum, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check |
maximum |
The maximum value to compare against |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is less than or equal to the specified maximum value, otherwise aborts with the error message specified by msg
try({ assert_less_than_or_equal_to(1, 2) # Passes assert_less_than_or_equal_to(c(1, 2, 3), 3) # Throws error assert_less_than_or_equal_to(3, 2) # Throws error })
try({ assert_less_than_or_equal_to(1, 2) # Passes assert_less_than_or_equal_to(c(1, 2, 3), 3) # Throws error assert_less_than_or_equal_to(3, 2) # Throws error })
Assert input is a list
assert_list( x, include_dataframes = FALSE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_list( x, include_dataframes = FALSE, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
include_dataframes |
A logical indicating whether data_frames should be considered vectors. Default is |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a list, otherwise aborts with the error message specified by msg
try({ # Assert that a variable is a list x <- list(1, 2, 3) assert_list(x) # does nothing # Assert that a variable is not a list x <- "not a list" assert_list(x) # stops execution and prints an error message })
try({ # Assert that a variable is a list x <- list(1, 2, 3) assert_list(x) # does nothing # Assert that a variable is not a list x <- "not a list" assert_list(x) # stops execution and prints an error message })
Assert an R object is 'logical' (TRUE/FALSE).
Works for vector and matrix objects.
To assert an object is specifically a logical vector see assert_logical_vector()
assert_logical(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_logical(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is logical, otherwise aborts with the error message specified by msg
try({ assert_logical(TRUE) # Passes assert_logical(c(TRUE, FALSE, TRUE)) # Passes assert_logical(c("a", "b")) # Throws default error assert_logical(1:3, "Custom error message") # Throws custom error })
try({ assert_logical(TRUE) # Passes assert_logical(c(TRUE, FALSE, TRUE)) # Passes assert_logical(c("a", "b")) # Throws default error assert_logical(1:3, "Custom error message") # Throws custom error })
Assert input is an atomic logical vector
assert_logical_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_logical_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
msg |
A character string containing the error message to display if x is not an atomic logical vector |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x is an atomic logical vector, otherwise aborts with the error message specified by msg
try({ assert_logical_vector(c(TRUE, TRUE, TRUE)) # Passes assert_logical_vector("a") # Throws default error assert_logical_vector(c(1, 0, 1), "Custom error message") # Throws custom error })
try({ assert_logical_vector(c(TRUE, TRUE, TRUE)) # Passes assert_logical_vector("a") # Throws default error assert_logical_vector(c(1, 0, 1), "Custom error message") # Throws custom error })
Assert input is a matrix
assert_matrix(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_matrix(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a matrix, otherwise aborts with the error message specified by msg
try({ assert_matrix(matrix(1:9, 3)) # Passes assert_matrix(matrix(1:9, 3, 3)) # Passes assert_matrix(c(1, 2, 3)) # Throws default error assert_matrix(1:10, "Custom error message") # Throws custom error })
try({ assert_matrix(matrix(1:9, 3)) # Passes assert_matrix(matrix(1:9, 3, 3)) # Passes assert_matrix(c(1, 2, 3)) # Throws default error assert_matrix(1:10, "Custom error message") # Throws custom error })
Assert that the input object includes a specified name
assert_names_include( x, names, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_names_include( x, names, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object to check for the presence of specific names |
names |
A character vector of names to check for in |
msg |
A character string containing the error message to display if any of the |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if all names
are present in x
, otherwise aborts with the error message specified by msg
try({ x <- list(a = 1, b = 2, c = 3) assert_includes_name(x, "a") # Passes assert_includes_name(x, c("a", "b")) # Passes assert_includes_name(x, c("a", "b", "d")) # Throws default error message assert_includes_name(x, c("a", "b", "d"), "Custom error message") # Throws custom error message })
try({ x <- list(a = 1, b = 2, c = 3) assert_includes_name(x, "a") # Passes assert_includes_name(x, c("a", "b")) # Passes assert_includes_name(x, c("a", "b", "d")) # Throws default error message assert_includes_name(x, c("a", "b", "d"), "Custom error message") # Throws custom error message })
Assert the input vector has no duplicated elements
assert_no_duplicates( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_no_duplicates( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
A vector. |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
has no duplicates, otherwise aborts with the error message specified by msg
try({ assert_no_duplicates(c(1, 2, 3)) # Passes assert_no_duplicates(c(1, 2, 2)) # Throws default error assert_no_duplicates(c(1, 2, 3), msg = "Custom error message") # Passes assert_no_duplicates(c(1, 2, 2), msg = "Custom error message") # Throws custom error })
try({ assert_no_duplicates(c(1, 2, 3)) # Passes assert_no_duplicates(c(1, 2, 2)) # Throws default error assert_no_duplicates(c(1, 2, 3), msg = "Custom error message") # Passes assert_no_duplicates(c(1, 2, 2), msg = "Custom error message") # Throws custom error })
This function asserts that the input vector has no missing values (NA
) and aborts
with an error message if it does.
assert_no_missing(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_no_missing(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
A vector. |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
has no missing values (NA), otherwise aborts with the error message specified by msg
try({ assert_no_missing(c(1, 2, 3)) # Passes assert_no_missing(c(1, NA, 2)) # Throws default error assert_no_missing(c(1, 2, 3), msg = "Custom error message") # Passes assert_no_missing(c(1, NA, 2), msg = "Custom error message") # Throws custom error })
try({ assert_no_missing(c(1, 2, 3)) # Passes assert_no_missing(c(1, NA, 2)) # Throws default error assert_no_missing(c(1, 2, 3), msg = "Custom error message") # Passes assert_no_missing(c(1, NA, 2), msg = "Custom error message") # Throws custom error })
Asserts input is a string, and nonempty (i.e. not equal to ”)
assert_non_empty_string( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_non_empty_string( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
msg |
A character string containing the error message to display if x is not a |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x is a character vector, otherwise aborts with the error message specified by msg
try({ assert_non_empty_string("a") # Passes assert_non_empty_string("") # Fails })
try({ assert_non_empty_string("a") # Passes assert_non_empty_string("") # Fails })
This function asserts that the input is not NULL and aborts with an error message if it is.
assert_non_null(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_non_null(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
A value to check. |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is not NULL, otherwise aborts with the error message specified by msg
.
# Passes for non-NULL assert_non_null(1) try({ # Throws default error for NULL assert_non_null(NULL) # Throws custom error message assert_non_null(NULL, msg = "Custom error message") })
# Passes for non-NULL assert_non_null(1) try({ # Throws default error for NULL assert_non_null(NULL) # Throws custom error message assert_non_null(NULL, msg = "Custom error message") })
This function asserts that the input is NULL and aborts with an error message if it is not.
assert_null(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_null(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
A value to check. |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is NULL, otherwise aborts with the error message specified by msg
.
assert_null(NULL) # Passes try({ assert_null(1) # Throws default error assert_null(1, msg = "Custom error message") # Throws custom error })
assert_null(NULL) # Passes try({ assert_null(1) # Throws default error assert_null(1, msg = "Custom error message") # Throws custom error })
A number is a length 1 numeric vector. Numbers can be either integers or doubles.
assert_number(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_number(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if x is not a number |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x is a number, otherwise aborts with the error message specified by msg
assert_number(2) # Passes try({ assert_number(c(2, 3)) # Throws default error assert_number("a") # Throws default error assert_number(c("a", 1, "b"), "Custom error message") # Throws custom error })
assert_number(2) # Passes try({ assert_number(c(2, 3)) # Throws default error assert_number("a") # Throws default error assert_number(c("a", 1, "b"), "Custom error message") # Throws custom error })
Assert an R object is numeric
Works for vector and matrix objects.
To assert an object is specifically a numeric vector see assert_numeric_vector()
assert_numeric(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_numeric(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is numeric, otherwise aborts with the error message specified by msg
try({ assert_numeric(1:3) # Passes assert_numeric(1.5:5.5) # Passes assert_numeric(c("a", "b", "c")) # Throws default error assert_numeric(c("a", 1, "b"), "Custom error message") # Throws custom error })
try({ assert_numeric(1:3) # Passes assert_numeric(1.5:5.5) # Passes assert_numeric(c("a", "b", "c")) # Throws default error assert_numeric(c("a", 1, "b"), "Custom error message") # Throws custom error })
Assert input is a numeric vector
assert_numeric_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
assert_numeric_vector( x, msg = NULL, call = rlang::caller_env(), arg_name = NULL )
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a numeric vector, otherwise aborts with the error message specified by msg
Assert x
is one of the values of y
.
assert_one_of(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_one_of(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
A scalar value to check |
y |
A vector of acceptable values that |
msg |
The error message thrown if the assertion fails (string) |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
Returns invisible(TRUE) if x
is a scalar and is one of the values in y
, otherwise throws an error
assert_one_of(3, 1:5) # Passes because 3 is in 1:5 assert_one_of("A", c("A", "B", "C")) # Passes because "A" is in the vector try({ assert_one_of("D", c("A", "B", "C")) # Throws error because "D" is not in the vector })
assert_one_of(3, 1:5) # Passes because 3 is in 1:5 assert_one_of("A", c("A", "B", "C")) # Passes because "A" is in the vector try({ assert_one_of("D", c("A", "B", "C")) # Throws error because "D" is not in the vector })
Assert that x is reactive
assert_reactive(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_reactive(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a reactive, otherwise aborts with the error message specified by msg
try({ # Assert that a variable is reactive x <- shiny::reactive(1) assert_reactive(x) # does nothing # Assert that a variable is not a list x <- 1 assert_reactive(x) # stops execution and prints an error message })
try({ # Assert that a variable is reactive x <- shiny::reactive(1) assert_reactive(x) # does nothing # Assert that a variable is not a list x <- 1 assert_reactive(x) # stops execution and prints an error message })
Assert that an object is a scalar, meaning it is a length 1 atomic vector (such as numeric(1)
, character(1)
or logical(1)
).
Note lists, data.frames and matrices are never considered scalar objects, even if they have only one element.
assert_scalar(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_scalar(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a scalar, otherwise aborts with the error message specified by msg
# Pass when value is scalar assert_scalar(5) # Passes assert_scalar("single string") # Passes assert_scalar(TRUE) # Passes # Fail when value is not try({ assert_scalar(c(1, 2, 3)) # Throws default error assert_scalar(matrix(1:4, 2, 2)) # Throws default error })
# Pass when value is scalar assert_scalar(5) # Passes assert_scalar("single string") # Passes assert_scalar(TRUE) # Passes # Fail when value is not try({ assert_scalar(c(1, 2, 3)) # Throws default error assert_scalar(matrix(1:4, 2, 2)) # Throws default error })
This function checks that x
and y
contain exactly the same elements, ignoring order and duplicates.
assert_set_equal(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_set_equal(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
A vector to compare |
y |
Another vector to compare with |
msg |
The error message thrown if the assertion fails (string) |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
Returns invisible(TRUE)
if x
and y
contain all the same elements (ignoring order and duplicates), otherwise throws an error.
# Passes because elements are the same, order doesn't matter assert_set_equal(c(1, 2, 3), c(3, 2, 1)) # Passes because elements are identical assert_set_equal(c("A", "B", "C"), c("C", "A", "B")) try({ # Throws error because elements are not identical assert_set_equal(c(1, 2, 3), c(1, 2)) # Throws error because elements differ assert_set_equal(c("A", "B"), c("A", "B", "C")) })
# Passes because elements are the same, order doesn't matter assert_set_equal(c(1, 2, 3), c(3, 2, 1)) # Passes because elements are identical assert_set_equal(c("A", "B", "C"), c("C", "A", "B")) try({ # Throws error because elements are not identical assert_set_equal(c(1, 2, 3), c(1, 2)) # Throws error because elements differ assert_set_equal(c("A", "B"), c("A", "B", "C")) })
Assert input is a character string
assert_string(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_string(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if x is not a string |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x is a string, otherwise aborts with the error message specified by msg
try({ assert_string("a") # Passes assert_string(c("a", "b", "c")) # Throws default error assert_string(1:3) # Throws default error assert_string(c("a", 1, "b"), "Custom error message") # Throws custom error })
try({ assert_string("a") # Passes assert_string(c("a", "b", "c")) # Throws default error assert_string(1:3) # Throws default error assert_string(c("a", 1, "b"), "Custom error message") # Throws custom error })
This function checks that x
is a subset of y
assert_subset(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_subset(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
A vector to check |
y |
the acceptable values that x can take |
msg |
The error message thrown if the assertion fails (string) |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
Returns invisible(TRUE) if x
is a subset of y
, otherwise throws an error
try({ assert_subset(1:3, 1:5) # Passes assert_subset(c("A", "B", "C"), c("A", "B")) # Throws error since "C" is not present in first vector })
try({ assert_subset(1:3, 1:5) # Passes assert_subset(c("A", "B", "C"), c("A", "B")) # Throws error since "C" is not present in first vector })
Assert input is a vector
assert_vector(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_vector(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
A character string containing the error message to display if |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a vector, otherwise aborts with the error message specified by msg
By default, lists are not considered vectors (i.e. include_lists = FALSE
) to align with what end-users will expect, in spite of these objects technically being vectors.
try({ assert_vector(c(1, 2, 3)) # Passes assert_vector(matrix(1:6, 2, 3)) # Throws default error message assert_vector(1:3) # Passes assert_vector(list(1, 2, 3)) # Throws default error message assert_vector(list(1, 2, 3), include_lists = TRUE) # Passes assert_vector(c("a", 1, "b"), "Custom error message") # Throws custom error message assert_vector(factor(c(1, 2, 3)), "Custom error message") # Throws custom error message })
try({ assert_vector(c(1, 2, 3)) # Passes assert_vector(matrix(1:6, 2, 3)) # Throws default error message assert_vector(1:3) # Passes assert_vector(list(1, 2, 3)) # Throws default error message assert_vector(list(1, 2, 3), include_lists = TRUE) # Passes assert_vector(c("a", 1, "b"), "Custom error message") # Throws custom error message assert_vector(factor(c(1, 2, 3)), "Custom error message") # Throws custom error message })
Check if x
is a whole number (no decimal)
assert_whole_number(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
assert_whole_number(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)
x |
An object |
msg |
The error message thrown if the assertion fails (string) |
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
invisible(TRUE) if x
is a whole number, otherwise aborts with the error message specified by msg
try({ assert_whole_number(24) # Passes assert_whole_number(2.5) # Throws error })
try({ assert_whole_number(24) # Passes assert_whole_number(2.5) # Throws error })
List all assertion names
assertion_names(exclude_create_and_chain = TRUE)
assertion_names(exclude_create_and_chain = TRUE)
exclude_create_and_chain |
exclude assert_create and assert_create_chain (flag) |
unique set of assertion names (character)
Count the number of unit-tests per assertion.
Note assertion_tests only finds tests where expect_
and assert_
are on the same line.
assertion_tests()
assertion_tests()
two column data.frame describing assertion name and number of tests (expect_statement)
Check assertions are tested enough
check_all_assertions_are_tested_enough(min_required_tests = 5)
check_all_assertions_are_tested_enough(min_required_tests = 5)
min_required_tests |
min number of tests (expect statements) per assertion |
TRUE if all assertions sufficiently tested. Otherwise throws error
Common Parameter Descriptions
common_roxygen_params(call, arg_name, msg, ...)
common_roxygen_params(call, arg_name, msg, ...)
call |
Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details. |
arg_name |
Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name). |
msg |
The error message thrown if the assertion fails (string) |
... |
Used to pass any arguments to assertion function |
This function checks that x
does not include any of the illegal
elements.
x
must be the same type as illegal
.
Factors are treated as character vectors.
excludes_advanced(x, illegal)
excludes_advanced(x, illegal)
x |
An object to check |
illegal |
The prohibited elements to check for |
Returns TRUE if x
is the same type as illegal
and x
does not include any of the illegal
elements.
Otherwise returns a string representing the appropriate error message to display
The format_as_bullets
function is used for preprocessing character vectors by adding names.
These names are used to denote bullet points when the character vector is passed to cli::cli_abort()
.
This allows for the easy creation of bullet point lists in error messages.
The bullet argument allows the user to specify the desired bullet point symbol.
The default bullet point symbols are: *, >, , x, v, i, and !.
format_as_bullets(x, bullet = c("*", ">", " ", "x", "v", "i", "!"))
format_as_bullets(x, bullet = c("*", ">", " ", "x", "v", "i", "!"))
x |
A list of character strings |
bullet |
One of ”, '>', ' ', 'x', 'v', 'i', '!' (default: ”) The character to use as the bullet point for each element of x. |
A character string with each element of x formatted as a bullet point
Preprocess character vectors for cli package functions
format_inline(x, inline_tag = c("strong", "emph", "code", "arg"))
format_inline(x, inline_tag = c("strong", "emph", "code", "arg"))
x |
A character vector |
inline_tag |
A character vector of inline tag names (e.g. "strong", "emph", "code", "arg") |
A character vector with inline tags applied to each element
This function returns a logical value indicating whether the object x
has all the names specified in names
.
has_all_names(x, names)
has_all_names(x, names)
x |
a named object |
names |
A character vector of names to check for in |
A logical value indicating whether x
has all the names specified in names
This function checks whether object is a specific class
has_class(x, class)
has_class(x, class)
x |
A value to check. |
class |
checks if |
A logical scalar indicating x
belongs to class
if(interactive()) { has_class(1, "numeric") # TRUE has_class(1, "character") # FALSE }
if(interactive()) { has_class(1, "numeric") # TRUE has_class(1, "character") # FALSE }
This function returns a logical value indicating whether the input vector contains duplicated elements.
has_duplicates(x)
has_duplicates(x)
x |
A vector. |
A logical value indicating whether the input vector contains duplicated elements.
if(interactive()){ has_duplicates(c(1, 2, 3)) # returns FALSE has_duplicates(c(1, 2, 2)) # returns TRUE }
if(interactive()){ has_duplicates(c(1, 2, 3)) # returns FALSE has_duplicates(c(1, 2, 2)) # returns TRUE }
Has Extension
has_extension(x, extensions, compression = FALSE)
has_extension(x, extensions, compression = FALSE)
x |
object to test |
extensions |
valid extensions (character vector). Do not include the '.', e.g. supply |
compression |
should compression extension ‘.gz’, ‘.bz2’ or ‘.xz’ be removed first? |
TRUE if all x have valid extensions as supplied by extensions
(flag)
This function returns a logical value indicating whether the input vector contains
missing values (NA
).
has_missing_values(x)
has_missing_values(x)
x |
A vector. |
A logical value indicating whether the input vector contains missing values.
if(interactive()){ has_missing_values(c(1, 2, 3)) # returns FALSE has_missing_values(c(1, NA, 2)) # returns TRUE }
if(interactive()){ has_missing_values(c(1, 2, 3)) # returns FALSE has_missing_values(c(1, NA, 2)) # returns TRUE }
This function returns a logical value indicating whether the input vector contains no duplicated elements.
has_no_duplicates(x)
has_no_duplicates(x)
x |
A vector. |
A logical value indicating whether the input vector contains no duplicated elements.
if(interactive()){ has_no_duplicates(c(1, 2, 3)) # returns TRUE has_no_duplicates(c(1, 2, 2)) # returns FALSE }
if(interactive()){ has_no_duplicates(c(1, 2, 3)) # returns TRUE has_no_duplicates(c(1, 2, 2)) # returns FALSE }
This function returns a logical value indicating whether the input vector contains
no missing values (NA
).
has_no_missing_values(x)
has_no_missing_values(x)
x |
A vector. |
A logical value indicating whether the input vector contains no missing values.
if(interactive()){ has_no_missing_values(c(1, 2, 3)) # returns TRUE has_no_missing_values(c(1, NA, 2)) # returns FALSE }
if(interactive()){ has_no_missing_values(c(1, 2, 3)) # returns TRUE has_no_missing_values(c(1, NA, 2)) # returns FALSE }
Checks if all elements of required
are present in x
.
includes(x, required)
includes(x, required)
x |
A vector of elements. |
required |
A vector of elements to check for inclusion in |
A logical value indicating whether all elements of required
are present in x
(TRUE
) or not (FALSE
).
This function checks that x
includes all of the required
elements.
x
must be the same type as required
.
Factors are treated as character vectors.
includes_advanced(x, required)
includes_advanced(x, required)
x |
An object to check |
required |
The required elements to check for |
Returns TRUE if x
is the same type as required
and x
includes all the required
elements.
Otherwise returns a string representing the appropriate error message to display
Check if an object is a character vector
is_character_vector(x)
is_character_vector(x)
x |
An object to check. |
A logical value indicating whether x
is a character vector.
Differs from is_character_vector()
in that it permits glue character vectors to pass.
is_character_vector_or_glue(x)
is_character_vector_or_glue(x)
x |
An object to check. |
A logical value indicating whether x
is a character vector or glue vector.
Is x
equal to y
. powered by the all.equal()
function.
is_equal( x, y, tolerance = sqrt(.Machine$double.eps), check_names = TRUE, check_environment = TRUE, check_tzone = TRUE )
is_equal( x, y, tolerance = sqrt(.Machine$double.eps), check_names = TRUE, check_environment = TRUE, check_tzone = TRUE )
x |
first object to compare |
y |
second object to compare |
tolerance |
Differences smaller than tolerance are not reported. The default value is close to 1.5e-8 (numeric >= 0). |
check_names |
should the names(.) of target and current should be compare (flag) |
check_environment |
should the environments of functions should be compared? You may need to set check.environment=FALSE in unexpected cases, such as when comparing two nls() fits. (flag) |
check_tzone |
should "tzone" attributes be compared. Important for comparing POSIXt objects. (flag) |
TRUE if x is equal to y
if(interactive()){ is_equal(1, 1) #TRUE is_equal(c(1, 2), 1) #FALSE is_equal(c("A", "B"), c("A", "B")) #TRUE is_equal("A", "B") #FALSE }
if(interactive()){ is_equal(1, 1) #TRUE is_equal(c(1, 2), 1) #FALSE is_equal(c("A", "B"), c("A", "B")) #TRUE is_equal("A", "B") #FALSE }
This function checks if a value is a logical scalar (i.e., a single logical value).
is_flag(x)
is_flag(x)
x |
A value to check. |
A logical scalar indicating whether x
is a logical flag.
This function is designed for use with assert_create_advanced
. It must return
TRUE for the assertion to pass or a string representing the error message if the
assertion should fail.
is_flag_advanced(x)
is_flag_advanced(x)
x |
A value to be checked |
Returns invisible(TRUE) if x is a logical value with length 1. Returns a string with an error message if x is not a logical value or has a length other than 1.
This function checks if a numeric vector is greater than a specified minimum value. It can also optionally check if all elements of the vector must be greater than the minimum value or if only one element is sufficient
is_greater_than(x, minimum)
is_greater_than(x, minimum)
x |
a numeric vector to check |
minimum |
The minimum value to compare against |
A logical value indicating whether all elements of the numeric vector x are greater than the specified minimum value
if(interactive()){ is_greater_than(c(2,3,4), 1) # TRUE is_greater_than(c(2,3,4), 2) # TRUE is_greater_than(c(2,3,1), 3) # FALSE }
if(interactive()){ is_greater_than(c(2,3,4), 1) # TRUE is_greater_than(c(2,3,4), 2) # TRUE is_greater_than(c(2,3,1), 3) # FALSE }
This function checks if a numeric vector is greater than or equal to a specified minimum value. It can also optionally check if all elements of the vector must be greater than or equal to the minimum value or if only one element is sufficient
is_greater_than_or_equal_to(x, minimum)
is_greater_than_or_equal_to(x, minimum)
x |
a numeric vector to check |
minimum |
The minimum value to compare against |
A logical value indicating whether all elements of the numeric vector x are greater than or equal to the specified minimum value
if(interactive()){ is_greater_than_or_equal_to(c(2,3,4), 1) # TRUE is_greater_than_or_equal_to(c(2,3,4), 2) # TRUE is_greater_than_or_equal_to(c(2,3,1), 3) # FALSE }
if(interactive()){ is_greater_than_or_equal_to(c(2,3,4), 1) # TRUE is_greater_than_or_equal_to(c(2,3,4), 2) # TRUE is_greater_than_or_equal_to(c(2,3,1), 3) # FALSE }
Check if two objects are identical
is_identical(x, y)
is_identical(x, y)
x |
first object to compare |
y |
second object to compare |
logical value indicating whether or not the objects are identical
This function checks if a numeric vector is less than a specified maximum value. It can also optionally check if all elements of the vector must be less than the maximum value or if only one element is sufficient
is_less_than(x, maximum)
is_less_than(x, maximum)
x |
a numeric vector to check |
maximum |
The maximum value to compare against |
A logical value indicating whether all elements of the numeric vector x are less than the specified maximum value
if(interactive()){ is_less_than(c(1,2,3), 4) # TRUE is_less_than(c(1,2,3), 2) # FALSE is_less_than(c(1,2,4), 3) # FALSE }
if(interactive()){ is_less_than(c(1,2,3), 4) # TRUE is_less_than(c(1,2,3), 2) # FALSE is_less_than(c(1,2,4), 3) # FALSE }
This function checks if a numeric vector is less than or equal to a specified maximum value. It can also optionally check if all elements of the vector must be less than or equal to the maximum value or if only one element is sufficient
is_less_than_or_equal_to(x, maximum)
is_less_than_or_equal_to(x, maximum)
x |
a numeric vector to check |
maximum |
The maximum value to compare against |
A logical value indicating whether all elements of the numeric vector x are less than or equal to the specified maximum value
if(interactive()){ is_less_than_or_equal_to(c(1,2,3), 4) # TRUE is_less_than_or_equal_to(c(1,2,3), 3) # TRUE is_less_than_or_equal_to(c(1,2,4), 3) # FALSE }
if(interactive()){ is_less_than_or_equal_to(c(1,2,3), 4) # TRUE is_less_than_or_equal_to(c(1,2,3), 3) # TRUE is_less_than_or_equal_to(c(1,2,4), 3) # FALSE }
This function checks if a value is a list.
By default, definition of a 'list' excludes data.frames in spite of them technically being lists.
This behaviour can be changed by setting include_dataframes = TRUE
is_list(x, include_dataframes = FALSE)
is_list(x, include_dataframes = FALSE)
x |
A value to check. |
include_dataframes |
A logical indicating whether data_frames should be considered vectors. Default is |
A logical scalar indicating whether x
is a list.
if(interactive()){ is_list(list(1, 2)) # TRUE is_list(c(1, 2, 3)) # FALSE is_list(data.frame()) # FALSE is_list(data.frame(), include_dataframes = TRUE) # TRUE }
if(interactive()){ is_list(list(1, 2)) # TRUE is_list(c(1, 2, 3)) # FALSE is_list(data.frame()) # FALSE is_list(data.frame(), include_dataframes = TRUE) # TRUE }
Check if an object is a logical vector
is_logical_vector(x)
is_logical_vector(x)
x |
An object to check. |
A logical value indicating whether x
is a logical vector.
This function is designed for use with assert_create
. It returns
TRUE for the assertion to pass or a string representing the error message if the
assertion should fail.
is_non_empty_string_advanced(x)
is_non_empty_string_advanced(x)
x |
A value to be checked |
Returns invisible(TRUE) if x is a character value with length 1 and at least 1 character in string. Returns a string with an error message if x is not a character value or has a length other than 1.
Check if an object is a single number
is_number(x)
is_number(x)
x |
An object to check. |
A logical value indicating whether x
is a single number.
This function is designed for use with assert_create_advanced
. It must return
TRUE for the assertion to pass or a string representing the error message if the
assertion should fail.
is_number_advanced(x)
is_number_advanced(x)
x |
A value to be checked |
Returns invisible(TRUE) if x is a numeric value with length 1. Returns a string with an error message if x is not a numeric value or has a length other than 1.
This function checks if an object is a numeric vector in R.
is_numeric_vector(x)
is_numeric_vector(x)
x |
An object to check. |
A logical value indicating whether x
is a numeric vector.
if(interactive()){ is_numeric_vector(c(1, 2, 3)) # TRUE is_numeric_vector(list(1, 2, 3)) # FALSE is_numeric_vector(1:5) # TRUE is_numeric_vector("hello") # FALSE is_numeric_vector(list(1, 2, "a")) # FALSE }
if(interactive()){ is_numeric_vector(c(1, 2, 3)) # TRUE is_numeric_vector(list(1, 2, 3)) # FALSE is_numeric_vector(1:5) # TRUE is_numeric_vector("hello") # FALSE is_numeric_vector(list(1, 2, "a")) # FALSE }
This function checks if a value is reactive
is_reactive(x)
is_reactive(x)
x |
A value to check. |
A logical scalar indicating whether x
is a list.
if(interactive()){ is_reactive(shiny::reactive(1)) # TRUE is_reactive(1) # FALSE }
if(interactive()){ is_reactive(shiny::reactive(1)) # TRUE is_reactive(1) # FALSE }
Is type of x
the same as y
(according to typof)
is_same_type(x, y)
is_same_type(x, y)
x |
first object to compare |
y |
second object to compare |
TRUE if x and y are of the same type, otherwise FALSE
Check if an object is a single string
is_string(x)
is_string(x)
x |
An object to check. |
A logical value indicating whether x
is a single string.
This function is designed for use with assert_create
. It returns
TRUE for the assertion to pass or a string representing the error message if the
assertion should fail.
is_string_advanced(x)
is_string_advanced(x)
x |
A value to be checked |
Returns invisible(TRUE) if x is a character value with length 1. Returns a string with an error message if x is not a character value or has a length other than 1.
Determines if all elements in set x
are also present in set y
.
is_subset(x, y)
is_subset(x, y)
x |
A numeric, character, or logical vector. |
y |
A numeric, character, or logical vector. |
A logical value indicating whether x
is a subset of y
.
Determines if all elements in set y
are also present in set x
.
is_superset(x, y)
is_superset(x, y)
x |
A numeric, character, or logical vector. |
y |
A numeric, character, or logical vector. |
A logical value indicating whether x
is a superset of y
.
Check if an object is a vector This function checks if an object is a vector
is_vector(x)
is_vector(x)
x |
An object to check |
A logical indicating whether x
is a vector
Determine if the two sets are equal.
setopts_are_equal(x, y)
setopts_are_equal(x, y)
x |
A vector of elements. |
y |
A vector of elements. |
A logical value indicating whether the sets are equal (TRUE
) or not (FALSE
).
Find the elements that are present in both sets.
setopts_common_elements(x, y)
setopts_common_elements(x, y)
x |
A vector of elements. |
y |
A vector of elements. |
A vector of elements that are present in both sets.
Counts the number of elements that are in the first set but not in the second set.
setopts_count_exlusive_to_first(x, y)
setopts_count_exlusive_to_first(x, y)
x |
A vector of elements. |
y |
A vector of elements. |
A scalar representing the number of elements that are in the first set but not in the second set.
Finds the elements that are in the first set but not in the second set.
setopts_exlusive_to_first(x, y)
setopts_exlusive_to_first(x, y)
x |
A vector of elements. |
y |
A vector of elements. |
A vector of elements that are in the first set but not in the second set.
This function returns the number of duplicated values in the input vector.
util_count_duplicates(x)
util_count_duplicates(x)
x |
A vector. |
The number of duplicated values in the input vector.
if(interactive()) { util_count_duplicates(c(1, 2, 2)) # returns 1 util_count_duplicates(c(1, 2, 3)) # returns 0 }
if(interactive()) { util_count_duplicates(c(1, 2, 2)) # returns 1 util_count_duplicates(c(1, 2, 3)) # returns 0 }
This function returns the number of missing values (NA
) in the input vector.
util_count_missing(x)
util_count_missing(x)
x |
A vector. |
The number of missing values in the input vector.
if(interactive()){ util_count_missing(c(1, 2, 3)) # returns 0 util_count_missing(c(1, NA, 2)) # returns 1 }
if(interactive()){ util_count_missing(c(1, 2, 3)) # returns 0 util_count_missing(c(1, NA, 2)) # returns 1 }
This function returns a vector of the duplicated values in the input vector.
util_get_duplicated_values(x)
util_get_duplicated_values(x)
x |
A vector. |
A vector of the duplicated values in the input vector.
if(interactive()) { util_get_duplicated_values(c(1, 2, 2)) # returns 2 util_get_duplicated_values(c(1, 2, 3)) # returns NULL }
if(interactive()) { util_get_duplicated_values(c(1, 2, 2)) # returns 2 util_get_duplicated_values(c(1, 2, 3)) # returns NULL }