Debian Codegraph @

R/stats_imports.R

raw
1 # Copyright (C) Tal Galili
2 #
3 # This file is part of dendextend.
4 #
5 # dendextend is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # dendextend is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # A copy of the GNU General Public License is available at
16 # http://www.r-project.org/Licenses/
17 #
18
19
20 # This file includes hidden functions from "stats" that have been imported into this package
21 # in case their usage would be changed in future versions of R.
22
23 # Unexported objects imported by ':::' calls:
24 # 'stats:::.memberDend' 'stats:::.midDend'
25 # 'stats:::midcache.dendrogram' 'stats:::plotNode'
26 # 'stats:::plotNodeLimit'
27
28
29
30
31
32
33
34
35 # stats:::.memberDend
36 stats_.memberDend <- function(x) {
37 r <- attr(x, "x.member")
38 if (is.null(r)) {
39 r <- attr(x, "members")
40 if (is.null(r)) {
41 r <- 1L
42 }
43 }
44 r
45 }
46 .memberDend <- stats_.memberDend
47
48 # stats:::.midDend
49 stats_.midDend <- function(x) {
50 if (is.null(mp <- attr(x, "midpoint"))) 0 else mp
51 }
52 .midDend <- stats_.midDend # copied so that they would work inside the various functions here...
53
54
55 # stats:::midcache.dendrogram
56 stats_midcache.dendrogram <- function(x, type = "hclust", quiet = FALSE) {
57 type <- match.arg(type)
58 stopifnot(inherits(x, "dendrogram"))
59 setmid <- function(d, type) {
60 if (is.leaf(d)) {
61 return(d)
62 }
63 k <- length(d)
64 if (k < 1) {
65 stop("dendrogram node with non-positive #{branches}")
66 }
67 r <- d
68 midS <- 0
69 for (j in 1L:k) {
70 r[[j]] <- unclass(setmid(d[[j]], type))
71 midS <- midS + .midDend(r[[j]])
72 }
73 if (!quiet && type == "hclust" && k != 2) {
74 warning("midcache() of non-binary dendrograms only partly implemented")
75 }
76 attr(r, "midpoint") <- (.memberDend(d[[1L]]) + midS) / 2
77 r
78 }
79 setmid(x, type = type)
80 }
81 midcache.dendrogram <- stats_midcache.dendrogram
82
83
84 # stats:::plotNode
85 stats_plotNode <- function(x1, x2, subtree, type, center, leaflab, dLeaf, nodePar,
86 edgePar, horiz = FALSE) {
87 inner <- !is.leaf(subtree) && x1 != x2
88 yTop <- attr(subtree, "height")
89 bx <- plotNodeLimit(x1, x2, subtree, center)
90 xTop <- bx$x
91 hasP <- !is.null(nPar <- attr(subtree, "nodePar"))
92 if (!hasP) {
93 nPar <- nodePar
94 }
95 if (getOption("verbose")) {
96 cat(if (inner) {
97 "inner node"
98 } else {
99 "leaf"
100 }, ":")
101 if (!is.null(nPar)) {
102 cat(" with node pars\n")
103 str(nPar)
104 }
105 cat(if (inner) {
106 paste(" height", formatC(yTop), "; ")
107 }, "(x1,x2)= (",
108 formatC(x1, width = 4), ",", formatC(x2, width = 4),
109 ")", "--> xTop=", formatC(xTop, width = 8), "\n",
110 sep = ""
111 )
112 }
113 Xtract <- function(nam, L, default, indx) rep(if (nam %in%
114 names(L)) {
115 L[[nam]]
116 } else {
117 default
118 }, length.out = indx)[indx]
119 asTxt <- function(x) if (is.character(x) || is.expression(x) ||
120 is.null(x)) {
121 x
122 } else {
123 as.character(x)
124 }
125 i <- if (inner || hasP) {
126 1
127 } else {
128 2
129 }
130 if (!is.null(nPar)) {
131 pch <- Xtract("pch", nPar, default = 1L:2, i)
132 cex <- Xtract("cex", nPar, default = c(1, 1), i)
133 col <- Xtract("col", nPar, default = par("col"), i)
134 bg <- Xtract("bg", nPar, default = par("bg"), i)
135 points(if (horiz) {
136 cbind(yTop, xTop)
137 } else {
138 cbind(xTop, yTop)
139 },
140 pch = pch, bg = bg, col = col,
141 cex = cex
142 )
143 }
144 if (leaflab == "textlike") {
145 p.col <- Xtract("p.col", nPar, default = "white", i)
146 }
147 lab.col <- Xtract("lab.col", nPar,
148 default = par("col"),
149 i
150 )
151 lab.cex <- Xtract("lab.cex", nPar, default = c(1, 1), i)
152 lab.font <- Xtract("lab.font", nPar,
153 default = par("font"),
154 i
155 )
156 lab.xpd <- Xtract("xpd", nPar, default = c(TRUE, TRUE), i)
157 if (is.leaf(subtree)) {
158 if (leaflab == "perpendicular") {
159 if (horiz) {
160 X <- yTop + dLeaf * lab.cex
161 Y <- xTop
162 srt <- 0
163 adj <- c(0, 0.5)
164 }
165 else {
166 Y <- yTop - dLeaf * lab.cex
167 X <- xTop
168 srt <- 90
169 adj <- 1
170 }
171 nodeText <- asTxt(attr(subtree, "label"))
172 text(X, Y, nodeText,
173 xpd = lab.xpd, srt = srt, adj = adj,
174 cex = lab.cex, col = lab.col, font = lab.font
175 )
176 }
177 }
178 else if (inner) {
179 segmentsHV <- function(x0, y0, x1, y1) {
180 if (horiz) {
181 segments(y0, x0, y1, x1,
182 col = col, lty = lty,
183 lwd = lwd
184 )
185 } else {
186 segments(x0, y0, x1, y1,
187 col = col, lty = lty,
188 lwd = lwd
189 )
190 }
191 }
192 for (k in seq_along(subtree)) {
193 child <- subtree[[k]]
194 yBot <- attr(child, "height")
195 if (getOption("verbose")) {
196 cat("ch.", k, "@ h=", yBot, "; ")
197 }
198 if (is.null(yBot)) {
199 yBot <- 0
200 }
201 xBot <- if (center) {
202 mean(bx$limit[k:(k + 1)])
203 } else {
204 bx$limit[k] + .midDend(child)
205 }
206 hasE <- !is.null(ePar <- attr(child, "edgePar"))
207 if (!hasE) {
208 ePar <- edgePar
209 }
210 i <- if (!is.leaf(child) || hasE) {
211 1
212 } else {
213 2
214 }
215 col <- Xtract("col", ePar,
216 default = par("col"),
217 i
218 )
219 lty <- Xtract("lty", ePar,
220 default = par("lty"),
221 i
222 )
223 lwd <- Xtract("lwd", ePar,
224 default = par("lwd"),
225 i
226 )
227 if (type == "triangle") {
228 segmentsHV(xTop, yTop, xBot, yBot)
229 }
230 else {
231 segmentsHV(xTop, yTop, xBot, yTop)
232 segmentsHV(xBot, yTop, xBot, yBot)
233 }
234 vln <- NULL
235 if (is.leaf(child) && leaflab == "textlike") {
236 nodeText <- asTxt(attr(child, "label"))
237 if (getOption("verbose")) {
238 cat("-- with \"label\"", format(nodeText))
239 }
240 hln <- 0.6 * strwidth(nodeText, cex = lab.cex) / 2
241 vln <- 1.5 * strheight(nodeText, cex = lab.cex) / 2
242 rect(xBot - hln, yBot, xBot + hln, yBot + 2 *
243 vln, col = p.col)
244 text(xBot, yBot + vln, nodeText,
245 xpd = lab.xpd,
246 cex = lab.cex, col = lab.col, font = lab.font
247 )
248 }
249 if (!is.null(attr(child, "edgetext"))) {
250 edgeText <- asTxt(attr(child, "edgetext"))
251 if (getOption("verbose")) {
252 cat("-- with \"edgetext\"", format(edgeText))
253 }
254 if (!is.null(vln)) {
255 mx <- if (type == "triangle") {
256 (xTop + xBot + ((xTop - xBot) / (yTop - yBot)) *
257 vln) / 2
258 } else {
259 xBot
260 }
261 my <- (yTop + yBot + 2 * vln) / 2
262 }
263 else {
264 mx <- if (type == "triangle") {
265 (xTop + xBot) / 2
266 } else {
267 xBot
268 }
269 my <- (yTop + yBot) / 2
270 }
271 p.col <- Xtract("p.col", ePar,
272 default = "white",
273 i
274 )
275 p.border <- Xtract("p.border", ePar,
276 default = par("fg"),
277 i
278 )
279 p.lwd <- Xtract("p.lwd", ePar,
280 default = lwd,
281 i
282 )
283 p.lty <- Xtract("p.lty", ePar,
284 default = lty,
285 i
286 )
287 t.col <- Xtract("t.col", ePar,
288 default = col,
289 i
290 )
291 t.cex <- Xtract("t.cex", ePar, default = 1, i)
292 t.font <- Xtract("t.font", ePar,
293 default = par("font"),
294 i
295 )
296 vlm <- strheight(c(edgeText, "h"), cex = t.cex) / 2
297 hlm <- strwidth(c(edgeText, "m"), cex = t.cex) / 2
298 hl3 <- c(hlm[1L], hlm[1L] + hlm[2L], hlm[1L])
299 if (horiz) {
300 polygon(my + c(-hl3, hl3), mx + sum(vlm) *
301 c(-1L:1L, 1L:-1L),
302 col = p.col, border = p.border,
303 lty = p.lty, lwd = p.lwd
304 )
305 text(my, mx, edgeText,
306 cex = t.cex, col = t.col,
307 font = t.font
308 )
309 }
310 else {
311 polygon(mx + c(-hl3, hl3), my + sum(vlm) *
312 c(-1L:1L, 1L:-1L),
313 col = p.col, border = p.border,
314 lty = p.lty, lwd = p.lwd
315 )
316 text(mx, my, edgeText,
317 cex = t.cex, col = t.col,
318 font = t.font
319 )
320 }
321 }
322 plotNode(bx$limit[k], bx$limit[k + 1],
323 subtree = child,
324 type, center, leaflab, dLeaf, nodePar, edgePar,
325 horiz
326 )
327 }
328 }
329 invisible()
330 }
331 plotNode <- stats_plotNode
332
333
334 # stats:::plotNodeLimit
335 stats_plotNodeLimit <- function(x1, x2, subtree, center) {
336 inner <- !is.leaf(subtree) && x1 != x2
337 if (inner) {
338 K <- length(subtree)
339 mTop <- .memberDend(subtree)
340 limit <- integer(K)
341 xx1 <- x1
342 for (k in 1L:K) {
343 m <- .memberDend(subtree[[k]])
344 xx1 <- xx1 + (if (center) {
345 (x2 - x1) * m / mTop
346 } else {
347 m
348 })
349 limit[k] <- xx1
350 }
351 limit <- c(x1, limit)
352 }
353 else {
354 limit <- c(x1, x2)
355 }
356 mid <- attr(subtree, "midpoint")
357 center <- center || (inner && !is.numeric(mid))
358 x <- if (center) {
359 mean(c(x1, x2))
360 } else {
361 x1 + (if (inner) {
362 mid
363 } else {
364 0
365 })
366 }
367 list(x = x, limit = limit)
368 }
369 plotNodeLimit <- stats_plotNodeLimit