[R] R.oo data members / inheritance

Henrik Bengtsson hb at biostat.ucsf.edu
Mon Aug 29 18:36:47 CEST 2011


Hi,

comments below.

On Mon, Aug 29, 2011 at 8:12 AM, Ben qant <ccquant at gmail.com> wrote:
> Correction. My solution didn't work either.... Didn't return the correct
> values. Can you post an example that takes three arguments? I'm working on
> how to do this now.
> thanks...sorry. I"m new to R and R.oo.
>
> Ben
>
> On Mon, Aug 29, 2011 at 8:35 AM, Ben qant <ccquant at gmail.com> wrote:
>
>> Henrik,
>>
>> Your last suggestion did not work for me. It seems like it does not allow
>> me to create a ClassB object with 3 arguments:
>>
>>
>> > setConstructorS3("ClassA", function(A=15, x=NA) {
>> +   extend(Object(), "ClassA",
>> +    .size = A,
>> +    .x=x
>> +  )
>> + })
>> > setConstructorS3("ClassB", function(..., bData=NA) {
>> +   extend(ClassA(...), "ClassB",
>> +     .bData = bData
>> +   )
>> + })
>> > b = ClassB(1,2,3)
>> Error in ClassA(...) : unused argument(s) (3)

I should have clarified that when putting '...' (= "all arguments that
does not match other arguments") at the beginning like this, you have
to specify the arguments that you do not want to pass via '...' by
name, i.e.

b <- ClassB(1,2, bData=3)

I'd recommend to always name you argument, especially for a piece of
code that is not just a one-time call at the R prompt, i.e.

b <- ClassB(A=1, x=2, bData=3);

>>
>> I got around it using your 'specific' suggestion:
>>
>>
>> > setConstructorS3("ClassA", function(A=15, x=NA) {
>> +   extend(Object(), "ClassA",
>> +    .size = A,
>> +    .x=x
>> +  )
>> + })
>> >
>> > setConstructorS3("ClassB", function(..., bData=NA) {
>> +   extend(ClassA(A=15,x=NA), "ClassB",
>> +     .bData = bData
>> +   )
>> + })

That doesn't work, because arguments other than 'bData' that you pass
to ClassB() will end up in '...', and that you don't pass along to
ClassA(), i.e. such arguments are simply ignored.  So, a solution that
use neither '...' nor missing() is:

setConstructorS3("ClassB", function(A=15, x=NA, bData=NA) {
  extend(ClassA(A=A,x=x), "ClassB",
   .bData = bData
  )
})

This code is very explicit (hence more readable).  The downside is
that if you change the default arguments in ClassA() and you wish
those to also be in ClassB(), you have to update the defaults in
ClassB() manually.  With '...' you don't have to do that.

Finally, not that the above about '...', argument matching etc is
generic to R - it is not specific to R.oo.  You can find more about
the '...' argument(s) in 'An Introduction to R', which you find via
help.start().

Hope this helps

Henrik

>> > b = ClassB(1,2,3)
>> >
>>
>>
>>
>
>        [[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.
>



More information about the R-help mailing list