is_empty: Is the input empty/scalar?

View source: R/is-empty-scalar.R

assert_has_elementsR Documentation

Is the input empty/scalar?

Description

Checks to see if the input has length zero/one.

Usage

assert_has_elements(x, n, severity = getOption("assertive.severity", "stop"))

assert_is_empty(
  x,
  metric = c("length", "elements"),
  severity = getOption("assertive.severity", "stop")
)

assert_is_non_empty(
  x,
  metric = c("length", "elements"),
  severity = getOption("assertive.severity", "stop")
)

assert_is_non_scalar(
  x,
  metric = c("length", "elements"),
  severity = getOption("assertive.severity", "stop")
)

assert_is_of_dimension(
  x,
  n,
  severity = getOption("assertive.severity", "stop")
)

assert_is_of_length(x, n, severity = getOption("assertive.severity", "stop"))

assert_is_scalar(
  x,
  metric = c("length", "elements"),
  severity = getOption("assertive.severity", "stop")
)

is_empty(x, metric = c("length", "elements"), .xname = get_name_in_parent(x))

is_non_empty(
  x,
  metric = c("length", "elements"),
  .xname = get_name_in_parent(x)
)

is_non_scalar(
  x,
  metric = c("length", "elements"),
  .xname = get_name_in_parent(x)
)

is_scalar(x, metric = c("length", "elements"), .xname = get_name_in_parent(x))

has_elements(x, n, .xname = get_name_in_parent(x))

is_of_dimension(x, n, .xname = get_name_in_parent(x))

is_of_length(x, n, .xname = get_name_in_parent(x))

Arguments

x

Input to check.

n

Non-negative integer(s) of the expected length/number of elements/ lengths of dimensions. See note.

severity

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

metric

A string. Should be length or the number of elements be used to determine if the object is empty/non-empty/scalar?

.xname

Not intended to be used directly.

Value

is_empty returns TRUE if the input has length zero. is_scalar returns TRUE if the input has length one. The assert_* functions return nothing but throw an error if the corresponding is_* function returns FALSE.

Note

For is_empty, is_non_empty and is_scalar, n should be an single integer representing either the expected length or the expected number of elements in x. For is_of_dimension n should be a vector of integers representing the expected lengths of dimensions.

See Also

length.

Examples

# is_of_length returns TRUE if the length of an object
# matches a specified value.
is_of_length(1:5, 5)
assert_is_of_length(1:5, 5)

# has_elements returns TRUE if an object has a specified
# number of elements.  This is usually the same thing.
has_elements(1:5, 5)
assert_has_elements(1:5, 5)

# Data frames and lists behave differently for length
# and number of elements.
d <- data.frame(x = 1:5, y = letters[1:5])
assert_is_of_length(d, 2)
assert_has_elements(d, 10)

l <- list(a = 1:5, b = list(b.a = 1:3, b.b = 1:7))
assert_is_of_length(l, 2)
assert_has_elements(l, 15)

# Functions always have length one, but may have lots of 
# elements.
assert_is_of_length(var, 1)
assert_has_elements(var, 54)

# is_scalar is a shortcut for length one, or one elements.
assert_is_scalar(99)
assert_is_scalar("Multiple words in a single string are scalar.")
assert_is_scalar(NA)

# The two metrics can yield different results!
is_scalar(list(1:5))
is_scalar(list(1:5), "elements")
is_scalar(var)
is_scalar(var, "elements")

# Similarly, is_empty is a shortcut for length zero/zero elements.
assert_is_empty(NULL)
assert_is_empty(numeric())
assert_is_non_empty(1:10)
assert_is_non_empty(NA)

# is_of_dimension tests the lengths of all dimensions.
assert_is_of_dimension(d, c(5, 2))
assert_is_of_dimension(l, NULL)

assertive.properties documentation built on April 21, 2022, 5:13 p.m.