| 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 |
|
| 22 |
# as.dendrogram(as.hclust(as.phylo(hc))) |
| 23 |
#' @export |
| 24 |
as.dendrogram.phylo <- function(object, ...) { |
| 25 |
# library(ape) |
| 26 |
as.dendrogram(ape::as.hclust.phylo(object)) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
# Make it so that I can have as.phylo.dendrogram work, without forcing the user to have ape imported |
| 31 |
as.phylo <- function(x, ...) { |
| 32 |
# library(ape) |
| 33 |
ape::as.phylo(x, ...) |
| 34 |
# if (length(class(x)) == 1 && class(x) == "phylo") |
| 35 |
# return(x) |
| 36 |
# UseMethod("as.phylo") |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
# We can't use an S3 here without switching ape to be a depend/imports, and enhance is more fitting. So I must make this function visible... :\ |
| 42 |
# ' @method as.phylo dendrogram |
| 43 |
# ' @S3method as.phylo dendrogram |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#' @title Convert a dendrogram into phylo |
| 49 |
#' @description |
| 50 |
#' Based on \link{as.hclust.dendrogram} with \link[ape]{as.phylo.hclust} |
| 51 |
#' |
| 52 |
#' In the future I hope a more direct link will be made. |
| 53 |
#' |
| 54 |
#' @param x a dendrogram |
| 55 |
#' @param ... ignored. |
| 56 |
#' @return |
| 57 |
#' A phylo class object |
| 58 |
#' @seealso \link{as.dendrogram}, \link{as.hclust}, \link[ape]{as.phylo} |
| 59 |
#' @examples |
| 60 |
#' \dontrun{ |
| 61 |
#' |
| 62 |
#' library(dendextend) |
| 63 |
#' library(ape) |
| 64 |
#' dend <- iris[1:30, -5] %>% |
| 65 |
#' dist() %>% |
| 66 |
#' hclust() %>% |
| 67 |
#' as.dendrogram() |
| 68 |
#' dend2 <- as.phylo(dend) |
| 69 |
#' plot(dend2, type = "fan") |
| 70 |
#' |
| 71 |
#' library(dendextend) |
| 72 |
#' library(ggplot2) |
| 73 |
#' # no longer needed: library(ggdendro) |
| 74 |
#' dend <- iris[1:30, -5] %>% |
| 75 |
#' dist() %>% |
| 76 |
#' hclust() %>% |
| 77 |
#' as.dendrogram() |
| 78 |
#' # there is a bug in the location of the labels |
| 79 |
#' # If you want to solve it - please send a Pull Request to: |
| 80 |
#' # https://github.com/talgalili/dendextend/ |
| 81 |
#' ggplot(dend) + |
| 82 |
#' scale_y_reverse(expand = c(0.2, 0)) + coord_polar(start = 1, theta="x") |
| 83 |
#' |
| 84 |
#' } |
| 85 |
#' |
| 86 |
#' |
| 87 |
#' # see: https://github.com/klutometis/roxygen/issues/796 |
| 88 |
#' # |
| 89 |
#' @rawNamespace |
| 90 |
#' if(getRversion() >= "3.6.0") { |
| 91 |
#' S3method(ape::as.phylo, dendrogram) |
| 92 |
#' } else {export(as.phylo.dendrogram)} |
| 93 |
#' |
| 94 |
as.phylo.dendrogram <- function(x, ...) { |
| 95 |
# library(ape) |
| 96 |
ape::as.phylo.hclust(as.hclust(x)) |
| 97 |
} |
| 98 |
## https://stackoverflow.com/questions/13085481/namespace-dependencies-not-required |
| 99 |
## I also added ape to "Imports" in DESCRIPTION in order to avoid the error: |
| 100 |
## Namespace dependency not required: 'ape' |
| 101 |
## Update: I removed ape from the imports and NAMESPACE. |