surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)

Hervé Pagès-2
Hi,

It's nice to be able to define S4 classes with slots that correspond
to standard attributes:

   setClass("A1", slots=c(names="character"))
   setClass("A2", slots=c(dim="integer"))
   setClass("A3", slots=c(dimnames="list"))

By doing this, one gets a few methods for free:

   a1 <- new("A1", names=letters[1:3])
   names(a1)     # "a" "b" "c"
   a2 <- new("A2", dim=4:3)
   nrow(a2)      # 4
   a3 <- new("A3", dimnames=list(NULL, letters[1:3]))
   colnames(a3)  # "a" "b" "c"

However, when it comes to subclassing, some of these slots cause
problems. I can extend A1:

   setClass("B1", contains="A1")

but trying to extend A2 or A3 produces an error (with a non-informative
message in the 1st case and a somewhat obscure one in the 2nd):

   setClass("B2", contains="A2")
   # Error in attr(prototype, slotName) <- attr(pri, slotName) :
   #   invalid first argument

   setClass("B3", contains="A3")
   # Error in attr(prototype, slotName) <- attr(pri, slotName) :
   #   'dimnames' applied to non-array

So it seems that the presence of a "dim" or "dimnames" slot prevents a
class from being extended. Is this expected? I couldn't find anything
in TFM about this. Sorry if I missed it.

Thanks,
H.

--
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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)

Michael Lawrence-3
Thanks for the report. The issue is that one cannot set special attributes
like names, dim, dimnames, etc on S4 objects. I was aready working on this
and will have a fix soon.

> a2 <- new("A2")
> dim(a2) <- c(2, 3)
Error in dim(a2) <- c(2, 3) : invalid first argument


On Mon, Jun 5, 2017 at 6:08 PM, Hervé Pagès <[hidden email]> wrote:

> Hi,
>
> It's nice to be able to define S4 classes with slots that correspond
> to standard attributes:
>
>   setClass("A1", slots=c(names="character"))
>   setClass("A2", slots=c(dim="integer"))
>   setClass("A3", slots=c(dimnames="list"))
>
> By doing this, one gets a few methods for free:
>
>   a1 <- new("A1", names=letters[1:3])
>   names(a1)     # "a" "b" "c"
>   a2 <- new("A2", dim=4:3)
>   nrow(a2)      # 4
>   a3 <- new("A3", dimnames=list(NULL, letters[1:3]))
>   colnames(a3)  # "a" "b" "c"
>
> However, when it comes to subclassing, some of these slots cause
> problems. I can extend A1:
>
>   setClass("B1", contains="A1")
>
> but trying to extend A2 or A3 produces an error (with a non-informative
> message in the 1st case and a somewhat obscure one in the 2nd):
>
>   setClass("B2", contains="A2")
>   # Error in attr(prototype, slotName) <- attr(pri, slotName) :
>   #   invalid first argument
>
>   setClass("B3", contains="A3")
>   # Error in attr(prototype, slotName) <- attr(pri, slotName) :
>   #   'dimnames' applied to non-array
>
> So it seems that the presence of a "dim" or "dimnames" slot prevents a
> class from being extended. Is this expected? I couldn't find anything
> in TFM about this. Sorry if I missed it.
>
> Thanks,
> H.
>
> --
> 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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)

Michael Lawrence-3
I've fixed this and will commit soon.

Disregard my dim<-() example; that behaves as expected (the class needs a
dim<-() method).

Michael

On Tue, Jun 6, 2017 at 5:16 AM, Michael Lawrence <[hidden email]> wrote:

