Janko Thyson
2011-Jun-03 13:53 UTC
[Rd] Bug or feature: using "ANY" as a generic field class (was: '[R] Is there a (virtual) class that all R objects inherit from?)
Dear list, I was wondering if you could help me out in clarifying something: Is it possible to use class "ANY" in slots/fields of formal classes if you a) do not want to restrict valid classes of that field and b) if you are making explicit use of class inheritance? It seems to work in simple scenarios but produces errors when class inheritance comes into play. So I was wondering if that's a feature or a bug. If using "ANY" is not the right way, I'd appreciate a pointer to how you can to this. See previous post with an example below. Regards, Janko On 06/03/2011 01:53 AM, Janko Thyson wrote:> On 31.05.2011 18:17, Martin Morgan wrote: >> On 05/30/2011 07:02 AM, Janko Thyson wrote: >>> Dear list, >>> >>> I would like to set one specific Reference Class field to be of an >>> arbitrary class. Is there a class that all R objects inherit from? I >>> thought that "ANY" was something like this, but obviously that's not >>> true: >>> >>> > inherits(1:3, "ANY") >>> [1] FALSE >> >> I can't speak to the implementation, but ANY functions as a base class >> in terms of slot / field assignment and inheritance, e.g., >> >> setClass("A", representation(x="ANY")) >> new("A", x=1:3) >> >> Martin > > Hi Martin, > > sorry for the late response. The way you do it works. Yet, when you > declare dependencies more explicitly (contains=XY), then R complains. Is > this a feature or a bug (with respect to the "less explicit" way working > just fine)? See the example below: > > # S4 > setClass("A", representation(x="ANY")) > new("A", x=1:3) > > setClass("A", representation(x="ANY")) > setClass("B", contains="A", representation(x="character")) > new("B", x=1:3) > > # Reference Classes > setRefClass( > Class="A", > fields=list( > .PRIMARYDATA="ANY" > ), > contains=c("VIRTUAL") > ) > B <- setRefClass( > Class="B", > fields=list( > .PRIMARYDATA="character" > ), > contains=c("A") > )Bug, I'd say. Martin> > Regards, > Janko >>> >>> Regards, >>> Janko >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >>-- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793 [[alternative HTML version deleted]]
John Chambers
2011-Jun-03 17:13 UTC
[Rd] Bug or feature: using "ANY" as a generic field class (was: '[R] Is there a (virtual) class that all R objects inherit from?)
Well, your mail is unclear as to what you expected, but there is one bug shown by your example. The behavior of S4 classes is sensible, at least as far as the example shows: > setClass("A", representation(x="ANY")) [1] "A" > setClass("B", contains="A", representation(x="character")) [1] "B" > new("B", x=1:3) Error in validObject(.Object) : invalid class "B" object: invalid object for slot "x" in class "B": got class "integer", should be or extend class "character" You couldn't expect the new() call to work, as the error message clearly explains. A legitimate call does work: > new("B", x = "abc") An object of class "B" Slot "x": [1] "abc" The reference classes should work the same way, but don't, as your example shows. A <- setRefClass( + Class="A", + fields=list( + .PRIMARYDATA="ANY" + ), + contains=c("VIRTUAL") + ) > B <- setRefClass( + Class="B", + fields=list( + .PRIMARYDATA="character" + ), + contains=c("A") + ) Error in `insertFields<-`(`*tmp*`, value = "character") : The overriding class("character") of field ".PRIMARYDATA" is not a subclass of the existing field definition ("ANY") We'll fix that. And, yes, "ANY" is intended as a universal superclass, but is usually not mentioned explicitly. On 6/3/11 6:53 AM, Janko Thyson wrote:> Dear list, > > I was wondering if you could help me out in clarifying something: > Is it possible to use class "ANY" in slots/fields of formal classes if you > a) do not want to restrict valid classes of that field and > b) if you are making explicit use of class inheritance? > > It seems to work in simple scenarios but produces errors when class > inheritance comes into play. So I was wondering if that's a feature or a > bug. > > If using "ANY" is not the right way, I'd appreciate a pointer to how you > can to this. > > See previous post with an example below. > > Regards, > Janko > > On 06/03/2011 01:53 AM, Janko Thyson wrote: >> On 31.05.2011 18:17, Martin Morgan wrote: >>> On 05/30/2011 07:02 AM, Janko Thyson wrote: >>>> Dear list, >>>> >>>> I would like to set one specific Reference Class field to be of an >>>> arbitrary class. Is there a class that all R objects inherit from? I >>>> thought that "ANY" was something like this, but obviously that's not >>>> true: >>>> >>>>> inherits(1:3, "ANY") >>>> [1] FALSE >>> >>> I can't speak to the implementation, but ANY functions as a base class >>> in terms of slot / field assignment and inheritance, e.g., >>> >>> setClass("A", representation(x="ANY")) >>> new("A", x=1:3) >>> >>> Martin >> >> Hi Martin, >> >> sorry for the late response. The way you do it works. Yet, when you >> declare dependencies more explicitly (contains=XY), then R complains. Is >> this a feature or a bug (with respect to the "less explicit" way working >> just fine)? See the example below: >> >> # S4 >> setClass("A", representation(x="ANY")) >> new("A", x=1:3) >> >> setClass("A", representation(x="ANY")) >> setClass("B", contains="A", representation(x="character")) >> new("B", x=1:3) >> >> # Reference Classes >> setRefClass( >> Class="A", >> fields=list( >> .PRIMARYDATA="ANY" >> ), >> contains=c("VIRTUAL") >> ) >> B<- setRefClass( >> Class="B", >> fields=list( >> .PRIMARYDATA="character" >> ), >> contains=c("A") >> ) > > Bug, I'd say. Martin > >> >> Regards, >> Janko >>>> >>>> Regards, >>>> Janko >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> ______________________________________________ >>>> R-help at r-project.org mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html >>>> and provide commented, minimal, self-contained, reproducible code. >>> >>> > >
John Chambers
2011-Jun-06 16:29 UTC
[Rd] Bug or feature: using "ANY" as a generic field class (was: '[R] Is there a (virtual) class that all R objects inherit from?)
Should now behave as expected in r-devel and 2.13 patched, as of SVN 56045, June 4. (noted in the NEWS file.) On 6/6/11 6:27 AM, Janko Thyson wrote:> Thanks a lot for your reply and I'm sorry if I didn't make it quite > clear what I expected, but you got it right: > > I'd simply like to see the same behavior for Reference Classes as for S4 > classes when extending classes with "ANY" fields as featured in the > example below. > > > setClass("A", representation(x="ANY")) > [1] "A" > > setClass("B", contains="A", representation(x="character")) > [1] "B" > > new("B", x = "abc") > An object of class "B" > Slot "x": > [1] "abc" > > Thanks for addressing this! > > Regards, > Janko > > On 03.06.2011 19:13, John Chambers wrote: >> Well, your mail is unclear as to what you expected, but there is one >> bug shown by your example. >> >> The behavior of S4 classes is sensible, at least as far as the example >> shows: >> >> >> > setClass("A", representation(x="ANY")) >> [1] "A" >> > setClass("B", contains="A", representation(x="character")) >> [1] "B" >> > new("B", x=1:3) >> Error in validObject(.Object) : >> invalid class "B" object: invalid object for slot "x" in class "B": >> got class "integer", should be or extend class "character" >> >> You couldn't expect the new() call to work, as the error message >> clearly explains. A legitimate call does work: >> >> > new("B", x = "abc") >> An object of class "B" >> Slot "x": >> [1] "abc" >> >> The reference classes should work the same way, but don't, as your >> example shows. >> >> A <- setRefClass( >> + Class="A", >> + fields=list( >> + .PRIMARYDATA="ANY" >> + ), >> + contains=c("VIRTUAL") >> + ) >> > B <- setRefClass( >> + Class="B", >> + fields=list( >> + .PRIMARYDATA="character" >> + ), >> + contains=c("A") >> + ) >> Error in `insertFields<-`(`*tmp*`, value = "character") : >> The overriding class("character") of field ".PRIMARYDATA" is not a >> subclass of the existing field definition ("ANY") >> >> We'll fix that. And, yes, "ANY" is intended as a universal superclass, >> but is usually not mentioned explicitly. >> >> >> On 6/3/11 6:53 AM, Janko Thyson wrote: >>> Dear list, >>> >>> I was wondering if you could help me out in clarifying something: >>> Is it possible to use class "ANY" in slots/fields of formal classes >>> if you >>> a) do not want to restrict valid classes of that field and >>> b) if you are making explicit use of class inheritance? >>> >>> It seems to work in simple scenarios but produces errors when class >>> inheritance comes into play. So I was wondering if that's a feature or a >>> bug. >>> >>> If using "ANY" is not the right way, I'd appreciate a pointer to how you >>> can to this. >>> >>> See previous post with an example below. >>> >>> Regards, >>> Janko >>> >>> On 06/03/2011 01:53 AM, Janko Thyson wrote: >>>> On 31.05.2011 18:17, Martin Morgan wrote: >>>>> On 05/30/2011 07:02 AM, Janko Thyson wrote: >>>>>> Dear list, >>>>>> >>>>>> I would like to set one specific Reference Class field to be of an >>>>>> arbitrary class. Is there a class that all R objects inherit from? I >>>>>> thought that "ANY" was something like this, but obviously that's not >>>>>> true: >>>>>> >>>>>>> inherits(1:3, "ANY") >>>>>> [1] FALSE >>>>> >>>>> I can't speak to the implementation, but ANY functions as a base class >>>>> in terms of slot / field assignment and inheritance, e.g., >>>>> >>>>> setClass("A", representation(x="ANY")) >>>>> new("A", x=1:3) >>>>> >>>>> Martin >>>> >>>> Hi Martin, >>>> >>>> sorry for the late response. The way you do it works. Yet, when you >>>> declare dependencies more explicitly (contains=XY), then R >>>> complains. Is >>>> this a feature or a bug (with respect to the "less explicit" way >>>> working >>>> just fine)? See the example below: >>>> >>>> # S4 >>>> setClass("A", representation(x="ANY")) >>>> new("A", x=1:3) >>>> >>>> setClass("A", representation(x="ANY")) >>>> setClass("B", contains="A", representation(x="character")) >>>> new("B", x=1:3) >>>> >>>> # Reference Classes >>>> setRefClass( >>>> Class="A", >>>> fields=list( >>>> .PRIMARYDATA="ANY" >>>> ), >>>> contains=c("VIRTUAL") >>>> ) >>>> B<- setRefClass( >>>> Class="B", >>>> fields=list( >>>> .PRIMARYDATA="character" >>>> ), >>>> contains=c("A") >>>> ) >>> >>> Bug, I'd say. Martin >>> >>>> >>>> Regards, >>>> Janko >>>>>> >>>>>> Regards, >>>>>> Janko >>>>>> >>>>>> [[alternative HTML version deleted]] >>>>>> >>>>>> ______________________________________________ >>>>>> R-help at r-project.org mailing list >>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html >>>>>> and provide commented, minimal, self-contained, reproducible code. >>>>> >>>>> >>> >>> >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > > -- > ------------------------------------------------------------------------ > > *Janko Thyson* > janko.thyson at ku-eichstaett.de <mailto:janko.thyson at ku-eichstaett.de> > > Catholic University of Eichst?tt-Ingolstadt > Ingolstadt School of Management > Statistics and Quantitative Methods > Auf der Schanz 49 > D-85049 Ingolstadt > > www.wfi.edu/lsqm <http://www.wfi.edu/lsqm> > > Fon: +49 841 937-1923 > Fax: +49 841 937-1965 > > This e-mail and any attachment is for authorized use by the intended > recipient(s) only. It may contain proprietary material, confidential > information and/or be subject to legal privilege. It should not be > copied, disclosed to, retained or used by any other party. > If you are not an intended recipient then please promptly delete this > e-mail and any attachment and all copies and inform the sender. >
Apparently Analagous Threads
- Problems building own package (Error: "package has been build before R-2.10.0")
- Problems building own package (Error: "package has been build before R-2.10.0")
- Function "nsl()" missing in package utils
- reference classes: question on inheritance
- Can an object reference itself?