edit_node: Modify nodes in a dendrogram

Description Usage Arguments Details Value Examples

View source: R/dend_utils.R

Description

Modify nodes in a dendrogram

Usage

1
edit_node(dend, fun = function(d, index) d)

Arguments

dend

A dendrogram.

fun

A self-defined function.

Details

if fun only has one argument, it is basically the same as dendrapply, but it can have a second argument which is the index of the node in the dendrogram, which makes it possible to get information of child nodes and parent nodes for a specific node.

As an example, we first assign random values to every node in the dendrogram:

1
2
3
    mat = matrix(rnorm(100), 10)
    dend = as.dendrogram(hclust(dist(mat)))
    dend = edit_node(dend, function(d) {attr(d, 'score') = runif(1); d})  

Then for every node, we take the maximal absolute difference to all its child nodes and parent node as the attribute abs_diff

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    dend = edit_node(dend, function(d, index) {
        n = length(index)
        s = attr(d, "score")
        if(is.null(index)) {  # d is the top node
            s_children = sapply(d, function(x) attr(x, "score"))
            s_parent = NULL
        } else if(is.leaf(d)) { # d is the leaf
            s_children = NULL
            s_parent = attr(dend[[index[-n]]], "score")
        } else {
            s_children = sapply(d, function(x) attr(x, "score"))
            s_parent = attr(dend[[index[-n]]], "score")
        }
        abs_diff = max(abs(s - c(s_children, s_parent)))
        attr(d, "abs_diff") = abs_diff
        return(d)
    })  

Value

A dendrogram object.

Examples

1
2
# There is no example
NULL

simplifyEnrichment documentation built on Nov. 8, 2020, 5:07 p.m.