Rcpp::export Attribute
exportAttribute.RdThe Rcpp::export attribute is added to a C++ function definition to indicate that it should be made available as an R function. The sourceCpp and compileAttributes functions process the Rcpp::export attribute by generating the code required to call the C++ function from R.
Details
Functions marked with the Rcpp::export attribute must meet several conditions to be correctly handled:
Be defined in the global namespace (i.e. not within a C++
namespacedeclaration).Have a return type that is either void or compatible with
Rcpp::wrapand parameter types that are compatible withRcpp::as(see sections 3.1 and 3.2 of the Rcpp-introduction vignette for more details).Use fully qualified type names for the return value and all parameters. However, Rcpp types may appear without the namespace qualifier (i.e.
DataFrameis okay as a type name butstd::stringmust be specified fully).
If default argument values are provided in the C++ function definition then these defaults are also used for the exported R function. For example, the following C++ function:
DataFrame readData(
CharacterVector file,
CharacterVector exclude = CharacterVector::create(),
bool fill = true)
Will be exported to R as:
function (file, exclude = character(0), fill = TRUE)
Note that C++ rules for default arguments still apply: they must occur consecutively at the end of the function signature and unlike R can't rely on the values of other arguments.
Note
When a C++ function has export bindings automatically generated by the compileAttributes function, it can optionally also have a direct C++ interface generated using the Rcpp::interfaces attribute.
o The Rcpp::export attribute is specified using a syntax compatible with the new generalized attributes feature of the C++11 standard. Note however that since this feature is not yet broadly supported by compilers it needs to be specified within a comment (see examples below).