data.tree Structure With NodesNode is at the very heart of the data.tree package. All trees are constructed
by tying toghether Node objects.
# n1 <- Node$new("Node 1")An R6Class generator object
childrenA list of child Nodes
parentThe node's parent Node
Node$new(name)Creates a new Node called name. Often used to construct the root when creating trees programmatically.
AddChild(name)Creates a new Node called name and adds it to this Node as a child.
AddChildNode(node)Adds a Node as a child.
AddSibling(name)Creates a new Node called name and adds it after this Node as a sibling.
AddSiblingNode(sibling)Adds a new Node after this Node, as a sibling.
RemoveChild(name)Remove the child Node called name from a Node and returns it.
RemoveAttribute(name, stopIfNotAvailable)Removes attribute called name from this Node. Gives an error if stopIfNotAvailable and the attribute doesn't exist.
Climb(...)Find a node with path ..., where the ... arguments are the names of the Nodes, or other field values.
Navigate(path)Find a node by relative path
FindNode(name)Find a node with name name. Especially useful if AreNamesUnique is TRUE
Get(attribute, ..., traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), pruneFun = NULL, filterFun = NULL, format = NULL, inheritFromAncestors = FALSE, simplify = c(TRUE, FALSE, "array", "regular"))Traverses the tree and collects values along the way.
Do(fun, ..., traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), pruneFun = NULL, filterFun = NUL)Traverses the tree and call fun on each node.
Set(..., traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), pruneFun = NULL, filterFun = NULL)Traverses the tree and assigns the args along the way, recycling the args.
Sort(attribute, ..., decreasing = FALSE, recursive = TRUESort children of a node with respect to an attribute (field, method, active, function)
Revert(recursive = TRUE)Revert the sort order of a node
Prune(pruneFun)Prune a tree. The pruneFun takes a node as its first argument, and returns TRUE if the node should be kept, FALSE otherwise
nameGets or sets the name of a Node. For example Node$name <- "Acme"
parentGets or sets the parent Node of a Node. Only set this if you know what you are doing, as you might mess up the tree structure!
childrenGets or sets the children list of a Node. Only set this if you know what you are doing, as you might mess up the tree structure!
siblingsReturns a list of the siblings of this Node
fieldsGets the names of the set properties of a Node
fieldsAllGets the names of the set properties of a Node or any of its sub-Nodes
isLeafReturns TRUE if the Node is a leaf, FALSE otherwise
isRootReturns TRUE if the Node is the root, FALSE otherwise
countReturns the number of children of a Node
totalCountReturns the total number of Nodes in the tree
pathReturns a vector of mode character containing the names of the Nodes in the path from the root to this Node
pathStringReturns a string representing the path to this Node, separated by backslash
levelNameReturns the name of the Node, preceded by level times '*'. Useful for printing.
leafCountReturns the number of leaves are below a Node
leavesReturns a list containing all the leaf Nodes
levelReturns an integer representing the level of a Node. For example, the root has level 1.
heightReturns max(level) of any of the Nodes of the tree
averageBranchingFactorReturns the average number of crotches below this Node
rootReturns the root Node of a Node's tree
Assemble Node objects into a data.tree
structure and use the traversal methods to set, get, and perform operations on it. Typically, you construct larger tree
structures by converting from data.frame, list, or other formats.
For more details see the data.tree documentations, or the data.tree vignette: vignette("data.tree")
# NOT RUN {
library(data.tree)
acme <- Node$new("Acme Inc.")
accounting <- acme$AddChild("Accounting")$
AddSibling("Research")$
AddChild("New Labs")$
parent$
AddSibling("IT")$
AddChild("Outsource")
print(acme)
# }
Run the code above in your browser using DataLab