2

Example: I have a MatrixPair class and a NamedMatrixPair (inherits from MatrixPair). MatrixPair$show() prints the two matrices in it, and I want NamedMatrixPair$show() to print some extra info but then also append whatever MatrixPair$show() would print.

Obviously this is a toy example; I know you can name columns in matrices etc., but that's not the point of my question.

I'm super-new to R. I'm using reference classes for this.

This is a super-common thing in most languages I know (e.g., super.show() or base.show() or whatever). I don't know if that's even possible in R though.

MatrixPair <- setRefClass(
  'MatrixPair',

  fields = list(
    m1 = 'matrix', 
    m2 = 'matrix'),

  methods = list(

    initialize = function(...) {
      callSuper(...)
    },

    showX  = function() {
      cat(sprintf("2 matrices, with dimensions %s x %s:\n", nrow(m1), ncol(m1)))
      methods::show(m1)
      methods::show(m2)
    },
  )
)

NamedMatrixPair <- setRefClass(
  'NamedMatrixPair',

  contains = 'MatrixPair',

  fields = list(
    'rowName' = 'character',
    'colName' = 'character'
  ),

  methods = list(

    initialize = function(m1, m2, ...) {
      callSuper(m1 = m1, m2 = m2, ...)
    },

    show = function() {
      cat(sprintf("rows: %s ; cols: %s", rowName, colName))
      # I want to call show() for parent class MatrixPair.
      # methods::show(.self) won't do it
      # show(as(MatrixPair, .self)) - i.e. casting the derived class object to the parent class - won't do it
    }        
  )      
)

EDIT: I figured out the solution.

show = function() {
  cat(sprintf("rows: %s ; cols: %s", rowName, colName))
  callSuper()
}
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Anna
  • 21
  • 4
  • Hi Anna and welcome to stackoverflow! If you provide a reproducible example it would increase the chances of your question getting answered greatly. Here is good guide on how to do so: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – cgage1 Mar 26 '16 at 00:05
  • That's not typical R usage. The '$' operator is not usually going to pass a function to an object. The syntax is not the same as other OOPs. It can be done but it requires some setup that you have not yet demonstrated. – IRTFM Mar 26 '16 at 00:44
  • thank you for your responses. I just added some code. I'm sure my syntax is incorrect (I've spent all of 2-3 days learning R from scratch), but my main point is about whether a parent method is accessible to a derived class if it overrides it. – Anna Mar 26 '16 at 04:16
  • I found the answer on Hadley Wickham's "Advanced R" book (not that this item is really advanced). callSuper does it. I thought it only applies to initialize, but you can use it anywhere. By the way, I fail to see why this question was marked 'off-topic'; if someone can clarify that for me, I'd appreciate it. This was not seeking debugging help as per the stated reason for having this 'off topic'. I was asking a general OO question which does not need code to back it up. – Anna Mar 27 '16 at 06:43
  • I have the same problem, I have defined the show method in the parent class, but when I use show with an instance of the child class it does look to the parent method definition. I am trying to use your solution, but I am afraid the method: `callSupper` does not exist in R, instead calling `callNextMethod()` works. – David Leal Feb 15 '17 at 23:31

0 Answers0