Debian Codegraph @

R/has_edgePar.R

raw
1
2
3 #' Does a dendrogram has an edgePar/nodePar component?
4 #' @export
5 #' @aliases
6 #' has_edgePar
7 #' has_nodePar
8 #'
9 #' @param dend a \link{dendrogram} object.
10 #' @param component a character value to be checked if exists in the tree. For edgePar the list: "col", "lty" and "lwd" (for the segments),
11 #' "p.col", "p.lwd", and "p.lty" (for the polygon around the text) and "t.col" for the text color.
12 #' For edgePar "pch", "cex", "col", "xpd", and/or "bg".
13 #' @param the_attrib A character of the attribute for which to check the existence of the component.
14 #' Often either "edgePar" or "nodePar".
15 #' @param ... ignored
16 #'
17 #' @seealso \link{get_nodes_attr}, \link{set}
18 #' @return
19 #' Logical. TRUE if such a component is defined somewhere in the tree, FALSE otherwise.
20 #' If dend is not a dendrogram, the function will return FALSE.
21 #'
22 #' @examples
23 #'
24 #' dat <- iris[1:20, -5]
25 #' hca <- hclust(dist(dat))
26 #' hca2 <- hclust(dist(dat), method = "single")
27 #' dend <- as.dendrogram(hca)
28 #' dend2 <- as.dendrogram(hca2)
29 #'
30 #' dend %>%
31 #' set("branches_lwd", 2) %>%
32 #' set("branches_lty", 2) %>%
33 #' plot()
34 #' dend %>%
35 #' set("branches_lwd", 2) %>%
36 #' set("branches_lty", 2) %>%
37 #' has_edgePar("lty")
38 #' dend %>%
39 #' set("branches_lwd", 2) %>%
40 #' has_edgePar("lty")
41 #' dend %>%
42 #' set("branches_lwd", 2) %>%
43 #' has_edgePar("lwd")
44 #'
45 #' dend %>%
46 #' set("branches_lwd", 2) %>%
47 #' set("clear_branches") %>%
48 #' has_edgePar("lwd")
49 has_component_in_attribute <- function(dend, component, the_attrib = "edgePar", ...) {
50 if (!is.dendrogram(dend)) {
51 return(FALSE)
52 }
53
54 list_of_names <- dend %>%
55 get_nodes_attr(the_attrib, simplify = FALSE) %>%
56 lapply(names)
57
58 has_component_fun <- function(node_names) any(node_names %in% component)
59 has_component <- list_of_names %>%
60 lapply(has_component_fun) %>%
61 unlist() %>%
62 any()
63 has_component
64 }
65
66 #' @export
67 has_edgePar <- function(dend, component, the_attrib = "edgePar", ...) {
68 has_component_in_attribute(dend, component, the_attrib, ...)
69 }
70
71 #' @export
72 has_nodePar <- function(dend, component, the_attrib = "nodePar", ...) {
73 has_component_in_attribute(dend, component, the_attrib, ...)
74 }