Convert a data.tree structure to a list-of-list structure

# S3 method for class 'Node'
as.list(
  x,
  mode = c("simple", "explicit"),
  unname = FALSE,
  nameName = ifelse(unname, "name", ""),
  childrenName = "children",
  rootName = "",
  keepOnly = NULL,
  pruneFun = NULL,
  ...
)

ToListSimple(x, nameName = "name", pruneFun = NULL, ...)

ToListExplicit(
  x,
  unname = FALSE,
  nameName = ifelse(unname, "name", ""),
  childrenName = "children",
  pruneFun = NULL,
  ...
)

Arguments

x

The Node to convert

mode

How the list is structured. "simple" (the default) will add children directly as nested lists. "explicit" puts children in a separate nested list called childrenName

unname

If TRUE, and if mode is "explicit", then the nested children list will not have named arguments. This can be useful e.g. in the context of conversion to JSON, if you prefer the children to be an array rather than named objects.

nameName

The name that should be given to the name element

childrenName

The name that should be given to the children nested list

rootName

The name of the node. If provided, this overrides Node$name

keepOnly

A character vector of attributes to include in the result. If NULL (the default), all attributes are kept.

pruneFun

allows providing a prune criteria, i.e. a function taking a Node as an input, and returning TRUE or FALSE. If the pruneFun returns FALSE for a Node, then the Node and its entire sub-tree will not be considered.

...

Additional parameters passed to as.list.Node

Examples

data(acme)

str(ToListSimple(acme))
#> List of 4
#>  $ name      : chr "Acme Inc."
#>  $ Accounting:List of 3
#>   ..$ name                    : chr "Accounting"
#>   ..$ New Software            :List of 3
#>   .. ..$ name: chr "New Software"
#>   .. ..$ cost: num 1e+06
#>   .. ..$ p   : num 0.5
#>   ..$ New Accounting Standards:List of 3
#>   .. ..$ name: chr "New Accounting Standards"
#>   .. ..$ cost: num 5e+05
#>   .. ..$ p   : num 0.75
#>  $ Research  :List of 3
#>   ..$ name            : chr "Research"
#>   ..$ New Product Line:List of 3
#>   .. ..$ name: chr "New Product Line"
#>   .. ..$ cost: num 2e+06
#>   .. ..$ p   : num 0.25
#>   ..$ New Labs        :List of 3
#>   .. ..$ name: chr "New Labs"
#>   .. ..$ cost: num 750000
#>   .. ..$ p   : num 0.9
#>  $ IT        :List of 4
#>   ..$ name       : chr "IT"
#>   ..$ Outsource  :List of 3
#>   .. ..$ name: chr "Outsource"
#>   .. ..$ cost: num 4e+05
#>   .. ..$ p   : num 0.2
#>   ..$ Go agile   :List of 3
#>   .. ..$ name: chr "Go agile"
#>   .. ..$ cost: num 250000
#>   .. ..$ p   : num 0.05
#>   ..$ Switch to R:List of 3
#>   .. ..$ name: chr "Switch to R"
#>   .. ..$ cost: num 50000
#>   .. ..$ p   : num 1
str(ToListSimple(acme, keepOnly = "cost"))
#> List of 4
#>  $ name      : chr "Acme Inc."
#>  $ Accounting:List of 3
#>   ..$ name                    : chr "Accounting"
#>   ..$ New Software            :List of 2
#>   .. ..$ name: chr "New Software"
#>   .. ..$ cost: num 1e+06
#>   ..$ New Accounting Standards:List of 2
#>   .. ..$ name: chr "New Accounting Standards"
#>   .. ..$ cost: num 5e+05
#>  $ Research  :List of 3
#>   ..$ name            : chr "Research"
#>   ..$ New Product Line:List of 2
#>   .. ..$ name: chr "New Product Line"
#>   .. ..$ cost: num 2e+06
#>   ..$ New Labs        :List of 2
#>   .. ..$ name: chr "New Labs"
#>   .. ..$ cost: num 750000
#>  $ IT        :List of 4
#>   ..$ name       : chr "IT"
#>   ..$ Outsource  :List of 2
#>   .. ..$ name: chr "Outsource"
#>   .. ..$ cost: num 4e+05
#>   ..$ Go agile   :List of 2
#>   .. ..$ name: chr "Go agile"
#>   .. ..$ cost: num 250000
#>   ..$ Switch to R:List of 2
#>   .. ..$ name: chr "Switch to R"
#>   .. ..$ cost: num 50000

