2

I am trying to override a reference class method. Because reference class methods are bound to the class rather than the object, I believe in order to do this I need to define a new reference class which inherits from the old reference class. However the class I am trying to inherit from is defined in an external package to mine (dplyr). I cannot figure out the correct syntax to do this, contains seems to require only a text name, and does not search for class definitions in external packages.

In particular I am trying to inherit from the DbDisconnector reference class from dplyr and override the finalize method.

This correctly finds the parent class, but then cannot assign to it as it is from a different package.

NewDbDisconnector <- setRefClass("NewDbDisconnector",
  contains = 'DbDisconnector',
  methods = list(
    finalize = function() {
      message("test")
    }
  ),
  where=getNamespace('dplyr')
)
# Error in assign(mname, def, where) (from file.r#75) :
#  cannot add bindings to a locked environment

Contains methods only accept strings, they cannot just be given a refClass definition from getRefClass.

NewDbDisconnector <- setRefClass("NewDbDisconnector",
  contains = getRefClass("DbDisconnector", getNamespace("dplyr")),
  methods = list(
    finalize = function() {
      message("test")
    }
  )
)
# Error in FUN(X[[1L]], ...) :
#  the 'contains' argument should be the names of superclasses:  got an element of class “name”

I would think this should be possible, I just cannot figure out the correct way to do it.

Jim
  • 4,687
  • 29
  • 30
  • 1
    See this [discussion](https://github.com/hadley/httr/issues/113). I think this might be a limitation of reference classes. – Ramnath Jul 30 '14 at 14:46
  • I don't think it's possible because of the way reference classes. For this particular class, I think it's simple enough that you can have your own internal copy. – hadley Jul 30 '14 at 18:23
  • Thanks guys, that is unfortunate. As @hadley said for this particular case the class is simple enough I can just copy it. – Jim Jul 31 '14 at 17:48

1 Answers1

1

You can import the superclass to your environment:

DbDisconnector <- getFromNamespace("DbDisconnector", "dplyr")

And then set contains = "DbDisconnector" in your class.

Jussi Jousimo
  • 488
  • 1
  • 5
  • 11