Learn R Programming

crank (version 1.1-2)

listBuilder: 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.

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.

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.

See Also

list

Examples

Run this code
# NOT RUN {
 # 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)
# }

Run the code above in your browser using DataLab