str(ToListExplicit(acme))
#> List of 2
#>  $ name    : chr "Acme Inc."
#>  $ children:List of 3
#>   ..$ Accounting:List of 1
#>   .. ..$ children:List of 2
#>   .. .. ..$ New Software            :List of 2
#>   .. .. .. ..$ cost: num 1e+06
#>   .. .. .. ..$ p   : num 0.5
#>   .. .. ..$ New Accounting Standards:List of 2
#>   .. .. .. ..$ cost: num 5e+05
#>   .. .. .. ..$ p   : num 0.75
#>   ..$ Research  :List of 1
#>   .. ..$ children:List of 2
#>   .. .. ..$ New Product Line:List of 2
#>   .. .. .. ..$ cost: num 2e+06
#>   .. .. .. ..$ p   : num 0.25
#>   .. .. ..$ New Labs        :List of 2
#>   .. .. .. ..$ cost: num 750000
#>   .. .. .. ..$ p   : num 0.9
#>   ..$ IT        :List of 1
#>   .. ..$ children:List of 3
#>   .. .. ..$ Outsource  :List of 2
#>   .. .. .. ..$ cost: num 4e+05
#>   .. .. .. ..$ p   : num 0.2
#>   .. .. ..$ Go agile   :List of 2
#>   .. .. .. ..$ cost: num 250000
#>   .. .. .. ..$ p   : num 0.05
#>   .. .. ..$ Switch to R:List of 2
#>   .. .. .. ..$ cost: num 50000
#>   .. .. .. ..$ p   : num 1
str(ToListExplicit(acme, unname = TRUE))
#> List of 2
#>  $ name    : chr "Acme Inc."
#>  $ children:List of 3
#>   ..$ :List of 2
#>   .. ..$ name    : chr "Accounting"
#>   .. ..$ children:List of 2
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "New Software"
#>   .. .. .. ..$ cost: num 1e+06
#>   .. .. .. ..$ p   : num 0.5
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "New Accounting Standards"
#>   .. .. .. ..$ cost: num 5e+05
#>   .. .. .. ..$ p   : num 0.75
#>   ..$ :List of 2
#>   .. ..$ name    : chr "Research"
#>   .. ..$ children:List of 2
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "New Product Line"
#>   .. .. .. ..$ cost: num 2e+06
#>   .. .. .. ..$ p   : num 0.25
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "New Labs"
#>   .. .. .. ..$ cost: num 750000
#>   .. .. .. ..$ p   : num 0.9
#>   ..$ :List of 2
#>   .. ..$ name    : chr "IT"
#>   .. ..$ children:List of 3
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "Outsource"
#>   .. .. .. ..$ cost: num 4e+05
#>   .. .. .. ..$ p   : num 0.2
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "Go agile"
#>   .. .. .. ..$ cost: num 250000
#>   .. .. .. ..$ p   : num 0.05
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ name: chr "Switch to R"
#>   .. .. .. ..$ cost: num 50000
#>   .. .. .. ..$ p   : num 1
str(ToListExplicit(acme, unname = TRUE, nameName = "id", childrenName = "descendants"))
#> List of 2
#>  $ id         : chr "Acme Inc."
#>  $ descendants:List of 3
#>   ..$ :List of 2
#>   .. ..$ id         : chr "Accounting"
#>   .. ..$ descendants:List of 2
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "New Software"
#>   .. .. .. ..$ cost: num 1e+06
#>   .. .. .. ..$ p   : num 0.5
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "New Accounting Standards"
#>   .. .. .. ..$ cost: num 5e+05
#>   .. .. .. ..$ p   : num 0.75
#>   ..$ :List of 2
#>   .. ..$ id         : chr "Research"
#>   .. ..$ descendants:List of 2
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "New Product Line"
#>   .. .. .. ..$ cost: num 2e+06
#>   .. .. .. ..$ p   : num 0.25
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "New Labs"
#>   .. .. .. ..$ cost: num 750000
#>   .. .. .. ..$ p   : num 0.9
#>   ..$ :List of 2
#>   .. ..$ id         : chr "IT"
#>   .. ..$ descendants:List of 3
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "Outsource"
#>   .. .. .. ..$ cost: num 4e+05
#>   .. .. .. ..$ p   : num 0.2
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "Go agile"
#>   .. .. .. ..$ cost: num 250000
#>   .. .. .. ..$ p   : num 0.05
#>   .. .. ..$ :List of 3
#>   .. .. .. ..$ id  : chr "Switch to R"
#>   .. .. .. ..$ cost: num 50000
#>   .. .. .. ..$ p   : num 1