| 1 |
# library(testthat) |
| 2 |
|
| 3 |
# we don't need the warning now... https://stackoverflow.com/questions/16194212/how-to-supress-warnings-globally-in-an-r-script |
| 4 |
old_warn_opt <- options()$warn |
| 5 |
options(warn = -1) |
| 6 |
|
| 7 |
|
| 8 |
context("Checknig set.dendrogram") |
| 9 |
|
| 10 |
|
| 11 |
test_that("labels options works", { |
| 12 |
suppressWarnings(RNGversion("3.5.0")) |
| 13 |
# library(magrittr) |
| 14 |
set.seed(23235) |
| 15 |
ss <- sample(1:150, 10) |
| 16 |
dend <- iris[ss, -5] %>% |
| 17 |
dist() %>% |
| 18 |
hclust() %>% |
| 19 |
as.dendrogram() |
| 20 |
|
| 21 |
expect_equal( |
| 22 |
dend %>% set("labels", 1:10) %>% labels(), |
| 23 |
1:10 |
| 24 |
) |
| 25 |
|
| 26 |
expect_equal( |
| 27 |
dend %>% set("labels", as.character(1:10)) %>% labels(), |
| 28 |
as.character(1:10) |
| 29 |
) |
| 30 |
|
| 31 |
dendextend_options("warn", TRUE) |
| 32 |
expect_warning(set(dend, "labels_color")) |
| 33 |
dendextend_options("warn", FALSE) |
| 34 |
|
| 35 |
|
| 36 |
# before doing anything, we have NULL labels colors: |
| 37 |
expect_null(dend %>% labels_colors()) |
| 38 |
|
| 39 |
# piping is the same as not (just MUCH more readable) |
| 40 |
expect_equal( |
| 41 |
set(dend, "labels_color"), |
| 42 |
dend %>% set("labels_color") |
| 43 |
) |
| 44 |
|
| 45 |
# here we update the colors, and then try to see them: |
| 46 |
expect_equal( |
| 47 |
labels_colors(set(dend, "labels_color")), |
| 48 |
dend %>% set("labels_color") %>% labels_colors() |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
new_col_labels <- structure(c( |
| 53 |
"#CC476B", "#B76100", "#917600", "#518600", "#009232", |
| 54 |
"#009681", "#008FB7", "#1678D5", "#A352D1", "#CB39AA" |
| 55 |
), .Names = c( |
| 56 |
"123", |
| 57 |
"145", "126", "109", "23", "29", "94", "59", "67", "97" |
| 58 |
)) |
| 59 |
# dend %>% set("labels_color") %>% plot |
| 60 |
expect_equal( |
| 61 |
dend %>% set("labels_color") %>% labels_colors(), |
| 62 |
new_col_labels |
| 63 |
) |
| 64 |
# dend %>% set("labels_color", new_col_labels) %>% plot |
| 65 |
|
| 66 |
|
| 67 |
# we get the correct attribue set... |
| 68 |
tmp <- dend %>% |
| 69 |
set("labels_col", 2) %>% |
| 70 |
set("labels_cex", 1.2) |
| 71 |
tmp <- tmp[[1]][[1]] |
| 72 |
# unclass(tmp) |
| 73 |
expect_equal( |
| 74 |
attr(tmp, "nodePar")$lab.col, |
| 75 |
2 |
| 76 |
) |
| 77 |
expect_equal( |
| 78 |
attr(tmp, "nodePar")$lab.cex, |
| 79 |
1.2 |
| 80 |
) |
| 81 |
|
| 82 |
# if 'what' parameter not specified in set() then it should create a warning and return the same dendrogram |
| 83 |
dendextend_options("warn", TRUE) |
| 84 |
expect_warning( |
| 85 |
result <- dend %>% set() |
| 86 |
) |
| 87 |
dendextend_options("warn", FALSE) |
| 88 |
expect_identical( |
| 89 |
result, |
| 90 |
dend |
| 91 |
) |
| 92 |
|
| 93 |
# if order_value parameter used |
| 94 |
tmp <- dend %>% |
| 95 |
set("labels_col", 2, order_value = T) |
| 96 |
tmp <- tmp[[2]][[1]][[1]] |
| 97 |
expect_equal( |
| 98 |
attr(tmp, "nodePar")$lab.col, |
| 99 |
2 |
| 100 |
) |
| 101 |
|
| 102 |
}) |
| 103 |
|
| 104 |
|
| 105 |
test_that("leaves options works", { |
| 106 |
# library(magrittr) |
| 107 |
suppressWarnings(RNGversion("3.5.0")) |
| 108 |
set.seed(23235) |
| 109 |
ss <- sample(1:150, 10) |
| 110 |
dend <- iris[ss, -5] %>% |
| 111 |
dist() %>% |
| 112 |
hclust() %>% |
| 113 |
as.dendrogram() |
| 114 |
|
| 115 |
tmp <- dend |
| 116 |
tmp <- tmp %>% |
| 117 |
set("leaves_pch", 2) %>% |
| 118 |
set("leaves_cex", 1.5) %>% |
| 119 |
set("leaves_col", c(3:1)) %>% |
| 120 |
set("hang") |
| 121 |
tmp <- tmp[[1]][[1]] |
| 122 |
# unclass(tmp) |
| 123 |
expect_equal( |
| 124 |
attr(tmp, "nodePar")[["pch"]], |
| 125 |
2 |
| 126 |
) |
| 127 |
expect_equal( |
| 128 |
attr(tmp, "nodePar")[["cex"]], |
| 129 |
1.5 |
| 130 |
) |
| 131 |
expect_equal( |
| 132 |
attr(tmp, "nodePar")[["col"]], |
| 133 |
3 |
| 134 |
) |
| 135 |
expect_equal( |
| 136 |
attr(tmp, "height"), |
| 137 |
0.9030533 |
| 138 |
) |
| 139 |
|
| 140 |
# tmp %>% plot |
| 141 |
}) |
| 142 |
|
| 143 |
|
| 144 |
test_that("branches options works", { |
| 145 |
# library(magrittr) |
| 146 |
suppressWarnings(RNGversion("3.5.0")) |
| 147 |
set.seed(23235) |
| 148 |
ss <- sample(1:150, 10) |
| 149 |
dend <- iris[ss, -5] %>% |
| 150 |
dist() %>% |
| 151 |
hclust() %>% |
| 152 |
as.dendrogram() |
| 153 |
|
| 154 |
tmp <- dend %>% |
| 155 |
set("branches_k_col", c(3, 1, 2), k = 3) |
| 156 |
# tmp %>% plot |
| 157 |
|
| 158 |
expect_equal( |
| 159 |
unname(unlist(get_nodes_attr(tmp, "edgePar"))[1:3]), |
| 160 |
c(NA, 3, 3) |
| 161 |
) |
| 162 |
|
| 163 |
# as.data.frame(get_nodes_attr(tmp, "edgePar")) |
| 164 |
# unclass(tmp) |
| 165 |
|
| 166 |
tmp <- dend |
| 167 |
tmp <- tmp %>% |
| 168 |
set("branches_col", c(1, 2, 1, 2, Inf)) %>% |
| 169 |
set("branches_lwd", c(2, 1, 2)) %>% |
| 170 |
set("branches_lty", c(1, 2, 1)) # %>% plot |
| 171 |
|
| 172 |
# checking we got a nice list: |
| 173 |
# dput(attr(tmp,"edgePar")) |
| 174 |
should_be <- structure(list(col = 1, lwd = 2, lty = 1), .Names = c( |
| 175 |
"col", |
| 176 |
"lwd", "lty" |
| 177 |
)) |
| 178 |
expect_equal(attr(tmp, "edgePar"), should_be) |
| 179 |
}) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
test_that("clearing options works", { |
| 184 |
# library(magrittr) |
| 185 |
suppressWarnings(RNGversion("3.5.0")) |
| 186 |
set.seed(23235) |
| 187 |
ss <- sample(1:150, 10) |
| 188 |
# Getting the dend object onces |
| 189 |
dend <- iris[ss, -5] %>% |
| 190 |
dist() %>% |
| 191 |
hclust() %>% |
| 192 |
as.dendrogram() |
| 193 |
|
| 194 |
tmp <- dend |
| 195 |
tmp <- tmp %>% |
| 196 |
set("leaves_pch", c(19, 19, Inf)) %>% |
| 197 |
set("labels_color", c(19, 19, Inf)) # %>% |
| 198 |
# set("clear_leaves") %>% plot |
| 199 |
expect_identical(dend, set(tmp, "clear_leaves")) |
| 200 |
|
| 201 |
tmp <- dend |
| 202 |
tmp <- tmp %>% |
| 203 |
set("branches_col", c(1, 2, 1, 2, Inf)) %>% |
| 204 |
set("branches_lwd", c(2, 1, 2)) %>% |
| 205 |
set("branches_lty", c(1, 2, 1)) # %>% plot |
| 206 |
# We can remove all the branch attributes |
| 207 |
expect_false(identical(dend, tmp)) |
| 208 |
expect_identical(dend, set(tmp, "clear_branches")) |
| 209 |
}) |
| 210 |
|
| 211 |
|
| 212 |
test_that("set.dendlist works", { |
| 213 |
suppressWarnings(RNGversion("3.5.0")) |
| 214 |
set.seed(23235) |
| 215 |
|
| 216 |
ss <- sample(1:150, 10) |
| 217 |
dend1 <- iris[ss, -5] %>% |
| 218 |
dist() %>% |
| 219 |
hclust() %>% |
| 220 |
as.dendrogram() |
| 221 |
dend2 <- shuffle(dend1) |
| 222 |
dend12 <- dendlist(dend1, dend2) |
| 223 |
|
| 224 |
# without 'what' parameter specified, nothing is changed |
| 225 |
result <- set.dendlist(dend12) |
| 226 |
expect_identical( |
| 227 |
dend12, |
| 228 |
result |
| 229 |
) |
| 230 |
}) |
| 231 |
|
| 232 |
|
| 233 |
test_that("set.data.table works", { |
| 234 |
|
| 235 |
# expect warning as dendextend::set.data.table overwrites data.table::set |
| 236 |
dt <- data.table::data.table(id = 1:5, value = c(10, 20, 30, 40, 50)) |
| 237 |
expect_warning( |
| 238 |
set.data.table(dt, j = 1L, value = 1) |
| 239 |
) |
| 240 |
|
| 241 |
}) |
| 242 |
|
| 243 |
options(warn = old_warn_opt) |