as.list method for by Objects

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

as.list method for by Objects

Dario Strbenac-2
Good day,

I'd like to suggest the addition of an as.list method for a by object that actually returns a list of class "list". This would make it safer to do type-checking, because is.list also returns TRUE for a data.frame variable and using class(result) == "list" is an alternative that only returns TRUE for lists. It's also confusing initially that

> class(x)
[1] "by"
> is.list(x)
[1] TRUE

since there's no explicit class definition for "by" and no mention if it has any superclasses.

--------------------------------------
Dario Strbenac
University of Sydney
Camperdown NSW 2050
Australia

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Michael Lawrence-3
I agree that it would make sense for the object to have c("by", "list") as
its class attribute, since the object is known to behave as a list.
However, it would may be too disruptive to make this change at this point.
Hard to predict.

Michael

On Mon, Jan 29, 2018 at 5:00 PM, Dario Strbenac <[hidden email]>
wrote:

> Good day,
>
> I'd like to suggest the addition of an as.list method for a by object that
> actually returns a list of class "list". This would make it safer to do
> type-checking, because is.list also returns TRUE for a data.frame variable
> and using class(result) == "list" is an alternative that only returns TRUE
> for lists. It's also confusing initially that
>
> > class(x)
> [1] "by"
> > is.list(x)
> [1] TRUE
>
> since there's no explicit class definition for "by" and no mention if it
> has any superclasses.
>
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Hervé Pagès-2
In reply to this post by Dario Strbenac-2
I agree that it makes sense to expect as.list() to perform
a "strict coercion" i.e. to return an object of class "list",
*even* on a list derivative. That's what as( , "list") does
by default:

   # on a data.frame object
   as(data.frame(), "list")  # object of class "list"
                             # (but strangely it drops the names)

   # on a by object
   x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
   as(x, "list")  # object of class "list"

More generally speaking as() is expected to perform "strict
coercion" by default, unless called with 'strict=FALSE'.

That's also what as.list() does on a data.frame:

   as.list(data.frame())  # object of class "list"

FWIW as.numeric() also performs "strict coercion" on an integer
vector:

   as.numeric(1:3)  # object of class "numeric"

So an as.list.env method that does the same as as(x, "list")
would bring a small touch of consistency in an otherwise
quite inconsistent world of coercion methods(*).

H.

(*) as(data.frame(), "list", strict=FALSE) doesn't do what you'd
     expect (just one of many examples)


On 01/29/2018 05:00 PM, Dario Strbenac wrote:

> Good day,
>
> I'd like to suggest the addition of an as.list method for a by object that actually returns a list of class "list". This would make it safer to do type-checking, because is.list also returns TRUE for a data.frame variable and using class(result) == "list" is an alternative that only returns TRUE for lists. It's also confusing initially that
>
>> class(x)
> [1] "by"
>> is.list(x)
> [1] TRUE
>
> since there's no explicit class definition for "by" and no mention if it has any superclasses.
>
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
>
> ______________________________________________
> [hidden email] mailing list
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=
>

--
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: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Hervé Pagès-2
On 01/30/2018 02:24 PM, Hervé Pagès wrote:

> I agree that it makes sense to expect as.list() to perform
> a "strict coercion" i.e. to return an object of class "list",
> *even* on a list derivative. That's what as( , "list") does
> by default:
>
>    # on a data.frame object
>    as(data.frame(), "list")  # object of class "list"
>                              # (but strangely it drops the names)
>
>    # on a by object
>    x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>    as(x, "list")  # object of class "list"
>
> More generally speaking as() is expected to perform "strict
> coercion" by default, unless called with 'strict=FALSE'.
>
> That's also what as.list() does on a data.frame:
>
>    as.list(data.frame())  # object of class "list"
>
> FWIW as.numeric() also performs "strict coercion" on an integer
> vector:
>
>    as.numeric(1:3)  # object of class "numeric"
>
> So an as.list.env method that does the same as as(x, "list")
         ^^^^^^^^^^^
oops, I meant as.list.by, sorry...

H.

> would bring a small touch of consistency in an otherwise
> quite inconsistent world of coercion methods(*).
>
> H.
>
> (*) as(data.frame(), "list", strict=FALSE) doesn't do what you'd
>      expect (just one of many examples)
>
>
> On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>> Good day,
>>
>> I'd like to suggest the addition of an as.list method for a by object
>> that actually returns a list of class "list". This would make it safer
>> to do type-checking, because is.list also returns TRUE for a
>> data.frame variable and using class(result) == "list" is an
>> alternative that only returns TRUE for lists. It's also confusing
>> initially that
>>
>>> class(x)
>> [1] "by"
>>> is.list(x)
>> [1] TRUE
>>
>> since there's no explicit class definition for "by" and no mention if
>> it has any superclasses.
>>
>> --------------------------------------
>> Dario Strbenac
>> University of Sydney
>> Camperdown NSW 2050
>> Australia
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e= 
>>
>>
>

--
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: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Gabriel Becker
In reply to this post by Hervé Pagès-2
Dario,

What version of R are you using. In my mildly old 3.4.0 installation and in
the version of Revel I have lying around (also mildly old...)  I don't see
the behavior I think you are describing

> b = by(1:2, 1:2, identity)

> class(as.list(b))

[1] "list"

> sessionInfo()

R Under development (unstable) (2017-12-19 r73926)

Platform: x86_64-apple-darwin15.6.0 (64-bit)

Running under: OS X El Capitan 10.11.6


Matrix products: default

BLAS:
/Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRblas.dylib

LAPACK:
/Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib


locale:

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8


attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base


loaded via a namespace (and not attached):

[1] compiler_3.5.0

>


As for by not having a class definition, no S3 class has an explicit
definition, so this is somewhat par for the course here...

did I misunderstand something?


~G

On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès <[hidden email]> wrote:

> I agree that it makes sense to expect as.list() to perform
> a "strict coercion" i.e. to return an object of class "list",
> *even* on a list derivative. That's what as( , "list") does
> by default:
>
>   # on a data.frame object
>   as(data.frame(), "list")  # object of class "list"
>                             # (but strangely it drops the names)
>
>   # on a by object
>   x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>   as(x, "list")  # object of class "list"
>
> More generally speaking as() is expected to perform "strict
> coercion" by default, unless called with 'strict=FALSE'.
>
> That's also what as.list() does on a data.frame:
>
>   as.list(data.frame())  # object of class "list"
>
> FWIW as.numeric() also performs "strict coercion" on an integer
> vector:
>
>   as.numeric(1:3)  # object of class "numeric"
>
> So an as.list.env method that does the same as as(x, "list")
> would bring a small touch of consistency in an otherwise
> quite inconsistent world of coercion methods(*).
>
> H.
>
> (*) as(data.frame(), "list", strict=FALSE) doesn't do what you'd
>     expect (just one of many examples)
>
>
> On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>
>> Good day,
>>
>> I'd like to suggest the addition of an as.list method for a by object
>> that actually returns a list of class "list". This would make it safer to
>> do type-checking, because is.list also returns TRUE for a data.frame
>> variable and using class(result) == "list" is an alternative that only
>> returns TRUE for lists. It's also confusing initially that
>>
>> class(x)
>>>
>> [1] "by"
>>
>>> is.list(x)
>>>
>> [1] TRUE
>>
>> since there's no explicit class definition for "by" and no mention if it
>> has any superclasses.
>>
>> --------------------------------------
>> Dario Strbenac
>> University of Sydney
>> Camperdown NSW 2050
>> Australia
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.et
>> hz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84V
>> tBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbM
>> rKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1daj
>> p8RLURfRz0-nbwH721jFAcEo&e=
>>
>>
> --
> 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: [hidden email]
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



--
Gabriel Becker, PhD
Scientist (Bioinformatics)
Genentech Research

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Hervé Pagès-2
Hi Gabe,

Interestingly the behavior of as.list() on by objects seem to
depend on the object itself:

 > b1 <- by(1:2, 1:2, identity)
 > class(as.list(b1))
[1] "list"

 > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
 > class(as.list(b2))
[1] "by"

This is with R 3.4.3 and R devel (2017-12-11 r73889).

H.

On 01/30/2018 02:33 PM, Gabriel Becker wrote:

