Is the input atomic/recursive/vector?

Share:

Description

Checks to see if the input is a type that is atomic/recursive/vector.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
assert_is_atomic(x, severity = getOption("assertive.severity", "stop"))

assert_is_nested(x, severity = getOption("assertive.severity", "stop"))

assert_is_non_nested(x, severity = getOption("assertive.severity", "stop"))

assert_is_recursive(x, severity = getOption("assertive.severity", "stop"))

assert_is_vector(x, severity = getOption("assertive.severity", "stop"))

is_atomic(x, .xname = get_name_in_parent(x))

is_nested(x, .xname = get_name_in_parent(x))

is_non_nested(x, .xname = get_name_in_parent(x))

is_recursive(x, .xname = get_name_in_parent(x))

is_vector(x, .xname = get_name_in_parent(x))

Arguments

x

Input to check.

severity

How severe should the consequences of the assertion be? Either "stop", "warning", "message", or "none".

.xname

Not intended to be used directly.

Value

is_atomic, is_recursive and is_vector wrap is.atomic, is.recursive and is.vector respectively, providing more information on failure. is_nested checks for recursive objects where at least one element is also recursive. is_non_nested returns TRUE for atomic objects and recursive objects where no elements are recursive. The assert_* functions return nothing but throw an error if the corresponding is_* function returns FALSE.

See Also

is.atomic and is.vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
atomic_types <- list(
  logical(),
  integer(),
  numeric(), 
  complex(),
  character(), 
  raw(),
  matrix(), 
  array(),
  factor(),
  NULL
)
for(var in atomic_types) assert_is_atomic(var)

recursive_types <- list(
  list(), 
  expression(),
  data.frame(), 
  y ~ x,
  function(){},
  call("sin", "pi")
)
for(var in recursive_types) assert_is_recursive(var)

# Names are neither atomic nor recursive
a_name <- as.name("x")
is_atomic(a_name)
is_recursive(a_name)

vector_types <- c(
  atomic_types[1:6], 
  recursive_types[1:2]
)
for(var in vector_types) assert_is_vector(var)

# Nested objects are recursive and have at least one recursive element
nested_list <- list(a = 1, b = list(2:3))
assert_is_nested(nested_list)
for(elt in nested_list) assert_is_non_nested(elt)