Learn R Programming

assertions

Simple assertions with sensible defaults and customisable error messages.

Overview

The goals with assertions are to provide

  1. Convenient assertion calls (e.g. assert_number())

  2. A general assert function that asserts any possible condition/s and throws informative error messages

  3. Extremely user friendly error message defaults.

  4. Easily customisable error messages, with inline code evaluation & styling powered by the cli package

  5. Simple creation of custom assertion functions with user-specified defaults

Installation

install.packages("assertions")

Development version

You can install the development version of assertions from GitHub by running:

if (!require("remotes", quietly = TRUE))
    install.packages("remotes")

remotes::install_github("selkamand/assertions")

Quick Start

All assertions start with assert, which means you just type it in and levarage autocomplete suggestions to look through all available options

# Load library
library(assertions)

# Use premade assertions
assert_character(c('a', 'b', 'c'))
assert_number(2)
assert_flag(TRUE)

# Assert anything 
assert(1000 % 2 == 0)

# Assert multiple conditions at once (all must be true)
assert(1000 % 2 == 0, 6/2 == 3)

Customizing Error Messages

# Customise any error messages using the `msg` argument
assert_number("A", msg = "Please supply a number!")

# Evaluate code in your error message using '{}' operators
foo = "A"
assert_number(foo, msg = "'{foo}' is not a number :(. Try again")

# Emphasise cetain words in error using {.strong text_to_emphasise}
assert_number("A", msg = "{.strong Try again}")

For advanced customisation, see cli documentation

Create your own assertion functions

Have a custom assertion you want to use repeatedly?

Creating your own assertion functions is extremely easy

Just use assert_create(), you just need to supply:

  1. a function that returns TRUE/FALSE when assertion should PASS/FAIL

  2. a default error message

How about an example?

# Create a function that asserts input is lowercase 
assert_lowercase <- assert_create(
  func = function(x) {x == tolower(x)}, 
  default_error_msg = "'{arg_name}' must be entirely lowercase" 
)

#Assertion passes if input is lowercase
assert_lowercase("all lower case")

#But throws the expected error if uppercase characters are present
assert_lowercase("NOT all lower case") 

See ?assert_create() for details

Vectorised assertions

Assertions may have vectorised versions that test whether all elements in a vector/matrix meet a condition.

For example:

  • assert_greater_than() expects a single number as an input

  • assert_all_greater_than() works on vectors/matrices.

Vectorised functions have the assert_all_ prefix.

Contributing to this package

Two options

Request an assertion

  1. Open a github issue and request away. I’m happy to implement a tonne more assertions, just let me know what you want

Creating assertions yourself

  1. Create a custom assert_something function with a call to assert_create() or assert_create_chain()

  2. Create a github issue with the assertion creation code + any helper function you pass to the func argument (e.g. is_something())

Similar Packages

Great alternative packages for writing assertions include:

Each package has its own features and syntax. So hopefully there is one that suits your needs and preferences. I’m a big fan of checkmate for its speed, assertive for its huge library of ready-made assertion functions, and assertthat for its error message customization.

Copy Link

Version

Install

install.packages('assertions')

Monthly Downloads

850

Version

0.3.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Sam El-Kamand

Last Published

February 13th, 2026

Functions in assertions (0.3.0)

assert_all_strings_contain

Assert all strings contain a regex pattern
assert_class

Assert object belongs to class
assert_character_vector_or_glue

Assert input is a character vector / glue vector
assert_between

Assert input is between a specified minimum and maximum value
assert_file_exists

Assert a file exists
assert_file_has_extension

Assert file extensions
assert_character

Assert input is a character vector
assert_character_vector

Assert input is a character vector
assert_directory_does_not_exist

Assert a directory does not exist
assert_identical

Assert that the input object is identical to a specified value
assert_directory_exists

Assert are directory exists
assert_includes

Assert object includes required
assert_connection

Assert input is a database connection
assert_factor_vector

Assert input is a factor
assert_create

Create an assertion function
assert_file_does_not_exist

Assert a file does not exist
assert_finite

Assert that a value is finite
assert_flag

Assert input is a scalar logical
assert_function_expects

Assert function expects specific parameter names
assert_function

Assert input is a function
assert_int

Assert input is an integer
assert_length_greater_than_or_equal_to

Assert Length Greater Than or Equal To
assert_length

Assert Length
assert_length_greater_than

Assert Length Greater Than
assert_dataframe

Assert input is a data frame
assert_create_chain

Create Chains of Assertions
assert_logical_vector

Assert input is an atomic logical vector
assert_matrix

Assert input is a matrix
assert_number

