is_list_of: Test if an input is a list of something

Description Usage Arguments Value See Also Examples

View source: R/is_list_of.R

Description

Tests if all elements of a list are a specific type. is_list_of accepts any test function. is_list_of_lists() tests if all elements are lists. is_list_of_characters() tests if all elements are character vectors.

Usage

1
2
3
4
5

Arguments

input

List to test

test

Test function to apply to each element of input. Either a function name, an anonymous function, or a purrr-style lambda function

Value

A logical value. If input is not a list, FALSE and a warning.

See Also

is.list() to test if an input is a list

purrr::flatten() or rlang::flatten() to remove a level of hierarchy from a list

rlang::squash() to remove all levels of hierarchy from a list

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
list_of_lists      <- list(as.list(letters[1:4]), as.list(1:4))
list_of_characters <- list(letters[1:4], LETTERS[1:4])
list_of_integers   <- list(1:4, 5:8)

# Test if all elements of a list are lists
is_list_of_lists(list_of_lists)
is_list_of_lists(list_of_characters)
is_list_of_lists(list_of_integers)

# Test if all elements of a list are character vectors
is_list_of_characters(list_of_lists)
is_list_of_characters(list_of_characters)
is_list_of_characters(list_of_integers)

# Use any test function to test all elements of a list...
# ... with a function name
is_list_of(list_of_integers, is.integer)
# ... an anonymous function
is_list_of(list_of_integers, function(x) all(x %% 1 == 0))
# ... or a purrr-style lambda
is_list_of(list_of_integers, ~ all(. %% 1 == 0))

# All functions give a warning and return FALSE if their input is not a list
is_list_of_lists(1:4)
is_list_of_characters(1:4)
is_list_of(1:4, is.integer)

rossellhayes/leafpeepr documentation built on Feb. 29, 2020, 12:48 a.m.