The Get method is one of the most important ones of the data.tree package. It lets you traverse a tree and collect values along the way. Alternatively, you can call a method or a function on each Node.

# OO-style:
#node$Get(attribute, 
#        ..., 
#        traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), 
#        pruneFun = NULL, 
#        filterFun = NULL, 
#        format = FALSE, 
#        inheritFromAncestors = FALSE)
         
# traditional:
Get(nodes, 
    attribute, 
    ..., 
    format = FALSE, 
    inheritFromAncestors = FALSE, 
    simplify = c(TRUE, FALSE, "array", "regular"))

Arguments

nodes

The nodes on which to perform the Get (typically obtained via Traverse)

attribute

determines what is collected. The attribute can be

  • a.) the name of a field or a property/active of each Node in the tree, e.g. acme$Get("p") or acme$Get("position")

  • b.) the name of a method of each Node in the tree, e.g. acme$Get("levelZeroBased"), where e.g. acme$levelZeroBased <- function() acme$level - 1

  • c.) a function, whose first argument must be a Node e.g. acme$Get(function(node) node$cost * node$p)

...

in case the attribute is a function or a method, the ellipsis is passed to it as additional arguments.

format

if FALSE (the default), no formatting is being used. If TRUE, then the first formatter (if any) found along the ancestor path is being used for formatting (see SetFormat). If format is a function, then the collected value is passed to that function, and the result is returned.

inheritFromAncestors

if TRUE, then the path above a Node is searched to get the attribute in case it is NULL.

simplify

same as sapply, i.e. TRUE, FALSE or "array". Additionally, you can specify "regular" if each returned value is of length > 1, and equally named. See below for an example.

Value

a vector containing the atrributes collected during traversal, in traversal order. NULL is converted to NA, such that length(Node$Get) == Node$totalCount

See also

Examples

data(acme)
acme$Get("level")
#>                Acme Inc.               Accounting             New Software 
#>                        1                        2                        3 
#> New Accounting Standards                 Research         New Product Line 
#>                        3                        2                        3 
#>                 New Labs                       IT                Outsource 
#>                        3                        2                        3 
#>                 Go agile              Switch to R 
#>                        3                        3 
acme$Get("totalCount")
#>                Acme Inc.               Accounting             New Software 
#>                       11                        3                        1 
#> New Accounting Standards                 Research         New Product Line 
#>                        1                        3                        1 
#>                 New Labs                       IT                Outsource 
#>                        1                        4                        1 
#>                 Go agile              Switch to R 
#>                        1                        1 
 

acme$Get(function(node) node$cost * node$p,
         filterFun = isLeaf)
#>             New Software New Accounting Standards         New Product Line 
#>                   500000                   375000                   500000 
#>                 New Labs                Outsource                 Go agile 
#>                   675000                    80000                    12500 
#>              Switch to R 
#>                    50000 

#This is equivalent:
nodes <- Traverse(acme, filterFun = isLeaf)
Get(nodes, function(node) node$cost * node$p)
#>             New Software New Accounting Standards         New Product Line 
#>                   500000                   375000                   500000 
#>                 New Labs                Outsource                 Go agile 
#>                   675000                    80000                    12500 
#>              Switch to R 
#>                    50000 

   
#simplify = "regular" will preserve names
acme$Get(function(x) c(position = x$position, level = x$level), simplify = "regular")
#>          Acme Inc. Accounting New Software New Accounting Standards Research
#> position         1          1            1                        2        2
#> level            1          2            3                        3        2
#>          New Product Line New Labs IT Outsource Go agile Switch to R
#> position                1        2  3         1        2           3
#> level                   3        3  2         3        3           3