If a node field contains data of length > 1, then that is converted into a string in the data.frame.

# S3 method for class 'Node'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"),
  pruneFun = NULL,
  filterFun = NULL,
  format = FALSE,
  inheritFromAncestors = FALSE
)

ToDataFrameTree(x, ..., pruneFun = NULL)

ToDataFrameTable(x, ..., pruneFun = NULL)

ToDataFrameNetwork(
  x,
  ...,
  direction = c("climb", "descend"),
  pruneFun = NULL,
  format = FALSE,
  inheritFromAncestors = FALSE
)

ToDataFrameTypeCol(x, ..., type = "level", prefix = type, pruneFun = NULL)

Arguments

x

The root Node of the tree or sub-tree to be convert to a data.frame

row.names

NULL or a character vector giving the row names for the data frame. Missing values are not allowed.

optional

logical. If TRUE, setting row names and converting column names (to syntactic names: see make.names) is optional.

...

the attributes to be added as columns of the data.frame. See Get for details. If a specific Node does not contain the attribute, NA is added to the data.frame.

traversal

any of 'pre-order' (the default), 'post-order', 'in-order', 'level', or 'ancestor'. See Traverse for details.

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.

filterFun

a function taking a Node as an argument. See Traverse for details.

format

if FALSE (the default), then no formatting will be applied. If TRUE, then the first formatter (if any) along the ancestor path is used for formatting.

inheritFromAncestors

if FALSE, and if the attribute is a field or a method, then only a Node itself is searched for the field/method. If TRUE, and if the Node does not contain the attribute, then ancestors are also searched.

direction

when converting to a network, should the edges point from root to children ("climb") or from child to parent ("descend")?

type

when converting type columns, the type is the discriminator, i.e. an attribute (e.g. field name) of each node

prefix

when converting type columns, the prefix used for the column names. Can be NULL to omit prefixes.

Value

ToDataFrameTree: a data.frame, where each row represents a Node in the tree or sub-tree spanned by x, possibly pruned according to pruneFun.

ToDataFrameTable: a data.frame, where each row represents a leaf Node in the tree or sub-tree spanned by x, possibly pruned according to pruneFun.

ToDataFrameNetwork: a data.frame, where each row represents a Node in the tree or sub-tree spanned by x, possibly pruned according to pruneFun. The first column is called 'from', while the second is called 'to', describing the parent to child edge (for direction "climb") or the child to parent edge (for direction "descend"). If AreNamesUnique is TRUE, then the Network is based on the Node$name, otherwise on the Node$pathString

ToDataFrameTypeCol: a data.frame in table format (i.e. where each row represents a leaf in the tree or sub-tree spanned by x), possibly pruned according to pruneFun. In addition to ..., each distinct type is output to a column.

Examples

data(acme)
acme$attributesAll
#> [1] "cost" "p"   
as.data.frame(acme, row.names = NULL, optional = FALSE, "cost", "p")
#>                           levelName    cost    p
#> 1  Acme Inc.                             NA   NA
#> 2   ¦--Accounting                        NA   NA
#> 3   ¦   ¦--New Software             1000000 0.50
#> 4   ¦   °--New Accounting Standards  500000 0.75
#> 5   ¦--Research                          NA   NA
#> 6   ¦   ¦--New Product Line         2000000 0.25
#> 7   ¦   °--New Labs                  750000 0.90
#> 8   °--IT                                NA   NA
#> 9       ¦--Outsource                 400000 0.20
#> 10      ¦--Go agile                  250000 0.05
#> 11      °--Switch to R                50000 1.00

ToDataFrameTree(acme, "cost", "p")
#>                           levelName    cost    p
#> 1  Acme Inc.                             NA   NA
#> 2   ¦--Accounting                        NA   NA
#> 3   ¦   ¦--New Software             1000000 0.50
#> 4   ¦   °--New Accounting Standards  500000 0.75
#> 5   ¦--Research                          NA   NA
#> 6   ¦   ¦--New Product Line         2000000 0.25
#> 7   ¦   °--New Labs                  750000 0.90
#> 8   °--IT                                NA   NA
#> 9       ¦--Outsource                 400000 0.20
#> 10      ¦--Go agile                  250000 0.05
#> 11      °--Switch to R                50000 1.00
ToDataFrameNetwork(acme, "cost", "p", direction = "climb")
#>          from                       to    cost    p
#> 1   Acme Inc.               Accounting      NA   NA
#> 2   Acme Inc.                 Research      NA   NA
#> 3   Acme Inc.                       IT      NA   NA
#> 4  Accounting             New Software 1000000 0.50
#> 5  Accounting New Accounting Standards  500000 0.75
#> 6    Research         New Product Line 2000000 0.25
#> 7    Research                 New Labs  750000 0.90
#> 8          IT                Outsource  400000 0.20
#> 9          IT                 Go agile  250000 0.05
#> 10         IT              Switch to R   50000 1.00
ToDataFrameTable(acme, "cost", "p")
#>      cost    p
#> 1 1000000 0.50
#> 2  500000 0.75
#> 3 2000000 0.25
#> 4  750000 0.90
#> 5  400000 0.20
#> 6  250000 0.05
#> 7   50000 1.00
ToDataFrameTypeCol(acme)
#>     level_1    level_2                  level_3
#> 1 Acme Inc. Accounting             New Software
#> 2 Acme Inc. Accounting New Accounting Standards
#> 3 Acme Inc.   Research         New Product Line
#> 4 Acme Inc.   Research                 New Labs
#> 5 Acme Inc.         IT                Outsource
#> 6 Acme Inc.         IT                 Go agile
#> 7 Acme Inc.         IT              Switch to R

