data(mtcars)
stopifnot(
# Atomic vectors don't inherit from lists.
identical(enlist(1:3), list(1:3)),
# Atomic vectors are not lists internally.
identical(enlist(1:3, "list"), list(1:3)),
# Atomic vectors are a type of R vector.
identical(enlist(1:3, "vector"), 1:3),
# Data frames don't inherit from lists.
identical(enlist(mtcars), list(mtcars)),
# Data frames are lists internally.
identical(enlist(mtcars, "list"), mtcars),
# Data frames are not considered R vectors.
identical(enlist(mtcars, "vector"), list(mtcars))
)
# We treat something as a "list" if its first element is odd.
is.odd <- function(x) as.logical(x[1] %% 2)
stopifnot(
# 1 is a list.
identical(enlist(1, is.odd), 1),
# 2 is not.
identical(enlist(2, is.odd), list(2))
)
Run the code above in your browser using DataLab