> Dario,
>
> What version of R are you using. In my mildly old 3.4.0 installation and
> in the version of Revel I have lying around (also mildly old...)  I
> don't see the behavior I think you are describing
>
>     > b = by(1:2, 1:2, identity)
>
>     > class(as.list(b))
>
>     [1] "list"
>
>     > sessionInfo()
>
>     R Under development (unstable) (2017-12-19 r73926)
>
>     Platform: x86_64-apple-darwin15.6.0 (64-bit)
>
>     Running under: OS X El Capitan 10.11.6
>
>
>     Matrix products: default
>
>     BLAS:
>     /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRblas.dylib
>
>     LAPACK:
>     /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
>
>
>     locale:
>
>     [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
>
>     attached base packages:
>
>     [1] stats     graphics  grDevices utils     datasets  methods   base
>
>
>     loaded via a namespace (and not attached):
>
>     [1] compiler_3.5.0
>
>     >
>
>
>
> As for by not having a class definition, no S3 class has an explicit
> definition, so this is somewhat par for the course here...
>
> did I misunderstand something?
>
>
> ~G
>
> On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I agree that it makes sense to expect as.list() to perform
>     a "strict coercion" i.e. to return an object of class "list",
>     *even* on a list derivative. That's what as( , "list") does
>     by default:
>
>        # on a data.frame object
>        as(data.frame(), "list")  # object of class "list"
>                                  # (but strangely it drops the names)
>
>        # on a by object
>        x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>        as(x, "list")  # object of class "list"
>
>     More generally speaking as() is expected to perform "strict
>     coercion" by default, unless called with 'strict=FALSE'.
>
>     That's also what as.list() does on a data.frame:
>
>        as.list(data.frame())  # object of class "list"
>
>     FWIW as.numeric() also performs "strict coercion" on an integer
>     vector:
>
>        as.numeric(1:3)  # object of class "numeric"
>
>     So an as.list.env method that does the same as as(x, "list")
>     would bring a small touch of consistency in an otherwise
>     quite inconsistent world of coercion methods(*).
>
>     H.
>
>     (*) as(data.frame(), "list", strict=FALSE) doesn't do what you'd
>          expect (just one of many examples)
>
>
>     On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>
>         Good day,
>
>         I'd like to suggest the addition of an as.list method for a by
>         object that actually returns a list of class "list". This would
>         make it safer to do type-checking, because is.list also returns
>         TRUE for a data.frame variable and using class(result) == "list"
>         is an alternative that only returns TRUE for lists. It's also
>         confusing initially that
>
>             class(x)
>
>         [1] "by"
>
>             is.list(x)
>
>         [1] TRUE
>
>         since there's no explicit class definition for "by" and no
>         mention if it has any superclasses.
>
>         --------------------------------------
>         Dario Strbenac
>         University of Sydney
>         Camperdown NSW 2050
>         Australia
>
>         ______________________________________________
>         [hidden email] <mailto:[hidden email]> mailing list
>         https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=>
>
>
>     --
>     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: [hidden email] <mailto:[hidden email]>
>     Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>     Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=JIjTy48pWmEOxpHYM6DUHbRRSVwvOXOkgEFuoMhNkm0&s=x29ogWxfEnr2uKDcVEtKDWtB0USw8Xwm4f18WKBO-Dg&e=>
>
>
>
>
> --
> Gabriel Becker, PhD
> Scientist (Bioinformatics)
> Genentech Research

--
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: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Michael Lawrence-3
by() does not always return a list. In Gabe's example, it returns an
integer, thus it is coerced to a list. as.list() means that it should be a
VECSXP, not necessarily with "list" in the class attribute.

Michael

On Tue, Jan 30, 2018 at 2:41 PM, Hervé Pagès <[hidden email]> wrote:

> Hi Gabe,
>
> Interestingly the behavior of as.list() on by objects seem to
> depend on the object itself:
>
> > b1 <- by(1:2, 1:2, identity)
> > class(as.list(b1))
> [1] "list"
>
> > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
> > class(as.list(b2))
> [1] "by"
>
> This is with R 3.4.3 and R devel (2017-12-11 r73889).
>
> H.
>
> On 01/30/2018 02:33 PM, Gabriel Becker wrote:
>
>> Dario,
>>
>> What version of R are you using. In my mildly old 3.4.0 installation and
>> in the version of Revel I have lying around (also mildly old...)  I don't
>> see the behavior I think you are describing
>>
>>     > b = by(1:2, 1:2, identity)
>>
>>     > class(as.list(b))
>>
>>     [1] "list"
>>
>>     > sessionInfo()
>>
>>     R Under development (unstable) (2017-12-19 r73926)
>>
>>     Platform: x86_64-apple-darwin15.6.0 (64-bit)
>>
>>     Running under: OS X El Capitan 10.11.6
>>
>>
>>     Matrix products: default
>>
>>     BLAS:
>>     /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resour
>> ces/lib/libRblas.dylib
>>
>>     LAPACK:
>>     /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resour
>> ces/lib/libRlapack.dylib
>>
>>
>>     locale:
>>
>>     [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>
>>
>>     attached base packages:
>>
>>     [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>>
>>     loaded via a namespace (and not attached):
>>
>>     [1] compiler_3.5.0
>>
>>     >
>>
>>
>> As for by not having a class definition, no S3 class has an explicit
>> definition, so this is somewhat par for the course here...
>>
>> did I misunderstand something?
>>
>>
>> ~G
>>
>> On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     I agree that it makes sense to expect as.list() to perform
>>     a "strict coercion" i.e. to return an object of class "list",
>>     *even* on a list derivative. That's what as( , "list") does
>>     by default:
>>
>>        # on a data.frame object
>>        as(data.frame(), "list")  # object of class "list"
>>                                  # (but strangely it drops the names)
>>
>>        # on a by object
>>        x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>>        as(x, "list")  # object of class "list"
>>
>>     More generally speaking as() is expected to perform "strict
>>     coercion" by default, unless called with 'strict=FALSE'.
>>
>>     That's also what as.list() does on a data.frame:
>>
>>        as.list(data.frame())  # object of class "list"
>>
>>     FWIW as.numeric() also performs "strict coercion" on an integer
>>     vector:
>>
>>        as.numeric(1:3)  # object of class "numeric"
>>
>>     So an as.list.env method that does the same as as(x, "list")
>>     would bring a small touch of consistency in an otherwise
>>     quite inconsistent world of coercion methods(*).
>>
>>     H.
>>
>>     (*) as(data.frame(), "list", strict=FALSE) doesn't do what you'd
>>          expect (just one of many examples)
>>
>>
>>     On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>>
>>         Good day,
>>
>>         I'd like to suggest the addition of an as.list method for a by
>>         object that actually returns a list of class "list". This would
>>         make it safer to do type-checking, because is.list also returns
>>         TRUE for a data.frame variable and using class(result) == "list"
>>         is an alternative that only returns TRUE for lists. It's also
>>         confusing initially that
>>
>>             class(x)
>>
>>         [1] "by"
>>
>>             is.list(x)
>>
>>         [1] TRUE
>>
>>         since there's no explicit class definition for "by" and no
>>         mention if it has any superclasses.
>>
>>         --------------------------------------
>>         Dario Strbenac
>>         University of Sydney
>>         Camperdown NSW 2050
>>         Australia
>>
>>         ______________________________________________
>>         [hidden email] <mailto:[hidden email]> mailing list
>>         https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.et
>> hz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84V
>> tBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbM
>> rKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1daj
>> p8RLURfRz0-nbwH721jFAcEo&e=
>>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> 8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9
>> h1dajp8RLURfRz0-nbwH721jFAcEo&e=>
>>
>>
>>     --     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: [hidden email] <mailto:[hidden email]>
>>     Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>>     Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>>
>>
>>     ______________________________________________
>>     [hidden email] <mailto:[hidden email]> mailing list
>>     https://stat.ethz.ch/mailman/listinfo/r-devel
>>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> JIjTy48pWmEOxpHYM6DUHbRRSVwvOXOkgEFuoMhNkm0&s=x29ogWxfEnr2uK
>> DcVEtKDWtB0USw8Xwm4f18WKBO-Dg&e=>
>>
>>
>>
>>
>> --
>> Gabriel Becker, PhD
>> Scientist (Bioinformatics)
>> Genentech Research
>>
>
> --
> 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: [hidden email]
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Hervé Pagès-2
On 01/30/2018 02:50 PM, Michael Lawrence wrote:
> by() does not always return a list. In Gabe's example, it returns an
> integer, thus it is coerced to a list. as.list() means that it should be
> a VECSXP, not necessarily with "list" in the class attribute.

The documentation is not particularly clear about what as.list()
means for list derivatives. IMO clarifications should stick to
simple concepts and formulations like "is.list(x) is TRUE" or
"x is a list or a list derivative" rather than "x is a VECSXP".
Coercion is useful beyond the use case of implementing a .C entry
point and calling as.numeric/as.list/etc... on its arguments.

This is why I was hoping that we could maybe discuss the possibility
of making the as.list() contract less vague than just "as.list()
must return a list or a list derivative".

Again, I think that 2 things weight quite a lot in that discussion:
   1) as.list() returns an object of class "data.frame" on a
      data.frame (strict coercion). If all what as.list() needed to
      do was to return a VECSXP, then as.list.default() already does
      this on a data.frame so why did someone bother adding an
      as.list.data.frame method that does strict coercion?
   2) The S4 coercion system based on as() does strict coercion by
      default.

H.

>
> Michael
>
> On Tue, Jan 30, 2018 at 2:41 PM, Hervé Pagès <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi Gabe,
>
>     Interestingly the behavior of as.list() on by objects seem to
>     depend on the object itself:
>
>      > b1 <- by(1:2, 1:2, identity)
>      > class(as.list(b1))
>     [1] "list"
>
>      > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>      > class(as.list(b2))
>     [1] "by"
>
>     This is with R 3.4.3 and R devel (2017-12-11 r73889).
>
>     H.
>
>     On 01/30/2018 02:33 PM, Gabriel Becker wrote:
>
>         Dario,
>
>         What version of R are you using. In my mildly old 3.4.0
>         installation and in the version of Revel I have lying around
>         (also mildly old...)  I don't see the behavior I think you are
>         describing
>
>              > b = by(1:2, 1:2, identity)
>
>              > class(as.list(b))
>
>              [1] "list"
>
>              > sessionInfo()
>
>              R Under development (unstable) (2017-12-19 r73926)
>
>              Platform: x86_64-apple-darwin15.6.0 (64-bit)
>
>              Running under: OS X El Capitan 10.11.6
>
>
>              Matrix products: default
>
>              BLAS:
>            
>         /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRblas.dylib
>
>              LAPACK:
>            
>         /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
>
>
>              locale:
>
>              [1]
>         en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
>
>              attached base packages:
>
>              [1] stats     graphics  grDevices utils     datasets
>         methods   base
>
>
>              loaded via a namespace (and not attached):
>
>              [1] compiler_3.5.0
>
>              >
>
>
>         As for by not having a class definition, no S3 class has an
>         explicit definition, so this is somewhat par for the course here...
>
>         did I misunderstand something?
>
>
>         ~G
>
>         On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès
>         <[hidden email] <mailto:[hidden email]>
>         <mailto:[hidden email] <mailto:[hidden email]>>> wrote:
>
>              I agree that it makes sense to expect as.list() to perform
>              a "strict coercion" i.e. to return an object of class "list",
>              *even* on a list derivative. That's what as( , "list") does
>              by default:
>
>                 # on a data.frame object
>                 as(data.frame(), "list")  # object of class "list"
>                                           # (but strangely it drops the
>         names)
>
>                 # on a by object
>                 x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>                 as(x, "list")  # object of class "list"
>
>              More generally speaking as() is expected to perform "strict
>              coercion" by default, unless called with 'strict=FALSE'.
>
>              That's also what as.list() does on a data.frame:
>
>                 as.list(data.frame())  # object of class "list"
>
>              FWIW as.numeric() also performs "strict coercion" on an integer
>              vector:
>
>                 as.numeric(1:3)  # object of class "numeric"
>
>              So an as.list.env method that does the same as as(x, "list")
>              would bring a small touch of consistency in an otherwise
>              quite inconsistent world of coercion methods(*).
>
>              H.
>
>              (*) as(data.frame(), "list", strict=FALSE) doesn't do what
>         you'd
>                   expect (just one of many examples)
>
>
>              On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>
>                  Good day,
>
>                  I'd like to suggest the addition of an as.list method
>         for a by
>                  object that actually returns a list of class "list".
>         This would
>                  make it safer to do type-checking, because is.list also
>         returns
>                  TRUE for a data.frame variable and using class(result)
>         == "list"
>                  is an alternative that only returns TRUE for lists.
>         It's also
>                  confusing initially that
>
>                      class(x)
>
>                  [1] "by"
>
>                      is.list(x)
>
>                  [1] TRUE
>
>                  since there's no explicit class definition for "by" and no
>                  mention if it has any superclasses.
>
>                  --------------------------------------
>                  Dario Strbenac
>                  University of Sydney
>                  Camperdown NSW 2050
>                  Australia
>
>                  ______________________________________________
>         [hidden email] <mailto:[hidden email]>
>         <mailto:[hidden email] <mailto:[hidden email]>>
>         mailing list
>         https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=>
>                
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=>>
>
>
>              --     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: [hidden email] <mailto:[hidden email]>
>         <mailto:[hidden email] <mailto:[hidden email]>>
>              Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>         <tel:%28206%29%20667-5791>
>              Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>         <tel:%28206%29%20667-1319>
>
>
>              ______________________________________________
>         [hidden email] <mailto:[hidden email]>
>         <mailto:[hidden email] <mailto:[hidden email]>>
>         mailing list
>         https://stat.ethz.ch/mailman/listinfo/r-devel
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=V3BNiysTgyDXd28NM1GGUpZd9lWARN6uc3NIFowctfw&s=bjhWI85o-aZcT2fpOQtgjEy5gacOtmMDpIp2pLfaNmM&e=>
>            
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=JIjTy48pWmEOxpHYM6DUHbRRSVwvOXOkgEFuoMhNkm0&s=x29ogWxfEnr2uKDcVEtKDWtB0USw8Xwm4f18WKBO-Dg&e=
>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=JIjTy48pWmEOxpHYM6DUHbRRSVwvOXOkgEFuoMhNkm0&s=x29ogWxfEnr2uKDcVEtKDWtB0USw8Xwm4f18WKBO-Dg&e=>>
>
>
>
>
>         --
>         Gabriel Becker, PhD
>         Scientist (Bioinformatics)
>         Genentech Research
>
>
>     --
>     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: [hidden email] <mailto:[hidden email]>
>     Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>     Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=V3BNiysTgyDXd28NM1GGUpZd9lWARN6uc3NIFowctfw&s=bjhWI85o-aZcT2fpOQtgjEy5gacOtmMDpIp2pLfaNmM&e=>
>
>