#use the pruneFun:
acme$Do(function(x) x$totalCost <- Aggregate(x, "cost", sum), traversal = "post-order")
ToDataFrameTree(acme, "totalCost", pruneFun = function(x) x$totalCost > 300000)
#>                          levelName totalCost
#> 1 Acme Inc.                          4950000
#> 2  ¦--Accounting                     1500000
#> 3  ¦   ¦--New Software               1000000
#> 4  ¦   °--New Accounting Standards    500000
#> 5  ¦--Research                       2750000
#> 6  ¦   ¦--New Product Line           2000000
#> 7  ¦   °--New Labs                    750000
#> 8  °--IT                              700000
#> 9      °--Outsource                   400000

#inherit
acme$Set(floor = c(1, 2, 3), filterFun = function(x) x$level == 2)
as.data.frame(acme, row.names = NULL, optional = FALSE, "floor", inheritFromAncestors = FALSE)
#>                           levelName floor
#> 1  Acme Inc.                           NA
#> 2   ¦--Accounting                       1
#> 3   ¦   ¦--New Software                NA
#> 4   ¦   °--New Accounting Standards    NA
#> 5   ¦--Research                         2
#> 6   ¦   ¦--New Product Line            NA
#> 7   ¦   °--New Labs                    NA
#> 8   °--IT                               3
#> 9       ¦--Outsource                   NA
#> 10      ¦--Go agile                    NA
#> 11      °--Switch to R                 NA
as.data.frame(acme, row.names = NULL, optional = FALSE, "floor", inheritFromAncestors = TRUE)
#>                           levelName floor
#> 1  Acme Inc.                           NA
#> 2   ¦--Accounting                       1
#> 3   ¦   ¦--New Software                 1
#> 4   ¦   °--New Accounting Standards     1
#> 5   ¦--Research                         2
#> 6   ¦   ¦--New Product Line             2
#> 7   ¦   °--New Labs                     2
#> 8   °--IT                               3
#> 9       ¦--Outsource                    3
#> 10      ¦--Go agile                     3
#> 11      °--Switch to R                  3

#using a function as an attribute:
acme$Accounting$Head <- "Mrs. Numright"
acme$Research$Head <- "Mr. Stein"
acme$IT$Head <- "Mr. Squarehead"
ToDataFrameTable(acme, department = function(x) x$parent$name, "name", "Head", "cost")
#>   department                     name           Head    cost
#> 1 Accounting             New Software  Mrs. Numright 1000000
#> 2 Accounting New Accounting Standards  Mrs. Numright  500000
#> 3   Research         New Product Line      Mr. Stein 2000000
#> 4   Research                 New Labs      Mr. Stein  750000
#> 5         IT                Outsource Mr. Squarehead  400000
#> 6         IT                 Go agile Mr. Squarehead  250000
#> 7         IT              Switch to R Mr. Squarehead   50000

#complex TypeCol
acme$IT$Outsource$AddChild("India")
acme$IT$Outsource$AddChild("Poland")
acme$Set(type = c('company', 'department', 'project', 'project', 'department',
                  'project', 'project', 'department', 'program', 'project',
                  'project', 'project', 'project'
                  )
        )
print(acme, 'type')
#>                           levelName       type
#> 1  Acme Inc.                           company
#> 2   ¦--Accounting                   department
#> 3   ¦   ¦--New Software                project
#> 4   ¦   °--New Accounting Standards    project
#> 5   ¦--Research                     department
#> 6   ¦   ¦--New Product Line            project
#> 7   ¦   °--New Labs                    project
#> 8   °--IT                           department
#> 9       ¦--Outsource                   program
#> 10      ¦   ¦--India                   project
#> 11      ¦   °--Poland                  project
#> 12      ¦--Go agile                    project
#> 13      °--Switch to R                 project
ToDataFrameTypeCol(acme, type = 'type')
#>   type_company type_department type_program             type_project
#> 1    Acme Inc.      Accounting         <NA>             New Software
#> 2    Acme Inc.      Accounting         <NA> New Accounting Standards
#> 3    Acme Inc.        Research         <NA>         New Product Line
#> 4    Acme Inc.        Research         <NA>                 New Labs
#> 5    Acme Inc.              IT    Outsource                    India
#> 6    Acme Inc.              IT    Outsource                   Poland
#> 7    Acme Inc.              IT         <NA>                 Go agile
#> 8    Acme Inc.              IT         <NA>              Switch to R