Node or an entire data.tree structureR/node_methods_sideeffect.R
Sort.RdYou can sort with respect to any argument of the tree. But note that sorting has side-effects, meaning that you modify the underlying, original data.tree object structure.
Sort(node, attribute, ..., decreasing = FALSE, recursive = TRUE)The node whose children are to be sorted
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)
any parameters to be passed on the the attribute (in case it's a method or a function)
sort order
if TRUE, Sort will be called recursively on the Node's children.
This allows sorting an entire tree.
Returns the node on which Sort is called, invisibly. This can be useful to chain Node methods.
data(acme)
acme$Do(function(x) x$totalCost <- Aggregate(x, "cost", sum), traversal = "post-order")
Sort(acme, "totalCost", decreasing = FALSE)
print(acme, "totalCost")
#> levelName totalCost
#> 1 Acme Inc. 4950000
#> 2 ¦--IT 700000
#> 3 ¦ ¦--Switch to R 50000
#> 4 ¦ ¦--Go agile 250000
#> 5 ¦ °--Outsource 400000
#> 6 ¦--Accounting 1500000
#> 7 ¦ ¦--New Accounting Standards 500000
#> 8 ¦ °--New Software 1000000
#> 9 °--Research 2750000
#> 10 ¦--New Labs 750000
#> 11 °--New Product Line 2000000