David Quesada — written Jul 31, 2020 — source
When we are using R6 objects and want to introduce some C++ code in our project, we may also want to interact with these objects using Rcpp. With this objective in mind, the key to interacting with R6 objects is that they are essentially environments, and so they can be treated as such.
In this example, we will define a ‘Person’ class with some private attributes and public methods. It will allow us to show how to create R6 objects in C++ and how to interact with them.
First, we define the class ‘Person’, with a name, an id, an item and some methods:
With this simple class, we will be able to create R6 objects, initialize them and call some of their methods.
To create R6 objects, we must first get the ‘new’ function that initializes them. In this case, given that we have loaded the Person class into the global environment, we will get it from there:
[[1]] <Person> Public: clone: function (deep = FALSE) get_id: function () get_name: function () give_item: function (item) good_foo: function () initialize: function (name, id) Private: bad_foo: function () id: 1 item: NULL name: Jake [[2]] <Person> Public: clone: function (deep = FALSE) get_id: function () get_name: function () give_item: function (item) good_foo: function () initialize: function (name, id) Private: bad_foo: function () id: 2 item: NULL name: Anne [[3]] <Person> Public: clone: function (deep = FALSE) get_id: function () get_name: function () give_item: function (item) good_foo: function () initialize: function (name, id) Private: bad_foo: function () id: 3 item: NULL name: Alex
The previous example initializes a list with Persons. It is important to notice that the only function relevant to us from the global environment is the ‘new’ function created by the R6 class. All the functions defined inside Person will be contained in each instance of the class.
If the R6 object is defined inside some package, included our own package if we are developing one, we must get it from there. To do this, the previous function has to be modified as follows:
Once we have instantiated an R6 object as an environment, we can call its methods after getting them from the instance:
<Person> Public: clone: function (deep = FALSE) get_id: function () get_name: function () give_item: function (item) good_foo: function () initialize: function (name, id) Private: bad_foo: function () id: 1 item: shovel name: Jake
Note that if we are creating multiple instances, we have to get the desired method from each one of them.
Private attributes and methods cannot be accessed in this manner, trying to do so results in an error:
[1] "Exception raised: Error in access_method(\"bad_foo\"): Cannot convert object to a function: [type=NULL; target=CLOSXP, SPECIALSXP, or BUILTINSXP].\n"
[1] "Wrapped inside: {This is a private function}"
The error is telling us that there is no function called “bad_foo” in the environment of the object “new_p”. In that environment, only public attributes and methods are present on the surface. Theoretically, we could still try to find the private attributes and methods inside the object, but it goes against the paradigm and it would be much easier to just make the method we want to call public.
Passing R6 objects as arguments to C++ functions is also straight forward by treating them as environments:
<Person> Public: clone: function (deep = FALSE) get_id: function () get_name: function () give_item: function (item) good_foo: function () initialize: function (name, id) Private: bad_foo: function () id: 1 item: shovel name: Jake
Note that there is no need to return the object, as the environment is passed by reference and changes inside it will be reflected outside the function.
Tweet