1

My question is how to apply the generic function plot() using my new method for my inherited S4 class.

I extend the existing S4 class (whose name is stanfit) in rstan, say lowerS4class, and define a new method for generic function plot for the extended (inherited) S4 class, say upperS4class.

But the following undesired selection (i.e. the selected method is the method for the lower S4 class) occur with the following Note in the R console when I apply the generic function plot() for the extended S4 class object x as plot(x).

Note: method with signature "lowerS4class#missing" chosen for function ‘plot’,
 target signature "upperS4clss#missing".
 "upperS4clss#ANY" would also be valid

Edit for comment-----------------------------------------

Inheritance

  setMethod("plot",
           signature(x = "upperS4class"),
           definition = function(x){   
             foo(x)
             }

           )

where foo is a function defined for upperS4class.

Def. for upperS4class

upperS4class <- setClass(
                  #Name for upper class
                  "upperS4class",

                   # Inheritance
                   contains = "lowerS4class"

                   # New slots
                   representation(

                      .....
                   ),

                   # Initial values for new slots
                   prototype(
                      .....


                      ),

)

---Edit for comments------------------

By changing the above as follows, it goes well. Thank you for @JDL.

 setMethod("plot",
           signature(x = "stanfitExtended",
                     y = "missing"       # <--------------What's New !!
                     ),
           definition = function(x){ 
             foo(x)
             }

       )
Camford Oxbridge
  • 834
  • 8
  • 21
  • 1
    Could you post your method definition (or a skeleton of it)? Is the method defined for signature `x=upperS4class,y=missing` or just `x=upperS4class`? (I am also assuming your class definition starts with `setClass("upperS4class",contains="lowerS4class",...) -> upperS4class`) – JDL Jan 04 '19 at 09:00
  • Thank you for reply. I am not sure your code `y=missing`. Is it needed ? – Camford Oxbridge Jan 07 '19 at 00:08
  • 1
    I think it might be — try it and see if it solves your problem. – JDL Jan 07 '19 at 08:40
  • Thank you for @JDL. More precisely `y="missing"` So it helps me very much and learn alot. Thank you !! – Camford Oxbridge Jan 07 '19 at 08:57
  • 1
    yes, the method definition should start `setMethod("plot",signature(x="upperS4class",y="missing"),...)` . The reason this helps is because then both arguments in the signature match *exactly*, which is what R tries to prioritise with method selection. – JDL Jan 07 '19 at 09:17

0 Answers0