unnestList: Un-nest a nested list into a simple list

unnestListR Documentation

Un-nest a nested list into a simple list

Description

Un-nest a nested list into a simple list

Usage

unnestList(
  x,
  addNames = FALSE,
  unnamedBase = "x",
  parentName = NULL,
  sep = ".",
  makeNamesFunc = makeNames,
  stopClasses = c("dendrogram", "data.frame", "matrix", "package_version", "tbl",
    "data.table"),
  extraStopClasses = getOption("jam.stopClasses"),
  ...
)

Arguments

x

list potentially containing nested lists.

addNames

logical indicating whether to add names to the list elements when names are not already present. When addNames=TRUE and no names are present unnamedBase is used to define names.

unnamedBase

character value used as a base for naming any un-named lists, using the format makeNamesFunc(rep(unnamedBase, n)).

sep

character delimiter used between nested list names.

makeNamesFunc

function that takes a character vector and returns non-duplicated character vector of equal length. By default it uses jamba::makeNames().

stopClasses

vector of classes that should not be un-nested, useful in case some classes inherit list properties.

extraStopClasses

vector of additional values for stopClasses, created mostly to show that options("jam.stopClasses") can be used to define stopClasses, for example when this function is called but where arguments cannot be conveniently passed through the calling function.

...

additional arguments are ignored.

Details

This function inspects a list, and unlists each entry resulting in a simple list of non-list entries as a result. Sometimes when concatenating lists together, one list gets added as a list-of-lists. This function resolves that problem by providing one flat list.

Value

list that has been flattened so that it contains no list elements. Note that it may contain some list-like objects such as data.frame, defined by stopClasses.

See Also

Other jam list functions: cPasteSU(), cPasteS(), cPasteUnique(), cPasteU(), cPaste(), heads(), jam_rapply(), list2df(), mergeAllXY(), mixedSorts(), rbindList(), relist_named(), rlengths(), sclass(), sdim(), uniques()

Examples

L <- list(A=letters[1:10],
   B=list(C=LETTERS[3:9], D=letters[4:11]),
   E=list(F=list(G=LETTERS[3:9], D=letters[4:11])));
L;

# inspect the data using str()
str(L);

unnestList(L);

# optionally change the delimiter
unnestList(L, sep="|");

# example with nested lists of data.frame objects
df1 <- data.frame(a=1:2, b=letters[3:4]);
DFL <- list(A=df1,
   B=list(C=df1, D=df1),
   E=list(F=list(G=df1, D=df1)));
str(DFL);
unnestList(DFL);
str(unnestList(DFL));

# packageVersion() returns class "package_version"
# where is.list(packageVersion("base")) is TRUE,
# but it cannot ever be subsetted as a list with x[[1]],
# and thus it breaks this function
identical(is.list(packageVersion("base")), is.list(packageVersion("base"))[[1]])
unnestList(lapply(nameVector(c("base","graphics")), packageVersion))


jmw86069/jamba documentation built on March 26, 2024, 5:26 a.m.