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.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.