--
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: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Michael Lawrence-3
I just meant that the minimal contract for as.list() appears to be that it
returns a VECSXP. To the user, we might say that is.list() will always
return TRUE. I'm not sure we can expect consistency across methods beyond
that, nor is it feasible at this point to match the semantics of the
methods package. It deals in "class space" while as.list() deals in
"typeof() space".

Michael

On Tue, Jan 30, 2018 at 3:47 PM, Hervé Pagès <[hidden email]> wrote:

> On 01/30/2018 02:50 PM, Michael Lawrence wrote:
>
>> by() does not always return a list. In Gabe's example, it returns an
>> integer, thus it is coerced to a list. as.list() means that it should be a
>> VECSXP, not necessarily with "list" in the class attribute.
>>
>
> The documentation is not particularly clear about what as.list()
> means for list derivatives. IMO clarifications should stick to
> simple concepts and formulations like "is.list(x) is TRUE" or
> "x is a list or a list derivative" rather than "x is a VECSXP".
> Coercion is useful beyond the use case of implementing a .C entry
> point and calling as.numeric/as.list/etc... on its arguments.
>
> This is why I was hoping that we could maybe discuss the possibility
> of making the as.list() contract less vague than just "as.list()
> must return a list or a list derivative".
>
> Again, I think that 2 things weight quite a lot in that discussion:
>   1) as.list() returns an object of class "data.frame" on a
>      data.frame (strict coercion). If all what as.list() needed to
>      do was to return a VECSXP, then as.list.default() already does
>      this on a data.frame so why did someone bother adding an
>      as.list.data.frame method that does strict coercion?
>   2) The S4 coercion system based on as() does strict coercion by
>      default.
>
> H.
>
>
>> Michael
>>
>>
>> On Tue, Jan 30, 2018 at 2:41 PM, Hervé Pagès <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     Hi Gabe,
>>
>>     Interestingly the behavior of as.list() on by objects seem to
>>     depend on the object itself:
>>
>>      > b1 <- by(1:2, 1:2, identity)
>>      > class(as.list(b1))
>>     [1] "list"
>>
>>      > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>>      > class(as.list(b2))
>>     [1] "by"
>>
>>     This is with R 3.4.3 and R devel (2017-12-11 r73889).
>>
>>     H.
>>
>>     On 01/30/2018 02:33 PM, Gabriel Becker wrote:
>>
>>         Dario,
>>
>>         What version of R are you using. In my mildly old 3.4.0
>>         installation and in the version of Revel I have lying around
>>         (also mildly old...)  I don't see the behavior I think you are
>>         describing
>>
>>              > b = by(1:2, 1:2, identity)
>>
>>              > class(as.list(b))
>>
>>              [1] "list"
>>
>>              > sessionInfo()
>>
>>              R Under development (unstable) (2017-12-19 r73926)
>>
>>              Platform: x86_64-apple-darwin15.6.0 (64-bit)
>>
>>              Running under: OS X El Capitan 10.11.6
>>
>>
>>              Matrix products: default
>>
>>              BLAS:
>>                     /Users/beckerg4/local/Rdevel/R
>> .framework/Versions/3.5/Resources/lib/libRblas.dylib
>>
>>              LAPACK:
>>                     /Users/beckerg4/local/Rdevel/R
>> .framework/Versions/3.5/Resources/lib/libRlapack.dylib
>>
>>
>>              locale:
>>
>>              [1]
>>         en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>
>>
>>              attached base packages:
>>
>>              [1] stats     graphics  grDevices utils     datasets
>>  methods   base
>>
>>
>>              loaded via a namespace (and not attached):
>>
>>              [1] compiler_3.5.0
>>
>>              >
>>
>>
>>         As for by not having a class definition, no S3 class has an
>>         explicit definition, so this is somewhat par for the course
>> here...
>>
>>         did I misunderstand something?
>>
>>
>>         ~G
>>
>>         On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès
>>         <[hidden email] <mailto:[hidden email]>
>>         <mailto:[hidden email] <mailto:[hidden email]>>>
>> wrote:
>>
>>              I agree that it makes sense to expect as.list() to perform
>>              a "strict coercion" i.e. to return an object of class "list",
>>              *even* on a list derivative. That's what as( , "list") does
>>              by default:
>>
>>                 # on a data.frame object
>>                 as(data.frame(), "list")  # object of class "list"
>>                                           # (but strangely it drops the
>>         names)
>>
>>                 # on a by object
>>                 x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"],
>> summary)
>>                 as(x, "list")  # object of class "list"
>>
>>              More generally speaking as() is expected to perform "strict
>>              coercion" by default, unless called with 'strict=FALSE'.
>>
>>              That's also what as.list() does on a data.frame:
>>
>>                 as.list(data.frame())  # object of class "list"
>>
>>              FWIW as.numeric() also performs "strict coercion" on an
>> integer
>>              vector:
>>
>>                 as.numeric(1:3)  # object of class "numeric"
>>
>>              So an as.list.env method that does the same as as(x, "list")
>>              would bring a small touch of consistency in an otherwise
>>              quite inconsistent world of coercion methods(*).
>>
>>              H.
>>
>>              (*) as(data.frame(), "list", strict=FALSE) doesn't do what
>>         you'd
>>                   expect (just one of many examples)
>>
>>
>>              On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>>
>>                  Good day,
>>
>>                  I'd like to suggest the addition of an as.list method
>>         for a by
>>                  object that actually returns a list of class "list".
>>         This would
>>                  make it safer to do type-checking, because is.list also
>>         returns
>>                  TRUE for a data.frame variable and using class(result)
>>         == "list"
>>                  is an alternative that only returns TRUE for lists.
>>         It's also
>>                  confusing initially that
>>
>>                      class(x)
>>
>>                  [1] "by"
>>
>>                      is.list(x)
>>
>>                  [1] TRUE
>>
>>                  since there's no explicit class definition for "by" and
>> no
>>                  mention if it has any superclasses.
>>
>>                  --------------------------------------
>>                  Dario Strbenac
>>                  University of Sydney
>>                  Camperdown NSW 2050
>>                  Australia
>>
>>                  ______________________________________________
>>         [hidden email] <mailto:[hidden email]>
>>         <mailto:[hidden email] <mailto:[hidden email]>>
>>         mailing list
>>         https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.et
>> hz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84V
>> tBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbM
>> rKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1daj
>> p8RLURfRz0-nbwH721jFAcEo&e=
>>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> 8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9
>> h1dajp8RLURfRz0-nbwH721jFAcEo&e=>
>>                         <https://urldefense.proofpoint
>> .com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-
>> 2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGb
>> WY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMV
>> wOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e=
>>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> 8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9
>> h1dajp8RLURfRz0-nbwH721jFAcEo&e=>>
>>
>>
>>              --     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: [hidden email] <mailto:[hidden email]>
>>         <mailto:[hidden email] <mailto:[hidden email]>>
>>              Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>>         <tel:%28206%29%20667-5791>
>>              Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>>         <tel:%28206%29%20667-1319>
>>
>>
>>              ______________________________________________
>>         [hidden email] <mailto:[hidden email]>
>>         <mailto:[hidden email] <mailto:[hidden email]>>
>>         mailing list
>>         https://stat.ethz.ch/mailman/listinfo/r-devel
>>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> V3BNiysTgyDXd28NM1GGUpZd9lWARN6uc3NIFowctfw&s=bjhWI85o-aZcT2
>> fpOQtgjEy5gacOtmMDpIp2pLfaNmM&e=>
>>                     <https://urldefense.proofpoint
>> .com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-
>> 2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGb
>> WY_wJYbW0WYiZvSXAJJKaaPhzWA&m=JIjTy48pWmEOxpHYM6DUHbRRSVwvOX
>> OkgEFuoMhNkm0&s=x29ogWxfEnr2uKDcVEtKDWtB0USw8Xwm4f18WKBO-Dg&e=
>>         <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> JIjTy48pWmEOxpHYM6DUHbRRSVwvOXOkgEFuoMhNkm0&s=x29ogWxfEnr2uK
>> DcVEtKDWtB0USw8Xwm4f18WKBO-Dg&e=>>
>>
>>
>>
>>
>>         --         Gabriel Becker, PhD
>>         Scientist (Bioinformatics)
>>         Genentech Research
>>
>>
>>     --     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: [hidden email] <mailto:[hidden email]>
>>     Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>>     Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>>
>>     ______________________________________________
>>     [hidden email] <mailto:[hidden email]> mailing list
>>     https://stat.ethz.ch/mailman/listinfo/r-devel
>>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
>> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAf
>> qt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
>> V3BNiysTgyDXd28NM1GGUpZd9lWARN6uc3NIFowctfw&s=bjhWI85o-aZcT2
>> fpOQtgjEy5gacOtmMDpIp2pLfaNmM&e=>
>>
>>
>>
> --
> 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: [hidden email]
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Martin Maechler
>>>>> Michael Lawrence <[hidden email]>
>>>>>     on Tue, 30 Jan 2018 15:57:42 -0800 writes:

    > I just meant that the minimal contract for as.list() appears to be that it
    > returns a VECSXP. To the user, we might say that is.list() will always
    > return TRUE.
   
