Question

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.

Was it helpful?

Solution

You can import the superclass to your environment:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top