| 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 |
|
| 21 |
# ' @aliases |
| 22 |
# ' old_cut_lower_fun |
| 23 |
|
| 24 |
|
| 25 |
#' @title Cut a dendrogram - and run a function on the output |
| 26 |
#' @export |
| 27 |
#' @aliases |
| 28 |
#' dendextend_cut_lower_fun |
| 29 |
#' @description |
| 30 |
#' Cuts the dend at height h and returns a list with the FUN function |
| 31 |
#' implemented on all the sub trees created by cut at height h. |
| 32 |
#' This is used for creating a \link[dendextend]{cutree.dendrogram} function, |
| 33 |
#' by using the \code{labels} function as FUN. |
| 34 |
#' |
| 35 |
#' This is the Rcpp version of the function, offering a 10-60 times improvement |
| 36 |
#' in speed (depending on the tree size it is used on). |
| 37 |
#' |
| 38 |
#' @param dend a dendrogram object. |
| 39 |
#' @param h a scalar of height to cut the dend by. |
| 40 |
#' @param FUN a function to run. (default is "labels") |
| 41 |
#' @param warn logical (default from dendextend_options("warn") is FALSE). |
| 42 |
#' Set if warning are to be issued, it is safer to keep this at TRUE, |
| 43 |
#' but for keeping the noise down, the default is FALSE. |
| 44 |
#' Should the user be warned if reverting to default? |
| 45 |
#' @param ... passed to FUN. |
| 46 |
#' @return A list with the output of running FUN on each of the |
| 47 |
#' sub dends derived from cutting "dend" |
| 48 |
#' @author Tal Galili |
| 49 |
#' @seealso \code{\link{labels}}, \code{\link{dendrogram}}, |
| 50 |
#' \link[dendextend]{cutree.dendrogram} |
| 51 |
#' @examples |
| 52 |
#' |
| 53 |
#' dend <- as.dendrogram(hclust(dist(iris[1:4, -5]))) |
| 54 |
#' # this is really cool! |
| 55 |
#' cut_lower_fun(dend, .4, labels) |
| 56 |
#' lapply(cut(dend, h = .4)$lower, labels) |
| 57 |
#' cut_lower_fun(dend, .4, order.dendrogram) |
| 58 |
#' |
| 59 |
cut_lower_fun <- function(dend, h, FUN = labels, warn = dendextend_options("warn"), ...) { |
| 60 |
if (!is.dendrogram(dend)) stop("'dend' needs to be a dendrogram. Aborting the function 'cut_lower_labels'.") |
| 61 |
|
| 62 |
if (is.leaf(dend)) { |
| 63 |
return(list(FUN(dend))) |
| 64 |
} |
| 65 |
# else: |
| 66 |
# dend_and_FUN <- function(x) { |
| 67 |
# class(x) = "dendrogram" |
| 68 |
# FUN(x,...) |
| 69 |
# } |
| 70 |
# return(lapply(Rcpp_cut_lower(dend, h), dend_and_FUN)) |
| 71 |
|
| 72 |
return(lapply(cut(dend, h = h)$lower, FUN)) # If the proper labels are not important, this function is around 10 times faster than using labels (so it is much better for some other algorithms) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
# cut(dend, h = .14)$lower |
| 77 |
|
| 78 |
# detach( 'package:dendextendRcpp', unload=TRUE ) |
| 79 |
# library( 'dendextendRcpp' ) |
| 80 |
# labels(dend) |