Indeed. I also agree with Herv'e that the user level
documentation should rather mention  is.list(.) |--> TRUE  than
VECSXP, and interestingly for the experts among us,
the  is.list() primitive gives not only TRUE for  VECSXP  but
also of LISTSXP (the good ole' pairlists).

    > I'm not sure we can expect consistency across methods
    > beyond that, nor is it feasible at this point to match the
    > semantics of the methods package. It deals in "class
    > space" while as.list() deals in "typeof() space".

    > Michael

Yes, and that *is* the extra complexity we have in R (inherited
from S, I'd say)  which ideally wasn't there and of course is
not there in much younger languages/systems such as julia.

And --- by the way let me preach, for the "class space" ---
do __never__ use

      if(class(obj) == "<classname>")

in your code (I see this so often, shockingly to me ...) but rather use

      if(inherits(obj, "<classname>"))

instead.

Martin



    > On Tue, Jan 30, 2018 at 3:47 PM, Hervé Pagès <[hidden email]> wrote:

    >> On 01/30/2018 02:50 PM, Michael Lawrence wrote:
    >>
    >>> by() does not always return a list. In Gabe's example, it returns an
    >>> integer, thus it is coerced to a list. as.list() means that it should be a
    >>> VECSXP, not necessarily with "list" in the class attribute.
    >>>
    >>
    >> The documentation is not particularly clear about what as.list()
    >> means for list derivatives. IMO clarifications should stick to
    >> simple concepts and formulations like "is.list(x) is TRUE" or
    >> "x is a list or a list derivative" rather than "x is a VECSXP".
    >> Coercion is useful beyond the use case of implementing a .C entry
    >> point and calling as.numeric/as.list/etc... on its arguments.
    >>
    >> This is why I was hoping that we could maybe discuss the possibility
    >> of making the as.list() contract less vague than just "as.list()
    >> must return a list or a list derivative".
    >>
    >> Again, I think that 2 things weight quite a lot in that discussion:
    >> 1) as.list() returns an object of class "data.frame" on a
    >> data.frame (strict coercion). If all what as.list() needed to
    >> do was to return a VECSXP, then as.list.default() already does
    >> this on a data.frame so why did someone bother adding an
    >> as.list.data.frame method that does strict coercion?
    >> 2) The S4 coercion system based on as() does strict coercion by
    >> default.
    >>
    >> H.
    >>
    >>
    >>> Michael
    >>>
    >>>
    >>> On Tue, Jan 30, 2018 at 2:41 PM, Hervé Pagès <[hidden email]
    >>> <mailto:[hidden email]>> wrote:
    >>>
    >>> Hi Gabe,
    >>>
    >>> Interestingly the behavior of as.list() on by objects seem to
    >>> depend on the object itself:
    >>>
    >>> > b1 <- by(1:2, 1:2, identity)
    >>> > class(as.list(b1))
    >>> [1] "list"
    >>>
    >>> > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
    >>> > class(as.list(b2))
    >>> [1] "by"
    >>>
    >>> This is with R 3.4.3 and R devel (2017-12-11 r73889).
    >>>
    >>> H.
    >>>
    >>> On 01/30/2018 02:33 PM, Gabriel Becker wrote:
    >>>
    >>> Dario,
    >>>
    >>> What version of R are you using. In my mildly old 3.4.0
    >>> installation and in the version of Revel I have lying around
    >>> (also mildly old...)  I don't see the behavior I think you are
    >>> describing
    >>>
    >>> > b = by(1:2, 1:2, identity)
    >>>
    >>> > class(as.list(b))
    >>>
    >>> [1] "list"
    >>>
    >>> > sessionInfo()
    >>>
    >>> R Under development (unstable) (2017-12-19 r73926)
    >>>
    >>> Platform: x86_64-apple-darwin15.6.0 (64-bit)
    >>>
    >>> Running under: OS X El Capitan 10.11.6
    >>>
    >>>
    >>> Matrix products: default
    >>>
    >>> BLAS:
    >>> /Users/beckerg4/local/Rdevel/R
    >>> .framework/Versions/3.5/Resources/lib/libRblas.dylib
    >>>
    >>> LAPACK:
    >>> /Users/beckerg4/local/Rdevel/R
    >>> .framework/Versions/3.5/Resources/lib/libRlapack.dylib
    >>>
    >>>
    >>> locale:
    >>>
    >>> [1]
    >>> en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    >>>
    >>>
    >>> attached base packages:
    >>>
    >>> [1] stats     graphics  grDevices utils     datasets
    >>> methods   base
    >>>
    >>>
    >>> loaded via a namespace (and not attached):
    >>>
    >>> [1] compiler_3.5.0
    >>>
    >>> >
    >>>
    >>>
    >>> As for by not having a class definition, no S3 class has an
    >>> explicit definition, so this is somewhat par for the course
    >>> here...
    >>>
    >>> did I misunderstand something?
    >>>
    >>>
    >>> ~G
    >>>
    >>> On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès
    >>> <[hidden email] <mailto:[hidden email]>
    >>> <mailto:[hidden email] <mailto:[hidden email]>>>
    >>> wrote:
    >>>
    >>> I agree that it makes sense to expect as.list() to perform
    >>> a "strict coercion" i.e. to return an object of class "list",
    >>> *even* on a list derivative. That's what as( , "list") does
    >>> by default:
    >>>
    >>> # on a data.frame object
    >>> as(data.frame(), "list")  # object of class "list"
    >>> # (but strangely it drops the
    >>> names)
    >>>
    >>> # on a by object
    >>> x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"],
    >>> summary)
    >>> as(x, "list")  # object of class "list"
    >>>
    >>> More generally speaking as() is expected to perform "strict
    >>> coercion" by default, unless called with 'strict=FALSE'.
    >>>
    >>> That's also what as.list() does on a data.frame:
    >>>
    >>> as.list(data.frame())  # object of class "list"
    >>>
    >>> FWIW as.numeric() also performs "strict coercion" on an
    >>> integer
    >>> vector:
    >>>
    >>> as.numeric(1:3)  # object of class "numeric"
    >>>
    >>> So an as.list.env method that does the same as as(x, "list")
    >>> would bring a small touch of consistency in an otherwise
    >>> quite inconsistent world of coercion methods(*).
    >>>
    >>> H.
    >>>
    >>> (*) as(data.frame(), "list", strict=FALSE) doesn't do what
    >>> you'd
    >>> expect (just one of many examples)
    >>>
    >>>
    >>> On 01/29/2018 05:00 PM, Dario Strbenac wrote:
    >>>
    >>> Good day,
    >>>
    >>> I'd like to suggest the addition of an as.list method
    >>> for a by
    >>> object that actually returns a list of class "list".
    >>> This would
    >>> make it safer to do type-checking, because is.list also
    >>> returns
    >>> TRUE for a data.frame variable and using class(result)
    >>> == "list"
    >>> is an alternative that only returns TRUE for lists.
    >>> It's also
    >>> confusing initially that
    >>>
    >>> class(x)
    >>>
    >>> [1] "by"
    >>>
    >>> is.list(x)
    >>>
    >>> [1] TRUE
    >>>
    >>> since there's no explicit class definition for "by" and no
    >>> mention if it has any superclasses.
    >>>
    >>> --------------------------------------
    >>> Dario Strbenac
    >>> University of Sydney
    >>> Camperdown NSW 2050
    >>> Australia

    .............

    >>> --         Gabriel Becker, PhD
    >>> Scientist (Bioinformatics)
    >>> Genentech Research
    >>>

    >> 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: [hidden email]
    >> Phone:  (206) 667-5791
    >> Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Martin Maechler
