Description Usage Arguments Details Calling methods Calling methods in a parent Examples
Construct a new object with ggproto
, test with is.proto
,
and access parent methods/fields with ggproto_parent
.
1 2 3 4 5 | ggproto(`_class` = NULL, `_inherit` = NULL, ...)
ggproto_parent(parent, self)
is.ggproto(x)
|
_class |
Class name to assign to the object. This is stored as the class
attribute of the object. This is optional: if |
_inherit |
ggproto object to inherit from. If |
... |
A list of members in the ggproto object. |
parent, self |
Access parent class |
x |
An object to test. |
ggproto implements a protype based OO system which blurs the lines between classes and instances. It is inspired by the proto package, but it has some important differences. Notably, it cleanly supports cross-package inheritance, and has faster performance.
In most cases, creating a new OO system to be used by a single package is not a good idea. However, it was the least-bad solution for ggplot2 because it required the fewest changes to an already complex code base.
ggproto methods can take an optional self
argument: if it is present,
it is a regular method; if it's absent, it's a "static" method (i.e. it
doesn't use any fields).
Imagine you have a ggproto object Adder
, which has a
method addx = function(self, n) n + self$x
. Then, to call this
function, you would use Adder$addx(10)
– the self
is passed
in automatically by the wrapper function. self
be located anywhere
in the function signature, although customarily it comes first.
To explicitly call a methods in a parent, use
ggproto_parent(Parent, self)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.