Skip to contents

[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.

Examples

# Assert a general class.
a <- 5
is_class(a, "character")
#> [1] FALSE

# Assert a `AnyHermesData` object.
is_hermes_data(hermes_data)
#> [1] TRUE
is_hermes_data(42)
#> [1] FALSE

# Assert a counts vector.
a <- 5L
is_counts_vector(a)
#> [1] TRUE

# Assert a list containing certain elements.
b <- list(a = 5, b = 3)
is_list_with(b, c("a", "c"))
#> [1] FALSE
is_list_with(b, c("a", "b"))
#> [1] TRUE

# Assert that exactly one of two arguments is provided.
a <- 10
b <- 10
one_provided(a, b)
#> [1] FALSE
one_provided(a, NULL)
#> [1] TRUE

# Assert a constant vector.
is_constant(c(1, 2))
#> [1] FALSE
is_constant(c(NA, 1))
#> [1] TRUE
is_constant(c("a", "a"))
#> [1] TRUE
is_constant(factor(c("a", "a")))
#> [1] TRUE