In reply to this post by Michael Lawrence-3
>>>>> Michael Lawrence <[hidden email]>
>>>>>     on Tue, 30 Jan 2018 10:37:38 -0800 writes:

    > I agree that it would make sense for the object to have c("by", "list") as
    > its class attribute, since the object is known to behave as a list.

Well, but that (list behavior) applies to most non-simple S3
classed objects, say "data.frame", say "lm" to start with real basic ones.

The later part of the discussion, seems more relevant to me.
Adding "list" to the class attribute seems as wrong to me as
e.g. adding "double" to "Date" or "POSIXct" (and many more such cases).

For the present case, we should stay with focusing on  is.list()
being true after as.list() .. the same we would do with
as.numeric() and is.numeric().

Martin

    > However, it would may be too disruptive to make this change at this point.
    > Hard to predict.

    > Michael

    > On Mon, Jan 29, 2018 at 5:00 PM, Dario Strbenac <[hidden email]>
    > wrote:

    >> Good day,
    >>
    >> I'd like to suggest the addition of an as.list method for a by object that
    >> actually returns a list of class "list". This would make it safer to do
    >> type-checking, because is.list also returns TRUE for a data.frame variable
    >> and using class(result) == "list" is an alternative that only returns TRUE
    >> for lists. It's also confusing initially that
    >>
    >> > class(x)
    >> [1] "by"
    >> > is.list(x)
    >> [1] TRUE
    >>
    >> since there's no explicit class definition for "by" and no mention if it
    >> has any superclasses.
    >>
    >> --------------------------------------
    >> Dario Strbenac
    >> University of Sydney
    >> Camperdown NSW 2050
    >> Australia
    >>
    >> ______________________________________________
    >> [hidden email] mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel
    >>
    >>

    > [[alternative HTML version deleted]]

    > ______________________________________________
    > [hidden email] mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Michael Lawrence-3
On Thu, Feb 1, 2018 at 1:21 AM, Martin Maechler <[hidden email]>
wrote:

> >>>>> Michael Lawrence <[hidden email]>
> >>>>>     on Tue, 30 Jan 2018 10:37:38 -0800 writes:
>
>     > I agree that it would make sense for the object to have c("by",
> "list") as
>     > its class attribute, since the object is known to behave as a list.
>
> Well, but that (list behavior) applies to most non-simple S3
> classed objects, say "data.frame", say "lm" to start with real basic ones.
>
> The later part of the discussion, seems more relevant to me.
> Adding "list" to the class attribute seems as wrong to me as
> e.g. adding "double" to "Date" or "POSIXct" (and many more such cases).
>
>
There's a distinction though. Date and POSIXct should not really behave as
double values (an implementation detail), but "by" is expected to behave as
a list (when it is one).

For the present case, we should stay with focusing on  is.list()

> being true after as.list() .. the same we would do with
> as.numeric() and is.numeric().
>
> Martin
>
>     > However, it would may be too disruptive to make this change at this
> point.
>     > Hard to predict.
>
>     > Michael
>
>     > On Mon, Jan 29, 2018 at 5:00 PM, Dario Strbenac <
> [hidden email]>
>     > wrote:
>
>     >> Good day,
>     >>
>     >> I'd like to suggest the addition of an as.list method for a by
> object that
>     >> actually returns a list of class "list". This would make it safer
> to do
>     >> type-checking, because is.list also returns TRUE for a data.frame
> variable
>     >> and using class(result) == "list" is an alternative that only
> returns TRUE
>     >> for lists. It's also confusing initially that
>     >>
>     >> > class(x)
>     >> [1] "by"
>     >> > is.list(x)
>     >> [1] TRUE
>     >>
>     >> since there's no explicit class definition for "by" and no mention
> if it
>     >> has any superclasses.
>     >>
>     >> --------------------------------------
>     >> Dario Strbenac
>     >> University of Sydney
>     >> Camperdown NSW 2050
>     >> Australia
>     >>
>     >> ______________________________________________
>     >> [hidden email] mailing list
>     >> https://stat.ethz.ch/mailman/listinfo/r-devel
>     >>
>     >>
>
>     > [[alternative HTML version deleted]]
>
>     > ______________________________________________
>     > [hidden email] mailing list
>     > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Martin Maechler
>>>>> Michael Lawrence <[hidden email]>
>>>>>     on Thu, 1 Feb 2018 06:12:20 -0800 writes:

    > On Thu, Feb 1, 2018 at 1:21 AM, Martin Maechler <[hidden email]>
    > wrote:

    >> >>>>> Michael Lawrence <[hidden email]>
    >> >>>>>     on Tue, 30 Jan 2018 10:37:38 -0800 writes:
    >>
    >> > I agree that it would make sense for the object to have c("by",
    >> "list") as
    >> > its class attribute, since the object is known to behave as a list.
    >>
    >> Well, but that (list behavior) applies to most non-simple S3
    >> classed objects, say "data.frame", say "lm" to start with real basic ones.
    >>
    >> The later part of the discussion, seems more relevant to me.
    >> Adding "list" to the class attribute seems as wrong to me as
    >> e.g. adding "double" to "Date" or "POSIXct" (and many more such cases).
    >>
    >>
    > There's a distinction though. Date and POSIXct should not really behave as
    > double values (an implementation detail), but "by" is expected to behave as
    > a list (when it is one).

