Hervé Pagès
2017-Jun-06 01:08 UTC
[Rd] surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)
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: hpages at fredhutch.org Phone: (206) 667-5791 Fax: (206) 667-1319
Michael Lawrence
2017-Jun-06 12:16 UTC
[Rd] surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)
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 <hpages at fredhutch.org> 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: hpages at fredhutch.org > Phone: (206) 667-5791 > Fax: (206) 667-1319 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
Michael Lawrence
2017-Jun-06 18:48 UTC
[Rd] surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)
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 <michafla at gene.com> 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 <hpages at fredhutch.org> 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: hpages at fredhutch.org >> Phone: (206) 667-5791 >> Fax: (206) 667-1319 >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > >[[alternative HTML version deleted]]
Maybe Matching Threads
- surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)
- surprisingly, S4 classes with a "dim" or "dimnames" slot are final (in the Java sense)
- slot(obj, "nosuch") documentation question
- unary class union of an S3 class
- as.list method for by Objects