Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Introduction
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.
Creating a dummy R6 class
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.
Creating R6 objects in C++
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:
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:
Calling the methods of an instance
Once we have instantiated an R6 object as an environment, we can call its methods after getting them from the instance:
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:
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 down R6 objects
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.