yes, you are right....  As I'm "never"(*) using by(), I'm glad
to leave this issue to you.

Martin

---
*) Never .... [James Bond, 1983]

    > For the present case, we should stay with focusing on  is.list()
    >> being true after as.list() .. the same we would do with
    >> as.numeric() and is.numeric().
    >>
    >> Martin
    >>
    >> > However, it would may be too disruptive to make this change at this
    >> point.
    >> > Hard to predict.
    >>
    >> > Michael
    >>
    >> > On Mon, Jan 29, 2018 at 5:00 PM, Dario Strbenac <
    >> [hidden email]>
    >> > wrote:
    >>
    >> >> Good day,
    >> >>
    >> >> I'd like to suggest the addition of an as.list method for a by
    >> object that
    >> >> actually returns a list of class "list". This would make it safer
    >> to do
    >> >> type-checking, because is.list also returns TRUE for a data.frame
    >> variable
    >> >> and using class(result) == "list" is an alternative that only
    >> returns TRUE
    >> >> for lists. It's also confusing initially that
    >> >>
    >> >> > class(x)
    >> >> [1] "by"
    >> >> > is.list(x)
    >> >> [1] TRUE
    >> >>
    >> >> since there's no explicit class definition for "by" and no mention
    >> if it
    >> >> has any superclasses.
    >> >>
    >> >> --------------------------------------
    >> >> Dario Strbenac
    >> >> University of Sydney
    >> >> Camperdown NSW 2050
    >> >> Australia
    >> >>
    >> >> ______________________________________________
    >> >> [hidden email] mailing list
    >> >> https://stat.ethz.ch/mailman/listinfo/r-devel
    >> >>
    >> >>
    >>
    >> > [[alternative HTML version deleted]]
    >>
    >> > ______________________________________________
    >> > [hidden email] mailing list
    >> > https://stat.ethz.ch/mailman/listinfo/r-devel
    >>
    >>

    > [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Henrik Bengtsson-5
In reply to this post by Martin Maechler
On Thu, Feb 1, 2018 at 12:14 AM, Martin Maechler
<[hidden email]> wrote:

>>>>>> Michael Lawrence <[hidden email]>
>>>>>>     on Tue, 30 Jan 2018 15:57:42 -0800 writes:
>
>     > I just meant that the minimal contract for as.list() appears to be that it
>     > returns a VECSXP. To the user, we might say that is.list() will always
>     > return TRUE.
>
> Indeed. I also agree with Herv'e that the user level
> documentation should rather mention  is.list(.) |--> TRUE  than
> VECSXP, and interestingly for the experts among us,
> the  is.list() primitive gives not only TRUE for  VECSXP  but
> also of LISTSXP (the good ole' pairlists).
>
>     > I'm not sure we can expect consistency across methods
>     > beyond that, nor is it feasible at this point to match the
>     > semantics of the methods package. It deals in "class
>     > space" while as.list() deals in "typeof() space".
>
>     > Michael
>
> Yes, and that *is* the extra complexity we have in R (inherited
> from S, I'd say)  which ideally wasn't there and of course is
> not there in much younger languages/systems such as julia.
>
> And --- by the way let me preach, for the "class space" ---
> do __never__ use
>
>       if(class(obj) == "<classname>")
>
> in your code (I see this so often, shockingly to me ...) but rather use
>
>       if(inherits(obj, "<classname>"))
>
> instead.

Second this one.  But, soon (*) the former will at least give the
correct answer when length(class(obj)) == 1 and produce an error
otherwise.  So, several of these cases will be caught at run-time in a
near future.

(*) When _R_CHECK_LENGTH_1_CONDITION_=true becomes the default
behavior - hopefully by R 3.5.0.

>
> Martin
>
>
>
>     > On Tue, Jan 30, 2018 at 3:47 PM, Hervé Pagès <[hidden email]> wrote:
>
>     >> On 01/30/2018 02:50 PM, Michael Lawrence wrote:
>     >>
>     >>> by() does not always return a list. In Gabe's example, it returns an
>     >>> integer, thus it is coerced to a list. as.list() means that it should be a
>     >>> VECSXP, not necessarily with "list" in the class attribute.
>     >>>
>     >>
>     >> The documentation is not particularly clear about what as.list()
>     >> means for list derivatives. IMO clarifications should stick to
>     >> simple concepts and formulations like "is.list(x) is TRUE" or
>     >> "x is a list or a list derivative" rather than "x is a VECSXP".
>     >> Coercion is useful beyond the use case of implementing a .C entry
>     >> point and calling as.numeric/as.list/etc... on its arguments.
>     >>
>     >> This is why I was hoping that we could maybe discuss the possibility
>     >> of making the as.list() contract less vague than just "as.list()
>     >> must return a list or a list derivative".
>     >>
>     >> Again, I think that 2 things weight quite a lot in that discussion:
>     >> 1) as.list() returns an object of class "data.frame" on a
>     >> data.frame (strict coercion). If all what as.list() needed to
>     >> do was to return a VECSXP, then as.list.default() already does
>     >> this on a data.frame so why did someone bother adding an
>     >> as.list.data.frame method that does strict coercion?
>     >> 2) The S4 coercion system based on as() does strict coercion by
>     >> default.
>     >>
>     >> H.
>     >>
>     >>
>     >>> Michael
>     >>>
>     >>>
>     >>> On Tue, Jan 30, 2018 at 2:41 PM, Hervé Pagès <[hidden email]
>     >>> <mailto:[hidden email]>> wrote:
>     >>>
>     >>> Hi Gabe,
>     >>>
>     >>> Interestingly the behavior of as.list() on by objects seem to
>     >>> depend on the object itself:
>     >>>
>     >>> > b1 <- by(1:2, 1:2, identity)
>     >>> > class(as.list(b1))
>     >>> [1] "list"
>     >>>
>     >>> > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
>     >>> > class(as.list(b2))
>     >>> [1] "by"
>     >>>
>     >>> This is with R 3.4.3 and R devel (2017-12-11 r73889).
>     >>>
>     >>> H.
>     >>>
>     >>> On 01/30/2018 02:33 PM, Gabriel Becker wrote:
>     >>>
>     >>> Dario,
>     >>>
>     >>> What version of R are you using. In my mildly old 3.4.0
>     >>> installation and in the version of Revel I have lying around
>     >>> (also mildly old...)  I don't see the behavior I think you are
>     >>> describing
>     >>>
>     >>> > b = by(1:2, 1:2, identity)
>     >>>
>     >>> > class(as.list(b))
>     >>>
>     >>> [1] "list"
>     >>>
>     >>> > sessionInfo()
>     >>>
>     >>> R Under development (unstable) (2017-12-19 r73926)
>     >>>
>     >>> Platform: x86_64-apple-darwin15.6.0 (64-bit)
>     >>>
>     >>> Running under: OS X El Capitan 10.11.6
>     >>>
>     >>>
>     >>> Matrix products: default
>     >>>
>     >>> BLAS:
>     >>> /Users/beckerg4/local/Rdevel/R
>     >>> .framework/Versions/3.5/Resources/lib/libRblas.dylib
>     >>>
>     >>> LAPACK:
>     >>> /Users/beckerg4/local/Rdevel/R
>     >>> .framework/Versions/3.5/Resources/lib/libRlapack.dylib
>     >>>
>     >>>
>     >>> locale:
>     >>>
>     >>> [1]
>     >>> en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>     >>>
>     >>>
>     >>> attached base packages:
>     >>>
>     >>> [1] stats     graphics  grDevices utils     datasets
>     >>> methods   base
>     >>>
>     >>>
>     >>> loaded via a namespace (and not attached):
>     >>>
>     >>> [1] compiler_3.5.0
>     >>>
>     >>> >
>     >>>
>     >>>
>     >>> As for by not having a class definition, no S3 class has an
>     >>> explicit definition, so this is somewhat par for the course
>     >>> here...
>     >>>
>     >>> did I misunderstand something?
>     >>>
>     >>>
>     >>> ~G
>     >>>
>     >>> On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès
>     >>> <[hidden email] <mailto:[hidden email]>
>     >>> <mailto:[hidden email] <mailto:[hidden email]>>>
>     >>> wrote:
>     >>>
>     >>> I agree that it makes sense to expect as.list() to perform
>     >>> a "strict coercion" i.e. to return an object of class "list",
>     >>> *even* on a list derivative. That's what as( , "list") does
>     >>> by default:
>     >>>
>     >>> # on a data.frame object
>     >>> as(data.frame(), "list")  # object of class "list"
>     >>> # (but strangely it drops the
>     >>> names)
>     >>>
>     >>> # on a by object
>     >>> x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"],
>     >>> summary)
>     >>> as(x, "list")  # object of class "list"
>     >>>
>     >>> More generally speaking as() is expected to perform "strict
>     >>> coercion" by default, unless called with 'strict=FALSE'.
>     >>>
>     >>> That's also what as.list() does on a data.frame:
>     >>>
>     >>> as.list(data.frame())  # object of class "list"
>     >>>
>     >>> FWIW as.numeric() also performs "strict coercion" on an
>     >>> integer
>     >>> vector:
>     >>>
>     >>> as.numeric(1:3)  # object of class "numeric"
>     >>>
>     >>> So an as.list.env method that does the same as as(x, "list")
>     >>> would bring a small touch of consistency in an otherwise
>     >>> quite inconsistent world of coercion methods(*).
>     >>>
>     >>> H.
>     >>>
>     >>> (*) as(data.frame(), "list", strict=FALSE) doesn't do what
>     >>> you'd
>     >>> expect (just one of many examples)
>     >>>
>     >>>
>     >>> On 01/29/2018 05:00 PM, Dario Strbenac wrote:
>     >>>
>     >>> Good day,
>     >>>
>     >>> I'd like to suggest the addition of an as.list method
>     >>> for a by
>     >>> object that actually returns a list of class "list".
>     >>> This would
>     >>> make it safer to do type-checking, because is.list also
>     >>> returns
>     >>> TRUE for a data.frame variable and using class(result)
>     >>> == "list"
>     >>> is an alternative that only returns TRUE for lists.
>     >>> It's also
>     >>> confusing initially that
>     >>>
>     >>> class(x)
>     >>>
>     >>> [1] "by"
>     >>>
>     >>> is.list(x)
>     >>>
>     >>> [1] TRUE
>     >>>
>     >>> since there's no explicit class definition for "by" and no
>     >>> mention if it has any superclasses.
>     >>>
>     >>> --------------------------------------
>     >>> Dario Strbenac
>     >>> University of Sydney
>     >>> Camperdown NSW 2050
>     >>> Australia
>
>     .............
>
>     >>> --         Gabriel Becker, PhD
>     >>> Scientist (Bioinformatics)
>     >>> Genentech Research
>     >>>
>
>     >> 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: [hidden email]
>     >> Phone:  (206) 667-5791
>     >> Fax:    (206) 667-1319
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Martin Maechler
>>>>> Henrik Bengtsson <[hidden email]>
>>>>>     on Thu, 1 Feb 2018 10:26:23 -0800 writes:

    > On Thu, Feb 1, 2018 at 12:14 AM, Martin Maechler
    > <[hidden email]> wrote:
    >>>>>>> Michael Lawrence <[hidden email]>
    >>>>>>> on Tue, 30 Jan 2018 15:57:42 -0800 writes:
    >>
    >> > I just meant that the minimal contract for as.list() appears to be that it
    >> > returns a VECSXP. To the user, we might say that is.list() will always
    >> > return TRUE.
    >>
    >> Indeed. I also agree with Herv'e that the user level
    >> documentation should rather mention  is.list(.) |--> TRUE  than
    >> VECSXP, and interestingly for the experts among us,
    >> the  is.list() primitive gives not only TRUE for  VECSXP  but
    >> also of LISTSXP (the good ole' pairlists).
    >>
    >> > I'm not sure we can expect consistency across methods
    >> > beyond that, nor is it feasible at this point to match the
    >> > semantics of the methods package. It deals in "class
    >> > space" while as.list() deals in "typeof() space".
    >>
    >> > Michael
    >>
    >> Yes, and that *is* the extra complexity we have in R (inherited
    >> from S, I'd say)  which ideally wasn't there and of course is
    >> not there in much younger languages/systems such as julia.
    >>
    >> And --- by the way let me preach, for the "class space" ---
    >> do __never__ use
    >>
    >> if(class(obj) == "<classname>")
    >>
    >> in your code (I see this so often, shockingly to me ...) but rather use
    >>
    >> if(inherits(obj, "<classname>"))
    >>
    >> instead.

    > Second this one.  But, soon (*) the former will at least give the
    > correct answer when length(class(obj)) == 1
    > and produce an error
    > otherwise.

Not quite; I think you you did not get the real danger in using
'class(.) == *':
What you say above would only be true if there were only S3 classes!
Try the following small R snippet

myDate <- setClass("myDate", contains = "Date")
## Object of class "myDate"
## [1] "2018-02-02"
(d <- myDate(Sys.Date()))
class(d) == "Date"  # is FALSE (hence of length 1)
inherits(d, "Date") # is TRUE

    > So, several of these cases will be caught at run-time in a
    > near future.

Maybe.  But all the others are  still wrong, as I show above.
Martin

    > (*) When _R_CHECK_LENGTH_1_CONDITION_=true becomes the default
    > behavior - hopefully by R 3.5.0.

    >>
    >> Martin

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

R devel mailing list
In reply to this post by Dario Strbenac-2
Maybe behavior of 'as.list' in R is not inherited from S?
- From https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=78 , in "the prototype" (S), 'as.list' on a data frame gave a list, not a data frame as given by the default 'as.list' in R. That led to introduction of 'as.list.data.frame'.
- From https://biostat-lists.wustl.edu/sympa/arc/s-news/1999-07/msg00198.html , with
s <- c("a"=1, "b"=2) ,
as.list(z) doesn't have names in S-PLUS 3.4, different from in R. In S-PLUS 5.1, as.list(z) has names, like in R.

In "Details" section, the documentation, list.Rd, mentions this about 'as.list'.
Attributes may be dropped unless the argument already is a list or expression.  (This is inconsistent with functions such as as.character which always drop attributes, and is for efficiency since lists can be expensive to copy.)

On efficiency issue, shallow copying has been introduced. So, can the behavior of the default method of 'as.list' be reconsidered?

Related: The default mehod of 'as.vector' with mode="list" behaves like the default method of 'as.list'. As a consequence, 'is.vector' with mode="list" on its result may return FALSE. I have raised the issue in https://stat.ethz.ch/pipermail/r-devel/2013-May/066671.html .

------------------------
>>>>> Michael Lawrence <lawrence.michael at gene.com>
>>>>>     on Tue, 30 Jan 2018 15:57:42 -0800 writes:

    > I just meant that the minimal contract for as.list() appears to be that it
    > returns a VECSXP. To the user, we might say that is.list() will always
    > return TRUE.
   
Indeed. I also agree with Herv'e that the user level
documentation should rather mention  is.list(.) |--> TRUE  than
VECSXP, and interestingly for the experts among us,
the  is.list() primitive gives not only TRUE for  VECSXP  but
also of LISTSXP (the good ole' pairlists).

    > I'm not sure we can expect consistency across methods
    > beyond that, nor is it feasible at this point to match the
    > semantics of the methods package. It deals in "class
    > space" while as.list() deals in "typeof() space".

    > Michael

Yes, and that *is* the extra complexity we have in R (inherited
from S, I'd say)  which ideally wasn't there and of course is
not there in much younger languages/systems such as julia.

And --- by the way let me preach, for the "class space" ---
do __never__ use

      if(class(obj) == "<classname>")

in your code (I see this so often, shockingly to me ...) but rather use

      if(inherits(obj, "<classname>"))

instead.

Martin



    > On Tue, Jan 30, 2018 at 3:47 PM, Hervé Pagès <hpages at fredhutch.org> wrote:

    >> On 01/30/2018 02:50 PM, Michael Lawrence wrote:
    >>
    >>> by() does not always return a list. In Gabe's example, it returns an
    >>> integer, thus it is coerced to a list. as.list() means that it should be a
    >>> VECSXP, not necessarily with "list" in the class attribute.
    >>>
    >>
    >> The documentation is not particularly clear about what as.list()
    >> means for list derivatives. IMO clarifications should stick to
    >> simple concepts and formulations like "is.list(x) is TRUE" or
    >> "x is a list or a list derivative" rather than "x is a VECSXP".
    >> Coercion is useful beyond the use case of implementing a .C entry
    >> point and calling as.numeric/as.list/etc... on its arguments.
    >>
    >> This is why I was hoping that we could maybe discuss the possibility
    >> of making the as.list() contract less vague than just "as.list()
    >> must return a list or a list derivative".
    >>
    >> Again, I think that 2 things weight quite a lot in that discussion:
    >> 1) as.list() returns an object of class "data.frame" on a
    >> data.frame (strict coercion). If all what as.list() needed to
    >> do was to return a VECSXP, then as.list.default() already does
    >> this on a data.frame so why did someone bother adding an
    >> as.list.data.frame method that does strict coercion?
    >> 2) The S4 coercion system based on as() does strict coercion by
    >> default.
    >>
    >> H.
    >>
    >>
    >>> Michael
    >>>
    >>>
    >>> On Tue, Jan 30, 2018 at 2:41 PM, Hervé Pagès <hpages at fredhutch.org
    >>> <mailto:hpages at fredhutch.org>> wrote:
    >>>
    >>> Hi Gabe,
    >>>
    >>> Interestingly the behavior of as.list() on by objects seem to
    >>> depend on the object itself:
    >>>
    >>> > b1 <- by(1:2, 1:2, identity)
    >>> > class(as.list(b1))
    >>> [1] "list"
    >>>
    >>> > b2 <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
    >>> > class(as.list(b2))
    >>> [1] "by"
    >>>
    >>> This is with R 3.4.3 and R devel (2017-12-11 r73889).
    >>>
    >>> H.
    >>>
    >>> On 01/30/2018 02:33 PM, Gabriel Becker wrote:
    >>>
    >>> Dario,
    >>>
    >>> What version of R are you using. In my mildly old 3.4.0
    >>> installation and in the version of Revel I have lying around
    >>> (also mildly old...)  I don't see the behavior I think you are
    >>> describing
    >>>
    >>> > b = by(1:2, 1:2, identity)
    >>>
    >>> > class(as.list(b))
    >>>
    >>> [1] "list"
    >>>
    >>> > sessionInfo()
    >>>
    >>> R Under development (unstable) (2017-12-19 r73926)
    >>>
    >>> Platform: x86_64-apple-darwin15.6.0 (64-bit)
    >>>
    >>> Running under: OS X El Capitan 10.11.6
    >>>
    >>>
    >>> Matrix products: default
    >>>
    >>> BLAS:
    >>> /Users/beckerg4/local/Rdevel/R
    >>> .framework/Versions/3.5/Resources/lib/libRblas.dylib
    >>>
    >>> LAPACK:
    >>> /Users/beckerg4/local/Rdevel/R
    >>> .framework/Versions/3.5/Resources/lib/libRlapack.dylib
    >>>
    >>>
    >>> locale:
    >>>
    >>> [1]
    >>> en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    >>>
    >>>
    >>> attached base packages:
    >>>
    >>> [1] stats     graphics  grDevices utils     datasets
    >>> methods   base
    >>>
    >>>
    >>> loaded via a namespace (and not attached):
    >>>
    >>> [1] compiler_3.5.0
    >>>
    >>> >
    >>>
    >>>
    >>> As for by not having a class definition, no S3 class has an
    >>> explicit definition, so this is somewhat par for the course
    >>> here...
    >>>
    >>> did I misunderstand something?
    >>>
    >>>
    >>> ~G
    >>>
    >>> On Tue, Jan 30, 2018 at 2:24 PM, Hervé Pagès
    >>> <hpages at fredhutch.org <mailto:hpages at fredhutch.org>
    >>> <mailto:hpages at fredhutch.org <mailto:hpages at fredhutch.org>>>
    >>> wrote:
    >>>
    >>> I agree that it makes sense to expect as.list() to perform
    >>> a "strict coercion" i.e. to return an object of class "list",
    >>> *even* on a list derivative. That's what as( , "list") does
    >>> by default:
    >>>
    >>> # on a data.frame object
    >>> as(data.frame(), "list")  # object of class "list"
    >>> # (but strangely it drops the
    >>> names)
    >>>
    >>> # on a by object
    >>> x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"],
    >>> summary)
    >>> as(x, "list")  # object of class "list"
    >>>
    >>> More generally speaking as() is expected to perform "strict
    >>> coercion" by default, unless called with 'strict=FALSE'.
    >>>
    >>> That's also what as.list() does on a data.frame:
    >>>
    >>> as.list(data.frame())  # object of class "list"
    >>>
    >>> FWIW as.numeric() also performs "strict coercion" on an
    >>> integer
    >>> vector:
    >>>
    >>> as.numeric(1:3)  # object of class "numeric"
    >>>
    >>> So an as.list.env method that does the same as as(x, "list")
    >>> would bring a small touch of consistency in an otherwise
    >>> quite inconsistent world of coercion methods(*).
    >>>
    >>> H.
    >>>
    >>> (*) as(data.frame(), "list", strict=FALSE) doesn't do what
    >>> you'd
    >>> expect (just one of many examples)
    >>>
    >>>
    >>> On 01/29/2018 05:00 PM, Dario Strbenac wrote:
    >>>
    >>> Good day,
    >>>
    >>> I'd like to suggest the addition of an as.list method
    >>> for a by
    >>> object that actually returns a list of class "list".
    >>> This would
    >>> make it safer to do type-checking, because is.list also
    >>> returns
    >>> TRUE for a data.frame variable and using class(result)
    >>> == "list"
    >>> is an alternative that only returns TRUE for lists.
    >>> It's also
    >>> confusing initially that
    >>>
    >>> class(x)
    >>>
    >>> [1] "by"
    >>>
    >>> is.list(x)
    >>>
    >>> [1] TRUE
    >>>
    >>> since there's no explicit class definition for "by" and no
    >>> mention if it has any superclasses.
    >>>
    >>> --------------------------------------
    >>> Dario Strbenac
    >>> University of Sydney
    >>> Camperdown NSW 2050
    >>> Australia

    .............

    >>> --         Gabriel Becker, PhD
    >>> Scientist (Bioinformatics)
    >>> Genentech Research
    >>>

    >> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.list method for by Objects

