| 1 |
# library(testthat) |
| 2 |
|
| 3 |
|
| 4 |
# fixing how the tests are printed |
| 5 |
# cat("\n") |
| 6 |
|
| 7 |
context("ggdend") |
| 8 |
|
| 9 |
|
| 10 |
test_that("as.ggdend.dendrogram works", { |
| 11 |
|
| 12 |
|
| 13 |
# get_nodes_attr(d1, "nodePar", simplify = FALSE) |
| 14 |
# plot(d1) |
| 15 |
|
| 16 |
dend <- 1:3 %>% |
| 17 |
dist() %>% |
| 18 |
hclust() %>% |
| 19 |
as.dendrogram() %>% |
| 20 |
set("branches_k_color", k = 2) %>% |
| 21 |
set("branches_lwd", c(1.5, 1, 1.5)) %>% |
| 22 |
set("branches_lty", c(1, 1, 3, 1, 1, 2)) %>% |
| 23 |
set("labels_colors") %>% |
| 24 |
set("labels_cex", c(.9, 1.2)) %>% |
| 25 |
set("nodes_pch", 19) %>% |
| 26 |
set("nodes_col", c("orange", "black", "NA")) |
| 27 |
|
| 28 |
gg1 <- as.ggdend(dend) |
| 29 |
# ggplot(gg1) |
| 30 |
# dput(gg1) |
| 31 |
should_be <- structure(list( |
| 32 |
segments = structure(list(x = c( |
| 33 |
1.75, 1, 1.75, |
| 34 |
2.5, 2.5, 2, 2.5, 3 |
| 35 |
), y = c(2, 2, 2, 2, 1, 1, 1, 1), xend = c( |
| 36 |
1, |
| 37 |
1, 2.5, 2.5, 2, 2, 3, 3 |
| 38 |
), yend = c(2, 0, 2, 1, 1, 0, 1, 0), col = c( |
| 39 |
"#CC476B", |
| 40 |
"#CC476B", "#009681", "#009681", "#009681", "#009681", "#009681", |
| 41 |
"#009681" |
| 42 |
), lwd = c(1, 1, 1.5, 1.5, 1.5, 1.5, 1, 1), lty = c( |
| 43 |
1, |
| 44 |
1, 3, 3, 1, 1, 1, 1 |
| 45 |
)), .Names = c( |
| 46 |
"x", "y", "xend", "yend", "col", |
| 47 |
"lwd", "lty" |
| 48 |
), row.names = c(NA, 8L), class = "data.frame"), |
| 49 |
labels = structure(list(x = c(1, 2, 3), y = c(0, 0, 0), label = structure(1:3, .Label = c( |
| 50 |
"3", |
| 51 |
"1", "2" |
| 52 |
), class = "factor"), col = c( |
| 53 |
"#CC476B", "#228B00", |
| 54 |
"#0082CE" |
| 55 |
), cex = c(0.9, 1.2, 0.9)), .Names = c( |
| 56 |
"x", "y", |
| 57 |
"label", "col", "cex" |
| 58 |
), row.names = c(NA, 3L), class = "data.frame"), |
| 59 |
nodes = structure(list(x = c(1.75, 1, 2.5, 2, 3), y = c( |
| 60 |
2, |
| 61 |
0, 1, 0, 0 |
| 62 |
), pch = c(19, 19, 19, 19, 19), cex = c( |
| 63 |
NA, NA, |
| 64 |
NA, NA, NA |
| 65 |
), col = c("orange", "black", "NA", "orange", "black"), members = c(3L, 1L, 2L, 1L, 1L), midpoint = c( |
| 66 |
0.75, NA, |
| 67 |
0.5, NA, NA |
| 68 |
), height = c(2, 0, 1, 0, 0), leaf = c( |
| 69 |
NA, TRUE, |
| 70 |
NA, TRUE, TRUE |
| 71 |
)), .Names = c( |
| 72 |
"x", "y", "pch", "cex", "col", |
| 73 |
"members", "midpoint", "height", "leaf" |
| 74 |
), row.names = c( |
| 75 |
NA, |
| 76 |
-5L |
| 77 |
), class = "data.frame") |
| 78 |
), .Names = c( |
| 79 |
"segments", "labels", |
| 80 |
"nodes" |
| 81 |
), class = "ggdend") |
| 82 |
expect_identical(gg1, should_be) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
dend <- 1:3 %>% |
| 87 |
dist() %>% |
| 88 |
hclust() %>% |
| 89 |
as.dendrogram() |
| 90 |
gg2 <- as.ggdend(dend) |
| 91 |
# dput(gg2) |
| 92 |
should_be <- structure(list(segments = structure(list(x = c( |
| 93 |
1.75, 1, 1.75, |
| 94 |
2.5, 2.5, 2, 2.5, 3 |
| 95 |
), y = c(2, 2, 2, 2, 1, 1, 1, 1), xend = c( |
| 96 |
1, |
| 97 |
1, 2.5, 2.5, 2, 2, 3, 3 |
| 98 |
), yend = c(2, 0, 2, 1, 1, 0, 1, 0), col = c( |
| 99 |
NA, |
| 100 |
NA, NA, NA, NA, NA, NA, NA |
| 101 |
), lwd = c( |
| 102 |
NA, NA, NA, NA, NA, NA, |
| 103 |
NA, NA |
| 104 |
), lty = c(NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c( |
| 105 |
"x", |
| 106 |
"y", "xend", "yend", "col", "lwd", "lty" |
| 107 |
), row.names = c( |
| 108 |
NA, |
| 109 |
8L |
| 110 |
), class = "data.frame"), labels = structure(list(x = c( |
| 111 |
1, |
| 112 |
2, 3 |
| 113 |
), y = c(0, 0, 0), label = structure(1:3, .Label = c( |
| 114 |
"3", |
| 115 |
"1", "2" |
| 116 |
), class = "factor"), col = c(NA, NA, NA), cex = c( |
| 117 |
NA, |
| 118 |
NA, NA |
| 119 |
)), .Names = c("x", "y", "label", "col", "cex"), row.names = c( |
| 120 |
NA, |
| 121 |
3L |
| 122 |
), class = "data.frame"), nodes = structure(list( |
| 123 |
x = c( |
| 124 |
1.75, |
| 125 |
1, 2.5, 2, 3 |
| 126 |
), y = c(2, 0, 1, 0, 0), pch = c( |
| 127 |
NA, NA, NA, NA, |
| 128 |
NA |
| 129 |
), cex = c(NA, NA, NA, NA, NA), col = c(NA, NA, NA, NA, NA), |
| 130 |
members = c(3L, 1L, 2L, 1L, 1L), midpoint = c( |
| 131 |
0.75, NA, 0.5, |
| 132 |
NA, NA |
| 133 |
), height = c(2, 0, 1, 0, 0), leaf = c( |
| 134 |
NA, TRUE, NA, |
| 135 |
TRUE, TRUE |
| 136 |
) |
| 137 |
), .Names = c( |
| 138 |
"x", "y", "pch", "cex", "col", "members", |
| 139 |
"midpoint", "height", "leaf" |
| 140 |
), row.names = c(NA, -5L), class = "data.frame")), .Names = c( |
| 141 |
"segments", |
| 142 |
"labels", "nodes" |
| 143 |
), class = "ggdend") |
| 144 |
### |
| 145 |
expect_identical(gg2, should_be) |
| 146 |
|
| 147 |
# if dend without leaves is passed in |
| 148 |
expect_error(with_mocked_bindings( |
| 149 |
as.ggdend(dend), |
| 150 |
nleaves = function(x, ...) return(0) |
| 151 |
)) |
| 152 |
|
| 153 |
# if type triangle used |
| 154 |
dend <- 1:3 %>% |
| 155 |
dist() %>% |
| 156 |
hclust() %>% |
| 157 |
as.dendrogram() |
| 158 |
expect_no_error( |
| 159 |
as.ggdend(dend, type = "triangle") |
| 160 |
) |
| 161 |
|
| 162 |
}) |
| 163 |
|
| 164 |
test_that("ggplot doesn't have warnings for dendrograms", { |
| 165 |
library(ggplot2) |
| 166 |
library(dendextend) |
| 167 |
g <- ggplot(as.dendrogram(hclust(dist(mtcars)))) |
| 168 |
built_plot <- ggplot_build(g) |
| 169 |
if (packageVersion("ggplot2") >= "4.0.0") { |
| 170 |
expect_identical(names(attributes(built_plot)), c("class", "S7_class", "data", "layout", "plot")) |
| 171 |
} else { |
| 172 |
expect_identical(names(built_plot), c("data", "layout", "plot")) |
| 173 |
} |
| 174 |
}) |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
# library(testthat) |
| 180 |
# library(dendextend) |
| 181 |
|
| 182 |
# Testing ggdend function |
| 183 |
test_that("ggdend provides usage message", { |
| 184 |
expect_output(ggdend(), "Use either as.ggdend or ggplot (or both). ", fixed = TRUE) |
| 185 |
}) |
| 186 |
|
| 187 |
# Testing allNA function |
| 188 |
test_that("allNA correctly identifies all NA values", { |
| 189 |
allNA <- dendextend:::allNA |
| 190 |
expect_true(dendextend:::allNA(c(NA, NA, NA))) |
| 191 |
expect_false(allNA(c(1, NA, 3))) |
| 192 |
}) |
| 193 |
|
| 194 |
# Testing as.ggdend.dendrogram function |
| 195 |
test_that("as.ggdend.dendrogram stops with non-dendrogram input", { |
| 196 |
as.ggdend.dendrogram <- dendextend:::as.ggdend.dendrogram |
| 197 |
expect_error(as.ggdend.dendrogram(list()), "dend is not a dendrogram") |
| 198 |
}) |
| 199 |
|
| 200 |
test_that("as.ggdend.dendrogram stops with empty dendrogram", { |
| 201 |
as.ggdend.dendrogram <- dendextend:::as.ggdend.dendrogram |
| 202 |
empty_dend <- list() |
| 203 |
class(empty_dend) <- "dendrogram" |
| 204 |
expect_error(as.ggdend.dendrogram(empty_dend), "argument is of length zero") |
| 205 |
}) |
| 206 |
|
| 207 |
test_that("as.ggdend.dendrogram stops with edge.root argument", { |
| 208 |
as.ggdend.dendrogram <- dendextend:::as.ggdend.dendrogram |
| 209 |
dend <- as.dendrogram(hclust(dist(1:5))) |
| 210 |
expect_error(as.ggdend.dendrogram(dend, edge.root = TRUE), "edge.root is not supported") |
| 211 |
}) |
| 212 |
|
| 213 |
test_that("as.ggdend.dendrogram returns ggdend class with valid input", { |
| 214 |
as.ggdend.dendrogram <- dendextend:::as.ggdend.dendrogram |
| 215 |
dend <- as.dendrogram(hclust(dist(1:5))) |
| 216 |
result <- as.ggdend.dendrogram(dend) |
| 217 |
expect_true("ggdend" %in% class(result)) |
| 218 |
}) |
| 219 |
|
| 220 |
# Testing prepare.ggdend function |
| 221 |
test_that("prepare.ggdend handles segment linetype and color", { |
| 222 |
dend <- as.dendrogram(hclust(dist(1:5))) |
| 223 |
ggdend_data <- as.ggdend(dend) |
| 224 |
prepared_data <- prepare.ggdend(ggdend_data) |
| 225 |
|
| 226 |
# Check for linetype and color corrections |
| 227 |
expect_true(all(!is.na(prepared_data$segments$lty))) |
| 228 |
expect_true(all(!is.na(prepared_data$segments$col))) |
| 229 |
|
| 230 |
data <- list() |
| 231 |
data$segments$lty <- 1 |
| 232 |
data$segments$col <- 1 |
| 233 |
data$labels$col <- 1 |
| 234 |
data$nodes <- matrix(1:6, ncol = 3) |
| 235 |
colnames(data$nodes) <- c("pch", "cex", "col") |
| 236 |
data$nodes <- as.data.frame(data$nodes) |
| 237 |
expect_no_error( |
| 238 |
prepare.ggdend(data) |
| 239 |
) |
| 240 |
|
| 241 |
}) |
| 242 |
|
| 243 |
# Testing ggplot.ggdend function |
| 244 |
test_that("ggplot.ggdend creates a ggplot object", { |
| 245 |
ggplot.ggdend <- dendextend:::ggplot.ggdend |
| 246 |
dend <- as.dendrogram(hclust(dist(1:5))) |
| 247 |
ggdend_data <- as.ggdend(dend) |
| 248 |
plot <- ggplot.ggdend(ggdend_data) |
| 249 |
expect_true(inherits(plot, "ggplot")) |
| 250 |
plot <- ggplot.ggdend(ggdend_data, horiz = T) |
| 251 |
expect_true(inherits(plot, "ggplot")) |
| 252 |
}) |
| 253 |
|
| 254 |
# Testing ggplot.dendrogram function |
| 255 |
test_that("ggplot.dendrogram converts dendrogram to ggplot", { |
| 256 |
ggplot.dendrogram <- dendextend:::ggplot.dendrogram |
| 257 |
dend <- as.dendrogram(hclust(dist(1:5))) |
| 258 |
plot <- ggplot.dendrogram(dend) |
| 259 |
expect_true(inherits(plot, "ggplot")) |
| 260 |
}) |
| 261 |
|
| 262 |
|
| 263 |
test_that("print.ggdend prints a ggplot", { |
| 264 |
dend <- as.dendrogram(hclust(dist(1:5))) |
| 265 |
ggdend_data <- as.ggdend(dend) |
| 266 |
pdf(file = NULL) # Open a null device |
| 267 |
expect_silent(print(ggdend_data)) |
| 268 |
dev.off() # Close the null device |
| 269 |
expect_true(inherits(ggdend_data, "ggdend")) # check if it's a ggdend object |
| 270 |
expect_true(inherits(ggplot2::ggplot(ggdend_data), "ggplot")) # check if it can be used to create a ggplot object |
| 271 |
}) |