3

I am trying to create a class in R6 which inherits a function from its parent class. However, that function relies on other 'helper' functions which are overwritten in the child class.

When I call the parent function, I want it to use the helper functions from the parent class, not the overridden ones from the child class.

Here is a mock-up of how my code works:

ParentClass <- R6Class(
  public = list(
    helper_fn = function() { print("Parent helper.") }, 
    main_fn = function() { self$helper_fn() } 
    }
  )
)

ChildClass <- R6Class(
  inherit = ParentClass,
  public = list(
    helper_fn = function() { print("Child helper.") },
    main_fn = function() { super$main_fn() }
  )
)

The intended behaviour is to print "Parent helper." but when the child_class calls the parent's main_fn, it uses the overridden function from the child_class.

child_class <- ChildClass$new()
child_class$main_fn()
# prints "Parent helper."

Is it possible to avoid this? Or is this just how overriding a function works?

ClownsENT
  • 155
  • 1
  • 8

1 Answers1

0

I may not be able to help with a solution, but it looks like since the parent's main_fn calls a function on self - and R directs this into the child's instance instead of the parent's.

You may try to inherit helper_fn from the parent and name the child's helper differently like childHelper_fn if that works for what you are trying to do.