[Bioc-devel] Adding a lengths() method to List class

Michael Lawrence lawrence.michael at gene.com
Thu Oct 15 00:33:08 CEST 2015


On Wed, Oct 14, 2015 at 2:50 PM, Henrik Bengtsson <henrik.bengtsson at ucsf.edu
> wrote:

> Of topic in the sense that it's unrelated to List, but it is related
> to lengths().  Feel free to take any follow ups offline.
>
> Any chance to add support for S3 dispatching as well?
>
> For instance, on R Under development (unstable) (2015-10-12 r69509),
> none of the following dispatches on S3 class:
>
> > lengths.Foo <- function(x, ...) 0L
> > x <- structure(new.env(), class="Foo")
> > str(x)
> Class 'Foo' <environment: 0x000000000fb35e90>
> > lengths(x)
> Error in lengths(x) : 'x' must be a list or atomic vector
>
> > lengths.Bar <- function(x, ...) 0L
> > x <- structure(1:2, class="Bar")
> > str(x)
> Class 'Bar'  int [1:2] 1 2
> > lengths(x)
> [1] 1 1
>
> It appears that the dispatching was added for S4 methods, but not for
> S3 ones, cf.
> https://github.com/wch/r-source/blob/f42ee5e7ecf89a245afd6619b46483f1e3594ab7/src/main/array.c#L504
>
>
No, that change only makes it so that the code will loop over an S4
list-like object using the length and [[ methods (this already works for an
S3 "object"). The code to actually dispatch to S3 and S4 methods on
lengths() exists only in my working copy ;) But it will soon be committed
after I get stuff past the tests.


> I'm trying to add lenghts() for list environments, cf.
> https://github.com/HenrikBengtsson/listenv/issues/14
>
> Thank you
>
> Henrik
>
> On Fri, Oct 2, 2015 at 3:04 AM, Peter Hickey <peter.hickey at gmail.com>
> wrote:
> > Thanks, Michael!
> >
> > On Fri, 2 Oct 2015 at 13:54 Michael Lawrence <lawrence.michael at gene.com>
> > wrote:
> >
> >> Change was made. Should dispatch to length and [[ methods.
> >>
> >> On Wed, Sep 30, 2015 at 9:37 PM, Hervé Pagès <hpages at fredhutch.org>
> wrote:
> >>
> >>> On 09/30/2015 05:28 PM, Michael Lawrence wrote:
> >>>
> >>>> It wasn't a conscious choice, but it would slow things down a bit. Not
> >>>> by much though, since we're already attempting dispatch on length(). I
> >>>> can make the change.
> >>>>
> >>>
> >>> That would be great. Thanks Michael!
> >>>
> >>> H.
> >>>
> >>>
> >>>> On Wed, Sep 30, 2015 at 1:33 PM, Hervé Pagès <hpages at fredhutch.org
> >>>> <mailto:hpages at fredhutch.org>> wrote:
> >>>>
> >>>>     Hi Michael,
> >>>>
> >>>>     I was expecting this to just work:
> >>>>
> >>>>        base::lengths(IntegerList(1:4, 1:6))
> >>>>
> >>>>     but it doesn't:
> >>>>
> >>>>        Error in base::lengths(IntegerList(1:4, 1:6)) :
> >>>>          'x' must be a list or atomic vector
> >>>>
> >>>>     The man page says:
> >>>>
> >>>>           This function loops over ‘x’ and returns a compatible vector
> >>>>           containing the length of each element in ‘x’.  Effectively,
> >>>>           ‘length(x[[i]])’ is called for all ‘i’, so any methods on
> >>>> ‘length’
> >>>>           are considered.
> >>>>
> >>>>     If length(x[[i]]) is called for all i then it should work on any
> >>>> object
> >>>>     for which [[ is defined. Note that this is what happens with
> >>>>     base::sapply(), base::mapply(), etc... they all use [[ internally.
> >>>>
> >>>>     Do you know of any reason why lengths() doesn't do this?
> >>>>
> >>>>     Thanks,
> >>>>     H.
> >>>>
> >>>>
> >>>>     On 09/28/2015 09:51 PM, Michael Lawrence wrote:
> >>>>
> >>>>         That is the plan. Note that we already have elementLengths()
> >>>>         that serves
> >>>>         the same purpose. It was the direct inspiration for lengths().
> >>>>
> >>>>         On Mon, Sep 28, 2015 at 9:41 PM, Peter Hickey
> >>>>         <peter.hickey at gmail.com <mailto:peter.hickey at gmail.com>>
> >>>>         wrote:
> >>>>
> >>>>             The lengths() function was added in R 3.2 to "get the
> length
> >>>>             of each
> >>>>             element of a list or atomic vector (is.atomic) as an
> integer
> >>>>             or numeric
> >>>>             vector." It seems useful to me to have also a similar
> method
> >>>>             defined for
> >>>>             the S4Vectors::List class (and subclasses). What do others
> >>>>             think?
> >>>>
> >>>>                       [[alternative HTML version deleted]]
> >>>>
> >>>>             _______________________________________________
> >>>>             Bioc-devel at r-project.org <mailto:Bioc-devel at r-project.org
> >
> >>>>             mailing list
> >>>>             https://stat.ethz.ch/mailman/listinfo/bioc-devel
> >>>>
> >>>>
> >>>>                  [[alternative HTML version deleted]]
> >>>>
> >>>>         _______________________________________________
> >>>>         Bioc-devel at r-project.org <mailto:Bioc-devel at r-project.org>
> >>>>         mailing list
> >>>>         https://stat.ethz.ch/mailman/listinfo/bioc-devel
> >>>>
> >>>>
> >>>>     --
> >>>>     Hervé Pagès
> >>>>
> >>>>     Program in Computational Biology
> >>>>     Division of Public Health Sciences
> >>>>     Fred Hutchinson Cancer Research Center
> >>>>     1100 Fairview Ave. N, M1-B514
> >>>>     P.O. Box 19024
> >>>>     Seattle, WA 98109-1024
> >>>>
> >>>>     E-mail: hpages at fredhutch.org <mailto:hpages at fredhutch.org>
> >>>>     Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
> >>>>     Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
> >>>>
> >>>>
> >>>>
> >>> --
> >>> Hervé Pagès
> >>>
> >>> Program in Computational Biology
> >>> Division of Public Health Sciences
> >>> Fred Hutchinson Cancer Research Center
> >>> 1100 Fairview Ave. N, M1-B514
> >>> P.O. Box 19024
> >>> Seattle, WA 98109-1024
> >>>
> >>> E-mail: hpages at fredhutch.org
> >>> Phone:  (206) 667-5791
> >>> Fax:    (206) 667-1319
> >>>
> >>
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > Bioc-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/bioc-devel
>

	[[alternative HTML version deleted]]



More information about the Bioc-devel mailing list