rggplot2ggproto

Access parent within own `ggproto` object


I want to access compute_group() of the parent method within my own ggproto object. Here are two different working approaches, but don't know if any is the "correct" way to go:

  1. self$super()$compute_group(...)
  2. ggproto_parent(<parent>, self)$compute_group(...)

Any (strong) opinions?


Solution

  • I'm just here to suggest that the correct way to do it is to use your second approach. One reason is that the documentation of ?ggproto reads:

    To explicitly call a methods in a parent, use ggproto_parent(Parent, self).

    A second reason is that ggplot2 source code never uses self$super()$method(), from which you could deduce that it is not intended to be used that way.

    A third option to consider is to call OtherClass$compute_group() directly in your code. This should work because the ggproto classes are designed to be stateless, so this should be safe if you adhere to the statelessness rule. The ggplot2 book mentions that this pattern in more prevalent than the ggproto_parent() method due to its clarity and equivalent safety.