Description Usage Arguments Details Value Examples
This function allows for easy recursive function calling of tree-like structures. The recursion can happen either downwards, calling the children after the parent, or upwards, calling the parent after the children. At each recursion there is access to the node(s) that was/were called before and thus have already been through the function.
1 2 3 4 5 6 7 8 9 10 11 | tree_apply(tree, FUN, ...)
## Default S3 method:
tree_apply(tree, FUN, ...)
## S3 method for class 'igraph'
tree_apply(tree, FUN, direction = "down", mode = "out",
...)
## S3 method for class 'dendrogram'
tree_apply(tree, FUN, direction = "down", ...)
|
tree |
A tree-like object. Currently support for igraph objects |
FUN |
The function to apply to each node. The function must return a
modified version of the graph if |
... |
Additional parameters to |
direction |
The direction of the recursion. If |
mode |
For |
The function is called with a set of predifined parameters along
with user defined ones. If direction = 'down'
The parameters supplied
automatically to the function are: node
, parent
, depth
and tree
, while if direction = 'up'
the parameters are:
node
, children
, depth
and tree
. The nature of
node
, parent
and children
depends on the class of
tree
. If class(tree) == 'igraph'
they will be indices of the
relevant vertices in the graph. If class(tree) == 'dendrogram'
they
will be the actual dendrogram objects.
A modified version of tree
(if anything is modified in
FUN
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # We'll start with igraph
require(igraph)
gr <- graph_from_data_frame(flare$edges, vertices = flare$vertices)
# Set depth and a class based on the name of the 2nd level node name
gr <- tree_apply(gr, function(node, parent, depth, tree) {
tree <- set_vertex_attr(tree, 'depth', node, depth)
if (depth == 1) {
tree <- set_vertex_attr(tree, 'Class', node, V(tree)$shortName[node])
} else if (depth > 1) {
tree <- set_vertex_attr(tree, 'Class', node, V(tree)$Class[parent])
}
tree
})
# For dendrograms it's slightly different
irisDen <- as.dendrogram(hclust(dist(iris[1:4], method='euclidean'),
method='ward.D2'))
# Add the species information to the leafs
irisDen <- dendrapply(irisDen, function(d) {
if(is.leaf(d))
attr(d, 'nodePar') <- list(species=iris[as.integer(attr(d, 'label')),5])
d
})
# Set class of node to the class of it's children they are of equal class
irisDen <- tree_apply(irisDen, function(node, children, ...) {
if (is.leaf(node)) {
attr(node, 'Class') <- attr(node, 'nodePar')$species
} else {
classes <- unique(sapply(children, attr, which = 'Class'))
if (length(classes) == 1 && !anyNA(classes)) {
attr(node, 'Class') <- classes
} else {
attr(node, 'Class') <- NA
}
}
node
}, direction = 'up')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.