[Rd] A question on stats::as.hclust.dendrogram

Martin Maechler maechler at stat.math.ethz.ch
Fri Mar 24 13:38:39 CET 2017


>>>>> Ma,Man Chun John <MMa at mdanderson.org>
>>>>>     on Thu, 23 Mar 2017 19:29:25 +0000 writes:

    > Hi all,
    > This is the first time I'm writing to R-devel, and this time I'm just asking for the purpose for a certain line of code in stats::as.hclust.dendrogram, which comes up as I'm trying to fix dendextend.

"fix": where is it broken?
Do you mean the fact that in R <= 3.3.3, it is defined via
recursion and hence infeasible for "deep" dendrograms?

In any case, note that  NEWS  for the upcoming version of R,
R 3.4.0  contains 

    • The str() and as.hclust() methods for "dendrogram" now also work
      for deeply nested dendrograms thanks to non-recursive
      implementations by Bradley Broom.

so the source code of  as.hclust.dendrogram  has been changed
substantially already.

Note that you **NEVER** see the "real" source code of function
by printing it to the console.
The source code is in the source of the corresponding package,
in the case of 'stats', as part of the source code of R.

I.e., here,
 https://svn.r-project.org/R/trunk/src/library/stats/R/dendrogram.R


I think the following question has become irrelevant now,
but yes, dendrograms *are* implemented as nested lists.

Martin Maechler
ETH Zurich and R core team


    > The line in question is at line 128 of dendrogram.R in R-3.3.3, at stats::as.hclust.dendrogram:

    > stopifnot(length(s) == 2L, all( vapply(s, is.integer, NA) ))

    > Is there any legitimate possibility that s is a nested list? Currently I have a case where a dendrogram object is breaks at this line, because s is a nested list:

    >> str (s)
    > List of 2
    > $ : int -779
    > $ :List of 2
    > ..$ : int -625
    > ..$ : int 15

    > I'm unsure if my dendrogram was malformed in the first place, since I was trying to use dendrapply.

    > So, my question is: for that particular check, why use

    > stopifnot(length(s) == 2L, all( vapply(s, is.integer, NA) ))

    > instead of

    > stopifnot(length(s) == 2L, all( vapply(unlist(s), is.integer, NA) ))?

    > I appreciate your time and I'm looking forward to your response.

    > Cheers,

    > Man Chun John Ma, PhD
    > Postdoctoral Fellow
    > Unit 0903
    > Dept Lymphoma & Myeloma Research
    > 1515 Holcombe Blvd.
    > Houston, TX 77030
    > MMa at mdanderson.org

    > The information contained in this e-mail message may be ...{{dropped:14}}

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list