| obj_is_list {vctrs} | R Documentation |
List checks
Description
-
obj_is_list()tests ifxis considered a list in the vctrs sense. It returnsTRUEif all of the following hold:-
xmust have list storage, i.e.typeof(x)returns"list" -
xmust not have adimattribute -
xmust not have aclassattribute, or must explicitly inherit from"list"as the last class
-
-
list_all_vectors()takes a list and returnsTRUEif all elements of that list are vectors. -
list_all_size()takes a list and returnsTRUEif all elements of that list have the samesize. -
list_all_recyclable()takes a list and returnsTRUEif all elements of that list can recycle tosize. -
obj_check_list(),list_check_all_vectors(),list_check_all_size(), andlist_check_all_recyclable()use the above functions, but throw a standardized and informative error if they returnFALSE.
Usage
obj_is_list(x)
obj_check_list(x, ..., arg = caller_arg(x), call = caller_env())
list_all_vectors(x, ..., allow_null = FALSE)
list_check_all_vectors(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
list_all_size(x, size, ..., allow_null = FALSE)
list_check_all_size(
x,
size,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
list_all_recyclable(x, size, ..., allow_null = FALSE)
list_check_all_recyclable(
x,
size,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
Arguments
x |
For |
... |
These dots are for future extensions and must be empty. |
arg |
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem. |
call |
The execution environment of a currently
running function, e.g. |
allow_null |
Whether |
size |
The size to check each element for compatibility with. |
Details
Notably, data frames and S3 record style classes like POSIXlt are not considered lists.
See Also
Examples
obj_is_list(list())
obj_is_list(list_of(1))
obj_is_list(data.frame())
list_all_vectors(list(1, mtcars))
list_all_vectors(list(1, environment()))
list_all_size(list(1:2, 2:3), 2)
list_all_size(list(1:2, 2:4), 2)
list_all_recyclable(list(1, 2:3), 2)
list_all_recyclable(list(1, 2:4), 2)
# `list_`-prefixed functions assume a list:
try(list_all_vectors(environment()))
# `NULL` elements are not considered vectors and generally have a size of 0
try(list_check_all_vectors(list(1, NULL, 2)))
try(list_check_all_size(list(1, NULL, 2), size = 1))
# However, it is often useful to perform upfront vector/size checks on a
# list, excluding `NULL`s, and then filter them out later on
list_check_all_vectors(list(1, NULL, 2), allow_null = TRUE)
list_check_all_size(list(1, NULL, 2), size = 1, allow_null = TRUE)
# Performing the checks before removing `NULL`s from the list ensures that
# any errors report the correct index. Note how the index is incorrect from a
# user's point of view if we filter out `NULL` too soon.
xs <- list(1, NULL, 2:3)
try(list_check_all_size(xs, size = 1, allow_null = TRUE))
xs <- vec_slice(xs, !vec_detect_missing(xs))
try(list_check_all_size(xs, size = 1))