> Thanks for the report. The issue is that one cannot set special attributes
> like names, dim, dimnames, etc on S4 objects. I was aready working on this
> and will have a fix soon.
>
> > a2 <- new("A2")
> > dim(a2) <- c(2, 3)
> Error in dim(a2) <- c(2, 3) : invalid first argument
>
>
> On Mon, Jun 5, 2017 at 6:08 PM, Hervé Pagès <[hidden email]> wrote:
>
>> Hi,
>>
>> It's nice to be able to define S4 classes with slots that correspond
>> to standard attributes:
>>
>>   setClass("A1", slots=c(names="character"))
>>   setClass("A2", slots=c(dim="integer"))
>>   setClass("A3", slots=c(dimnames="list"))
>>
>> By doing this, one gets a few methods for free:
>>
>>   a1 <- new("A1", names=letters[1:3])
>>   names(a1)     # "a" "b" "c"
>>   a2 <- new("A2", dim=4:3)
>>   nrow(a2)      # 4
>>   a3 <- new("A3", dimnames=list(NULL, letters[1:3]))
>>   colnames(a3)  # "a" "b" "c"
>>
>> However, when it comes to subclassing, some of these slots cause
>> problems. I can extend A1:
>>
>>   setClass("B1", contains="A1")
>>
>> but trying to extend A2 or A3 produces an error (with a non-informative
>> message in the 1st case and a somewhat obscure one in the 2nd):
>>
>>   setClass("B2", contains="A2")
>>   # Error in attr(prototype, slotName) <- attr(pri, slotName) :
>>   #   invalid first argument
>>
>>   setClass("B3", contains="A3")
>>   # Error in attr(prototype, slotName) <- attr(pri, slotName) :
>>   #   'dimnames' applied to non-array
>>
>> So it seems that the presence of a "dim" or "dimnames" slot prevents a
>> class from being extended. Is this expected? I couldn't find anything
>> in TFM about this. Sorry if I missed it.
>>
>> Thanks,
>> H.
>>
>> --
>> 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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)

Hervé Pagès-2
Thanks Michael for taking care of this.  H.

On 06/06/2017 11:48 AM, Michael Lawrence wrote:

> I've fixed this and will commit soon.
>
> Disregard my dim<-() example; that behaves as expected (the class needs
> a dim<-() method).
>
> Michael
>
> On Tue, Jun 6, 2017 at 5:16 AM, Michael Lawrence <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Thanks for the report. The issue is that one cannot set special
>     attributes like names, dim, dimnames, etc on S4 objects. I was
>     aready working on this and will have a fix soon.
>
>      > a2 <- new("A2")
>      > dim(a2) <- c(2, 3)
>     Error in dim(a2) <- c(2, 3) : invalid first argument
>
>
>     On Mon, Jun 5, 2017 at 6:08 PM, Hervé Pagès <[hidden email]
>     <mailto:[hidden email]>> wrote:
>
>         Hi,
>
>         It's nice to be able to define S4 classes with slots that correspond
>         to standard attributes:
>
>            setClass("A1", slots=c(names="character"))
>            setClass("A2", slots=c(dim="integer"))
>            setClass("A3", slots=c(dimnames="list"))
>
>         By doing this, one gets a few methods for free:
>
>            a1 <- new("A1", names=letters[1:3])
>            names(a1)     # "a" "b" "c"
>            a2 <- new("A2", dim=4:3)
>            nrow(a2)      # 4
>            a3 <- new("A3", dimnames=list(NULL, letters[1:3]))
>            colnames(a3)  # "a" "b" "c"
>
>         However, when it comes to subclassing, some of these slots cause
>         problems. I can extend A1:
>
>            setClass("B1", contains="A1")
>
>         but trying to extend A2 or A3 produces an error (with a
>         non-informative
>         message in the 1st case and a somewhat obscure one in the 2nd):
>
>            setClass("B2", contains="A2")
>            # Error in attr(prototype, slotName) <- attr(pri, slotName) :
>            #   invalid first argument
>
>            setClass("B3", contains="A3")
>            # Error in attr(prototype, slotName) <- attr(pri, slotName) :
>            #   'dimnames' applied to non-array
>
>         So it seems that the presence of a "dim" or "dimnames" slot
>         prevents a
>         class from being extended. Is this expected? I couldn't find
>         anything
>         in TFM about this. Sorry if I missed it.
>
>         Thanks,
>         H.
>
>         --
>         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=7MsydJAWI1B1wsZHDmsO-mpZ_vfvDpTo-YMHgUXrQKQ&s=dXHseRValxgm4TXgSsjasFRGgqAf46IivoNi4VnRj3o&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
Loading...