| RcppResultSet {Rcpp} | R Documentation |
RcppResultSet is a C++ class defined in Rcpp.h that can
assign any number of C++ objects to R in a single named list object
as the SEXP return value of a .Call() function call.
The C++ objects can be of different types that are limited to
types double, int, string, vectors of
double or int (with explicit dimensions),
matrices of double or int (with explicit dimensions),
STL vectors of double, int or string, STL
‘vector of vectors’ of types double or int (all
with implicit dimensions), the internal types RcppDate, RcppDateVector,
RcppStringVector, RcppVector of types double or
int, RcppMatrix of types double or int
as well RcppFrame, a type that can be converted into a
data.frame, and the R type SEXP.
Where applicable, the C++ types are automatically converted to the
corresponding R types structures around types numeric,
integer, or character. The C++ code can all be
retrieved in R as elements of a named list object.
Usage of RcppResultSet from C++ is fully defined in
Rcpp.h. An example for returning data to R at the end of a
.Call() call follows.
At the C++ level, the corresponding code to assign these parameter to
C++ objects is can be as follows (taken from the C++ source of
RcppExample):
SEXP rl;
RcppResultSet rs;
rs.add("date", aDate); // RcppDate
rs.add("dateVec", dateVec); // RcppDateVec
rs.add("method", method); // string
rs.add("tolerance", tol); // numeric
rs.add("maxIter", maxIter); // int
rs.add("matD", matD); // RcppMatrix
rs.add("stlvec", stlvec); // vector<double> or <int>
rs.add("stlmat", stlmat); // vector< vector <double> >
// or <int>
rs.add("a", a, nrows, ncols); // double** (or int**) with
// two dimension
rs.add("v", v, len); // double* (or int*) with
// one dimension
rs.add("stringVec", strVec); // RcppStringVector
rs.add("strings", svec); // vector<string>
rs.add("InputDF", inframe); // RcppFrame
rs.add("PreDF", frame); // RcppFrame
rl = rs.getReturnList();
return(rl);
As the R level, we assign the returned object a list variables from which we select each list element by its name. lookup is driven by the names givem at the R level, order is not important. It is however important that the types match. Errors are typically caught and an exception is thrown.
The class member function checkNames can be used to verify that the
SEXP object passed to the function contains a given set of
named object.
Dominick Samperi wrote most of Rcpp during 2005 and 2006. Dirk Eddelbuettel made some additions, and became maintainer in 2008.
RcppExample, the vignette “RcppAPI”.
# example from RcppDate # set up date and datetime vectors dvec <- Sys.Date() + -2:2 dtvec <- Sys.time() + (-2:2)*0.5 # call the underlying C++ function result <- RcppDateExample(dvec, dtvec) # inspect returned object result