assertions: Simple assertions

Description Usage Arguments Value Numeric versus double values Strict atomic values Single values Author(s) Examples

Description

Check if an object is of a proper type, mode, length, etc. This is useful when implementing simple checks. These are used extensively in package blueprint and are all available to the users for convenience.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
is_single(x)

is_scalar_character(x, accept_na = TRUE)

is_scalar_logical(x, accept_na = TRUE)

is_scalar_integer(x, accept_na = TRUE)

is_scalar_numeric(x, accept_na = TRUE)

is_strict_atomic(x, accept_na = TRUE)

is_named_list(x, unique_names = TRUE)

is_named_vctr(x, unique_names = TRUE)

Arguments

x

Any R object.

accept_na

A scalar logical. Should NA values be considered as valid scalar and/or atomic values?

unique_names

A scalar logical. Should names be unique?

Value

All functions return a scalar logical. A TRUE implies that the underlying tested object is something (that something is given by the function's name).

Numeric versus double values

Numeric is either an integer or a double. Is it NOT an atomic type, it is a mode. In blueprint, modes are purposely avoided. The focus is always on types for consistency.

Strict atomic values

In package blueprint, a strict atomic value is any R value that respects the following criteria.

  1. It is an atomic R type: NULL, logical, integer, single, double (or numeric), complex, character or raw.

  2. It is a pure vector that has only one non-degenerate dimension.

  3. It does not possess any structural attributes (other than type-related attributes), not even names.

Matrices, arrays and other recursive structures are not strict atomic values. Vectors such as factors that are described by attributes (comments, levels, names, etc.) are not strict atomic values. Single values (see section Single values below) are considered to be strict atomic values.

Single values

There is no single values in R. A single is a double that has a unique attribute called "Csingle" set equal to TRUE. Trying to use the old R function is.single() will simply throw an error. This is because so-called single values should only be used with arguments used in external interfaces (such as .C() and .Fortran()). Therefore, function is_single() checks that the argument passed to it is a double with an attribute "Csingle" set equal to TRUE.

Author(s)

Jean-Mathieu Potvin (jm@potvin.xyz)

Examples

1
2
3
# You control whether NAs are valid.
is_scalar_character(NA_character_, accept_na = TRUE)  # TRUE
is_scalar_character(NA_character_, accept_na = FALSE) # FALSE

jeanmathieupotvin/blueprint documentation built on Jan. 17, 2021, 10:54 a.m.