Henrik Bengtsson-5
In reply to this post by Martin Maechler
On Fri, Feb 2, 2018 at 12:07 AM, Martin Maechler
<[hidden email]> wrote:

>>>>>> Henrik Bengtsson <[hidden email]>
>>>>>>     on Thu, 1 Feb 2018 10:26:23 -0800 writes:
>
>     > On Thu, Feb 1, 2018 at 12:14 AM, Martin Maechler
>     > <[hidden email]> wrote:
>     >>>>>>> Michael Lawrence <[hidden email]>
>     >>>>>>> on Tue, 30 Jan 2018 15:57:42 -0800 writes:
>     >>
>     >> > I just meant that the minimal contract for as.list() appears to be that it
>     >> > returns a VECSXP. To the user, we might say that is.list() will always
>     >> > return TRUE.
>     >>
>     >> Indeed. I also agree with Herv'e that the user level
>     >> documentation should rather mention  is.list(.) |--> TRUE  than
>     >> VECSXP, and interestingly for the experts among us,
>     >> the  is.list() primitive gives not only TRUE for  VECSXP  but
>     >> also of LISTSXP (the good ole' pairlists).
>     >>
>     >> > I'm not sure we can expect consistency across methods
>     >> > beyond that, nor is it feasible at this point to match the
>     >> > semantics of the methods package. It deals in "class
>     >> > space" while as.list() deals in "typeof() space".
>     >>
>     >> > Michael
>     >>
>     >> Yes, and that *is* the extra complexity we have in R (inherited
>     >> from S, I'd say)  which ideally wasn't there and of course is
>     >> not there in much younger languages/systems such as julia.
>     >>
>     >> And --- by the way let me preach, for the "class space" ---
>     >> do __never__ use
>     >>
>     >> if(class(obj) == "<classname>")
>     >>
>     >> in your code (I see this so often, shockingly to me ...) but rather use
>     >>
>     >> if(inherits(obj, "<classname>"))
>     >>
>     >> instead.
>
>     > Second this one.  But, soon (*) the former will at least give the
>     > correct answer when length(class(obj)) == 1
>     > and produce an error
>     > otherwise.
>
> Not quite; I think you you did not get the real danger in using
> 'class(.) == *':
> What you say above would only be true if there were only S3 classes!
> Try the following small R snippet
>
> myDate <- setClass("myDate", contains = "Date")
> ## Object of class "myDate"
> ## [1] "2018-02-02"
> (d <- myDate(Sys.Date()))
> class(d) == "Date"  # is FALSE (hence of length 1)
> inherits(d, "Date") # is TRUE
>
>     > So, several of these cases will be caught at run-time in a
>     > near future.
>
> Maybe.  But all the others are  still wrong, as I show above.

Oh my, thanks for clarifying/emphasizing.  I hope I didn't mislead too
many people.  I've been away from S4 for too long - I like to stay in
the cozy S3 world :)

/Henrik

> Martin
>
>     > (*) When _R_CHECK_LENGTH_1_CONDITION_=true becomes the default
>     > behavior - hopefully by R 3.5.0.
>
>     >>
>     >> Martin

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel