Hi R Users, I am relatively new to programming in R ? so I apologise if my questions appear ?dumb?. I am using a package that defines a number of S3 classes. I want to create an S3 child class of one of these classes. The parent class has a contractor with many arguments. I have been having difficulty writing the child class contractor. I have been unable to find a good reference in the internet for writing S3 classes. What I have been able to find out so far is that the child class constructor should call the parent class constructor ? which in this case requires passing the argument list with a variable number of arguments (i.e. there are a number of optional arguments) from the child to the parent. So my first question is ? is there an easy way to do this? The match.call function will return a call object .. however the attributes function when used on the call object returns a ?NULL?. My second question is ? can any one recommend a good reference for object oriented programming in R which includes a comprehensive discussion of the S3 class model? Thanks Regards, Kym
The S3 class model isn't really a class model. It's more a way of overloading functions. So it's rather simple, and there's less there than you might be expecting. I always thought the "Object Oriented Programming" chapter of the R language definition manual that ships with R was a very good treatment. Have you tried there? If not, please do so. But surely you know how to search! (e.g. "S3 class system in R") -- there are many good tutorials on the internet. Also search at rseek.org, which is "optimized" for R queries. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Aug 7, 2017 at 5:06 PM, Kym Nitschke <kym.nitschke at icloud.com> wrote:> Hi R Users, > > I am relatively new to programming in R ? so I apologise if my questions appear ?dumb?. > > I am using a package that defines a number of S3 classes. I want to create an S3 child class of one of these classes. The parent class has a contractor with many arguments. I have been having difficulty writing the child class contractor. I have been unable to find a good reference in the internet for writing S3 classes. What I have been able to find out so far is that the child class constructor should call the parent class constructor ? which in this case requires passing the argument list with a variable number of arguments (i.e. there are a number of optional arguments) from the child to the parent. > > So my first question is ? is there an easy way to do this? The match.call function will return a call object .. however the attributes function when used on the call object returns a ?NULL?. > > My second question is ? can any one recommend a good reference for object oriented programming in R which includes a comprehensive discussion of the S3 class model? > > Thanks > > Regards, > > Kym > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
The direct question seemed to be how to pass many optional parameters through, which seems obvious once you know it but the OP might not have seen it yet... f1 <- function( x, y=1, z=2 ) { x*y + z } f2 <- function( x, ... ) { x <- x + 1 f1( x, ... ) } f2( 2, y=3 ) -- Sent from my phone. Please excuse my brevity. On August 7, 2017 7:16:42 PM PDT, Bert Gunter <bgunter.4567 at gmail.com> wrote:>The S3 class model isn't really a class model. It's more a way of >overloading functions. So it's rather simple, and there's less there >than you might be expecting. I always thought the "Object Oriented >Programming" chapter of the R language definition manual that ships >with R was a very good treatment. Have you tried there? If not, please >do so. > >But surely you know how to search! (e.g. "S3 class system in R") -- >there are many good tutorials on the internet. Also search at >rseek.org, which is "optimized" for R queries. > >Cheers, >Bert > > > > >Bert Gunter > >"The trouble with having an open mind is that people keep coming along >and sticking things into it." >-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > >On Mon, Aug 7, 2017 at 5:06 PM, Kym Nitschke <kym.nitschke at icloud.com> >wrote: >> Hi R Users, >> >> I am relatively new to programming in R ? so I apologise if my >questions appear ?dumb?. >> >> I am using a package that defines a number of S3 classes. I want to >create an S3 child class of one of these classes. The parent class has >a contractor with many arguments. I have been having difficulty writing >the child class contractor. I have been unable to find a good reference >in the internet for writing S3 classes. What I have been able to find >out so far is that the child class constructor should call the parent >class constructor ? which in this case requires passing the argument >list with a variable number of arguments (i.e. there are a number of >optional arguments) from the child to the parent. >> >> So my first question is ? is there an easy way to do this? The >match.call function will return a call object .. however the attributes >function when used on the call object returns a ?NULL?. >> >> My second question is ? can any one recommend a good reference for >object oriented programming in R which includes a comprehensive >discussion of the S3 class model? >> >> Thanks >> >> Regards, >> >> Kym >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
You might find http://adv-r.hadley.nz/s3.html to be helpful (in particular, http://adv-r.hadley.nz/s3.html#constructors-1, gives my advice about subclass constructors) Hadley On Mon, Aug 7, 2017 at 7:06 PM, Kym Nitschke <kym.nitschke at icloud.com> wrote:> Hi R Users, > > I am relatively new to programming in R ? so I apologise if my questions appear ?dumb?. > > I am using a package that defines a number of S3 classes. I want to create an S3 child class of one of these classes. The parent class has a contractor with many arguments. I have been having difficulty writing the child class contractor. I have been unable to find a good reference in the internet for writing S3 classes. What I have been able to find out so far is that the child class constructor should call the parent class constructor ? which in this case requires passing the argument list with a variable number of arguments (i.e. there are a number of optional arguments) from the child to the parent. > > So my first question is ? is there an easy way to do this? The match.call function will return a call object .. however the attributes function when used on the call object returns a ?NULL?. > > My second question is ? can any one recommend a good reference for object oriented programming in R which includes a comprehensive discussion of the S3 class model? > > Thanks > > Regards, > > Kym > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- http://hadley.nz
Thanks to everyone who replied to my query. It is much appreciated. Reading Hadley Wickham's book Advanced R and in particular S3 classes reveals that a class needs to be written allowing inhertinance (by passing the subclass parameter as an argument) and its methods written carefully to function properly with child class. The S3 objects in the package I am using do not appear to have been written with inheritance in mind. I will look for another (less elegant) solution. Thanks again, Kym> On 9 Aug 2017, at 8:48 am, Hadley Wickham <h.wickham at gmail.com> wrote: > > You might find http://adv-r.hadley.nz/s3.html to be helpful (in > particular, http://adv-r.hadley.nz/s3.html#constructors-1, gives my > advice about subclass constructors) > > Hadley > > On Mon, Aug 7, 2017 at 7:06 PM, Kym Nitschke <kym.nitschke at icloud.com> wrote: >> Hi R Users, >> >> I am relatively new to programming in R ? so I apologise if my questions appear ?dumb?. >> >> I am using a package that defines a number of S3 classes. I want to create an S3 child class of one of these classes. The parent class has a contractor with many arguments. I have been having difficulty writing the child class contractor. I have been unable to find a good reference in the internet for writing S3 classes. What I have been able to find out so far is that the child class constructor should call the parent class constructor ? which in this case requires passing the argument list with a variable number of arguments (i.e. there are a number of optional arguments) from the child to the parent. >> >> So my first question is ? is there an easy way to do this? The match.call function will return a call object .. however the attributes function when used on the call object returns a ?NULL?. >> >> My second question is ? can any one recommend a good reference for object oriented programming in R which includes a comprehensive discussion of the S3 class model? >> >> Thanks >> >> Regards, >> >> Kym >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > > > -- > http://hadley.nz