[R] S4 base class
Greetings, Meanwhile I have figured out how do do it only to find out that I have more serious problems. Generally calling Base::f on the base class object is not what you want, instead you want to call Base::f on the full object for the following reasons: If the base class is virtual, then Base::f might use virtual functions (not defined in Base but defined in derived classes). If you then call Base::f on an object of class Base the call will fail. Is it possible in R to call Base::f from within Derived (when there is also Derived::f) on the full object this? I suspect not, which would be a serious drawback to the R class mechanism. Thanks, Michael Meyer __ 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.
[R] S4 base class
Sorry, if the previous message seems without context. Indeed, the first message was bounced by filtering rules (triggered by subject heading than which nothing could be more benign or less liable to suspician). It was: Greetings, I have an S4 class B (Base) which defines a function f=f(this=B,...) Dervided from B we have a derived class D which also defines a function f=f(this=D,...) In the definition of D::f we want to call the version B::f and could do this by simply calling f(baseClassObject(this),...) The question is the following: How do I refer to the base class object from the derived class? Many thanks Michael Meyer __ 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.
Re: [R] S4 base class
On 17/10/2013 9:01 AM, Michael Meyer wrote: Sorry, if the previous message seems without context. Indeed, the first message was bounced by filtering rules (triggered by subject heading than which nothing could be more benign or less liable to suspician). It was: Greetings, I have an S4 class B (Base) which defines a function f=f(this=B,...) Dervided from B we have a derived class D which also defines a function f=f(this=D,...) In the definition of D::f we want to call the version B::f and could do this by simply calling f(baseClassObject(this),...) The question is the following: How do I refer to the base class object from the derived class? You're asking the wrong question. You should be asking how to call the method for the inherited class . callNextMethod() is the answer to that question. By the way, your use of the syntax D::f and B::f suggests that you're thinking from a C++ point of view. That's very likely to lead to frustration: the S4 object system is very different from C++. Methods don't belong to classes, they belong to generics. There is no such thing as D::f or B::f, only f methods with different signatures. Duncan Murdoch __ 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.
[R] S4 base class
Quote By the way, your use of the syntax D::f and B::f suggests that youapos;re thinking from a C++ point of view. Thatapos;s very likely to lead to frustration: the S4 object system is very different from C++. Methods donapos;t belong to classes, they belong to generics. There is no such thing as D::f or B::f, only f methods with different signatures. Duncan Murdoch #---# I am aware of this. We can probably agree that we should use S4 classes and generic functions to duplicate more usual object oriented architecture as far as possible while remaining conscious of the regrettable differences. For example we can pretend we are defining a virtual function in class Base by writing: setGeneric(F, function(this) standardGeneric(F) ) where the code for Base is, even though it has nothing to do with the class Base. We can even use it in other functions defined in class Base by writing setGeneric(G, function(this) standardGeneric(G) ) setMethod(G, signature(this=Base), definition=function(this){ F(this) }) which will work on all derived classes which implement F in some fashion: setMethod(F, signature(this=Derived), definition=function(this){ # do something appropriate for derived. }) With this we can reproduce some semblance of object oriented programming However, apparently we cannot solve in this manner a common problem of object oriented programming (from now on C++ parlance): Suppose you have a base class Base which implements a function Base::F which works in most contexts but not in the context of ComplicatedDerived class where some preparation has to happen before this very same function can be called. You would then define void ComplicatedDerived::F(...){ preparation(); Base::F(); } You can nealry duplicate this in R via setMethod(F, signature(this=ComplicatedDerived), definition=function(this){ preparation(this) F(as(this,Base)) }) but it will fail whenever F uses virtual functions (i.e. generics) which are only defined for derived classes of Base, whereas this is not a problem at all in normal object oriented languages. This is not a contrived problem but is rather basic. I wonder if you can do it in R in some other way. Many thanks, Michael __ 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.
[R] S4 base class
Greetings, I have an S4 class B (Base) which defines a function f=f(this=B,...) Dervided from B we have a derived class D which also defines a function f=f(this=D,...) In the definition of D::f we want to call the version B::f and could do this by simply calling f(baseClassObject(this),...) The question is the following: How do I refer to the base class object from the derived class? Many thanks Michael Meyer __ 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.
Re: [R] S4 base class
On 10/17/2013 08:54 AM, Michael Meyer wrote: Suppose you have a base class Base which implements a function Base::F which works in most contexts but not in the context of ComplicatedDerived class where some preparation has to happen before this very same function can be called. You would then define void ComplicatedDerived::F(...){ preparation(); Base::F(); } You can nealry duplicate this in R via setMethod(F, signature(this=ComplicatedDerived), definition=function(this){ preparation(this) F(as(this,Base)) }) but it will fail whenever F uses virtual functions (i.e. generics) which are only defined for derived classes of Base With .A - setClass(A, representation(a=numeric)) .B - setClass(B, representation(b=numeric), contains=A) setGeneric(f, function(x, ...) standardGeneric(f)) setMethod(f, A, function(x, ...) { message(f,A-method) g(x, ...) # generic with methods only for derived classes }) setMethod(f, B, function(x, ...) { message(f,B-method) callNextMethod(x, ...) # earlier response from Duncan Murdoch }) setGeneric(g, function(x, ...) standardGeneric(g)) setMethod(g, B, function(x, ...) { message(g,B-method) x }) one has f(.B()) f,B-method f,A-method g,B-method An object of class B Slot b: numeric(0) Slot a: numeric(0) ? -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 __ 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.
[R] S4 base class
@Martin Morgan, Duncan Murdoch: OK Thanks. I did not understand the callNextMethod. I will investigate this in detail. This is great! Thanks again, Michael Meyer __ 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.