introspectors: Introspectors

Description Usage Arguments Details Value Checking NA values Checking NULL values Note Examples

Description

Check types and lengths of R objects.

Usage

 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
is_nul(x)

is_lgl(x, n = NULL, .na = TRUE)

is_int(x, n = NULL, .na = TRUE)

is_dbl(x, n = NULL, .na = TRUE)

is_num(x, n = NULL, .na = TRUE)

is_cpx(x, n = NULL, .na = TRUE)

is_chr(x, n = NULL, .na = TRUE)

is_ato(x, n = NULL, .na = TRUE)

is_lst(x, n = NULL, .na = TRUE)

is_scalar_lgl(x, .na = FALSE)

is_scalar_int(x, .na = FALSE)

is_scalar_dbl(x, .na = FALSE)

is_scalar_num(x, .na = FALSE)

is_scalar_cpx(x, .na = FALSE)

is_scalar_chr(x, .na = FALSE)

is_scalar_ato(x, .na = FALSE)

is_scalar_lst(x, .na = FALSE)

Arguments

x

[any]

Object to be tested.

n

[NULL | integer(1)]

The desired length of x. If NULL, length is not tested, only the type is. It is automatically coerced to an integer value via as.integer().

.na

[logical(1)]

Can x contain NA values? Beware! Validation of this argument is deferred to the underlying if statement.

Details

Types are abbreviated for convenience and consistency:

An atomic vector is either a NULL or any of these base types: "logical", "integer", "double", "complex", "character", and "raw". Raw vectors are out of scope.

What R sometimes call a numeric is just a double (or a "real") value: a double-precision vector. Package dotprofile sticks to the conventional "double" name. Therefore, functions is_num() and is_scalar_num() are respectively identical to functions is_dbl() and is_scalar_dbl().

Value

A logical(1).

Checking NA values

By default, NA values are valid values, except when a scalar value is expected. In that case, they are invalid. This assumption can easily be changed via argument .na.

When checking for NA values in a list, is_lst() and is_scalar_lst() do not recurse into their recursive elements.

Checking NULL values

It makes no sense to check for a NULL's length, because it is always equal to 0. Therefore, is_nul() is just an alias to function is.null(), and is provided for convenience.

Keep in mind that is_ato(NULL) yields TRUE because NULL is a degenerate atomic type. However, is_ato(NULL, 1L) always yields FALSE, because of NULL's implicit length.

Note

Introspector functions of dotprofile are equivalent to what rlang typically offers, both in terms of performance and convenience. However, managing R dependencies can be a big mess in production, so dotprofile tries hard to avoid relying on other packages when it is not absolutely required.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Check if a vector contains 3 double values.
is_dbl(c(1.0, 1.1, 1.2), 3L)

## Beware of R implicit conversions. This yields FALSE.
is_int(c(1L, 2, 3L))

## By default, NAs are accepted in vectors.
is_int(c(1L, NA_integer_))        # TRUE
is_int(c(1L, NA_integer_), FALSE) # FALSE

## By default, scalar values cannot be NA.
is_scalar_int(NA_integer_)       # FALSE
is_scalar_int(NA_integer_, TRUE) # TRUE

jeanmathieupotvin/dotprofile documentation built on Dec. 20, 2021, 10:08 p.m.