Assert input is a number
assert_numeric

Assert input is numeric
assert_less_than

Assert input is less than some maximum value
assert_less_than_or_equal_to

Assert input is less than or equal to a specified maximum value
assert_string

Assert input is a character string
assert_string_contains

Assert string contains a regex pattern
assert_excludes

Assert object does not include any illegal values
assert_equal

Assert that the input objects are equal
includes

Check if All Values in Required are in x
is_greater_than

Check if a numeric vector is greater than a specified minimum value
includes_advanced

Check if an object contains required elements
assert_no_missing

Assert that the input vector has no missing values
assert_non_empty_string

Assert input is a non empty character string
assert_function_expects_n_arguments

Assert function expects n arguments
assert_function_variadic

Assert function is variadic
assert_non_null

Assert that the input is not NULL
assert_greater_than_or_equal_to

Assert input is greater than or equal to a specified minimum value
assert_greater_than

Assert input is greater than some minimum value
assert_packages_installed

Assert packages are installed
assert_null

Assert that the input is NULL
assert_list

Assert input is a list
assert_reactive

Assert that x is reactive
assert_length_less_than

Assert Length Less Than
assert_logical

Assert input is logical
assert_length_less_than_or_equal_to

Assert Length Less Than or Equal To
assert_numeric_vector

Assert input is a numeric vector
assert_names_include

Assert that the input object includes a specified name
assert_scalar

Assert input is a scalar
assert_no_duplicates

Assert that the input vector has no duplicates
is_greater_than_or_equal_to

Check if a numeric vector is greater than or equal to a specified minimum value
assert_one_of

Check if a scalar value is one of the acceptable values
assert_set_equal

Check if two sets are identical
format_as_bullets

Preprocess character vectors for cli::cli_abort()
assert_subset

Check if a vector is a subset of another
excludes_advanced

Check if an object does not contain prohibited elements
assertion_tests

Count tests per Assertion
assert_vector

Assert input is a vector
has_all_names

Check if a named object has all specified names
format_inline

Preprocess character vectors for cli package functions
is_reactive

Check if a value is reactive
is_numeric_vector

Check if an object is a numeric vector
common_roxygen_params

Common Parameter Descriptions
contains_pattern

Check if strings contain a regex pattern
check_all_assertions_are_tested_enough

Check assertions are tested enough
has_missing_values

Check if a vector has missing values
has_extension

Has Extension
is_between

Check if a numeric vector is between a specified minimum and maximum value
is_list

Check if a value is a list
util_count_duplicates

Count the number of duplicated values in a vector
is_less_than_or_equal_to

Check if a numeric vector is less than or equal to a specified maximum value
is_string

Check if an object is a single string
is_same_type

Check equality of type
is_character_vector

Check if an object is a character vector
util_get_duplicated_values

Get the duplicated values in a vector
assert_whole_number

Assert that the input object is a whole number
is_identical

Check if two objects are identical
is_character_vector_or_glue

Check if an object is a character vector
is_number

Check if an object is a single number
setopts_count_exlusive_to_first

Count of Elements Exclusive to First Set
is_number_advanced

Check if x is a number
is_less_than

Check if a numeric vector is less than a specified maximum value
assertion_names

List assertion names
is_equal

Check equality of two objects
setopts_exlusive_to_first

Elements Exclusive to First Set
is_flag

Check if a value is a logical flag
is_flag_advanced

Check if x is a flag
is_superset

Check if one set is a superset of another
has_class

Check object is some class
has_duplicates

Check if a vector has duplicates
is_vector

Check if an object is a vector This function checks if an object is a vector
util_count_missing

Count the number of missing values in a vector
has_no_duplicates

Check if a vector has no duplicates
has_no_missing_values

Check if a vector has no missing values
is_string_advanced

Check if x is a string
is_logical_vector

Check if an object is a logical vector
setopts_common_elements

Find Common Elements
setopts_are_equal

Compare Sets for Equality
is_subset

Check if one set is a subset of another
is_non_empty_string_advanced

Check if x is a nonempty string
assert_all_finite

Assert that all values are finite
assert_all_files_have_extension

Assert file extensions
assert_all_directories_exist

Assert all files are directories
assert

Assert that conditions are met
assert_all_less_than

Assert input is less than a specified maximum value
assert_all_less_than_or_equal_to

Assert input is less than or equal to a specified maximum value
assert_all_greater_than_or_equal_to

Assert input is greater than or equal to a specified minimum value
assert_all_greater_than

Assert input is greater than a specified minimum value
assert_all_between

Assert input is between a specified minimum and maximum value
assert_all_files_exist

Assert that all files exist