assertions: Additional Assertions for 'assert_that'

assertionsR Documentation

Additional Assertions for assert_that

Description

[Experimental]

We provide additional assertion functions which can be used together with assertthat::assert_that().

[Experimental]

We provide additional assertion functions which can be used together with the checkmate functions. These are described in individual help pages linked below.

Usage

is_class(x, class2)

is_hermes_data(x)

is_counts_vector(x)

is_list_with(x, elements)

one_provided(one, two)

is_constant(x)

Arguments

x

an object to check.

class2

(character or class definition)
the class to which x could belong.

elements

(character)
names of elements which should be in the list x.

one

first input.

two

second input.

Value

Depending on the function prefix.

  • assert_ functions return the object invisibly if successful, and otherwise throw an error message.

  • check_ functions return TRUE if successful, otherwise a string with the error message.

  • test_ functions just return TRUE or FALSE.

Functions

  • is_class(): checks the class.

  • is_hermes_data(): checks whether x is an AnyHermesData object.

  • is_counts_vector(): checks for a vector of counts (positive integers).

  • is_list_with(): checks for a list containing elements.

  • one_provided(): checks that exactly one of the two inputs one, two is not NULL.

  • is_constant(): checks whether the vector x is constant (only supports numeric, factor, character, logical). NAs are removed first.

See Also

assert_proportion()

Examples

# Assert a general class.
a <- 5
is_class(a, "character")

# Assert a `AnyHermesData` object.
is_hermes_data(hermes_data)
is_hermes_data(42)

# Assert a counts vector.
a <- 5L
is_counts_vector(a)

# Assert a list containing certain elements.
b <- list(a = 5, b = 3)
is_list_with(b, c("a", "c"))
is_list_with(b, c("a", "b"))

# Assert that exactly one of two arguments is provided.
a <- 10
b <- 10
one_provided(a, b)
one_provided(a, NULL)

# Assert a constant vector.
is_constant(c(1, 2))
is_constant(c(NA, 1))
is_constant(c("a", "a"))
is_constant(factor(c("a", "a")))

insightsengineering/hermes documentation built on Sept. 19, 2024, 9:06 p.m.