Often we need to have optional arguments in R of Rcpp functions with default values. Sometimes,
the default value for the optional parameters is set to be NULL. Rcpp provides the Nullable <>
to set default value as to be R_NilValue (equivalent of NULL in Rcpp). There have been several
StackOverflow posts on using the
Nullable behavior. As seen from quite a few posts, the key step in using Rcpp::Nullable<> is
to cast it to the underlying type first (i.e., instantiation) after checking that the input is not
NULL.
Nullability of Vector, Matrix or Logical Vector
Running a few examples with setting NULL for a vector, matrix or a boolean value gives us the
expected results.
Numeric Vector is set to not NULL.
1 2
Numeric Matrix is set to not NULL.
-0.500000 -0.500000 -0.500000
-0.500000 -0.500000 -0.500000
-0.500000 -0.500000 -0.500000
Logical Vector is set to not NULL.
0
Warning in nullable1(NULL, NULL, NULL): All arguments are set to NULL.
Warning in nullable1(c(), NULL, NULL): All arguments are set to NULL.
We get the same result when the input to the NumericVector argument is not NULL but an empty
vector, i.e., c(), which is also expected since is.null(c()) is TRUE in R.
A stricter test whether the input is usable can be (aptly named) isUsable().
Nullability of DataFrame and List
Rcpp::Nullable<> works for SEXP based Rcpp types, so Rcpp::DataFrame and Rcpp::List can
also be set to Nullable and instantiated if not NULL.
Testing with Rcpp::List and Rcpp::DataFrame gives expected results, i.e.,
List is not NULL.
List length of 2 elements.
DataFrame is not NULL.
DataFrame of 20 rows and 2 columns.
Nullability of RcppGSL::Matrix
In addition to Rcpp types, RcppGSL::Matrix can also be set with Nullable type (e.g, in the
mvlabundpackage):
e.g.,
Finally, testing with RcppGSL::Matrix which can also be set to Nullable<>, i.e.,
Warning in nullable4(NULL): Input GSL Matrix is NULL.
Input is not NULL.
Input GSL matrix has 3 and 3 columns.
Summary
Rcpp provides a convenient construct to set datatypes to NULL using R_NilValue and application
of the datatype if not set to NULL using the .isNotNull() check. This construct to applied to
set datatypes to NULL as default values and possible simple simplification.
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.