listBuilder {crank} | R Documentation |
Build a possibly nested list.
Description
Build a possibly nested list using the result of a function.
Usage
listBuilder(x,FUN=NULL,fargs=NULL)
Arguments
x |
The object that will be the first argument of ‘FUN’, or a possibly nested list of such objects. |
FUN |
A function that can accept ‘x’ as its first argument. |
fargs |
A list of the remaining arguments to ‘FUN’. |
Details
‘listBuilder’ descends the list structure of ‘x’ if it is a list until it encounters a non-list element. It then passes that element as the first argument to ‘FUN’ and returns the value of ‘FUN’. This may be a list of elements, replacing the original element, hence the name.
Value
If ‘x’ is not a list and ‘FUN’ is NULL, ‘x’ is returned. If ‘FUN’ creates a list from one or more elements of ‘x’, a list or nested list will be returned. Successive calls to ‘listBuilder’ can rapidly create very large, deeply nested list structures.
Author(s)
Jim Lemon
See Also
Examples
# define a function that splits a vector into a list
splitvec<-function(x) {
xlen<-length(x)
if(xlen > 1) {
newx<-vector("list",xlen)
for(newlist in 1:xlen) newx[[newlist]]<-x[newlist]
return(newx)
}
return(x)
}
testlist<-list(c(9,16),list(25,c(36,49)))
listBuilder(testlist,splitvec)