Clean lists
list_clean.RdRecursively remove NULL and zero-length elements from a list.
Value
If inherits(x, "list") is FALSE (for example, a data.frame, an atomic vector, NULL, or any S3 object whose class vector does not contain "list"), x is returned unchanged.
Otherwise, a list is returned with NULL, zero-length, and recursively-empty elements removed at every level of nesting.
Examples
#----------------------------------------------------------------------------
# list_clean() examples
#----------------------------------------------------------------------------
library(bkbase)
# Removes NULL and zero-length elements, preserves names
x <- list(a = 1, b = NULL, c = logical(0L), d = 2)
list_clean(x)
#> $a
#> [1] 1
#>
#> $d
#> [1] 2
#>
# Recursive cleaning, including fully-empty sublists
y <- list(a = list(1, NULL), b = list(NULL, logical(0L)))
list_clean(y)
#> $a
#> $a[[1]]
#> [1] 1
#>
#>
# Non-list inputs are returned unchanged
list_clean(data.frame(x = 1:3))
#> x
#> 1 1
#> 2 2
#> 3 3
list_clean(1:5)
#> [1] 1 2 3 4 5
list_clean(NULL)
#> NULL