Learn R Programming

Rcpp (version 0.7.7)

RcppResultSet: C++ class for sending C++ objects back to R

Description

RcppResultSet is a C++ class defined in Rcpp.h that can assign any number of C++ objects to Rin 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 Rtype SEXP. Where applicable, the C++ types are automatically converted to the corresponding Rtypes structures around types numeric, integer, or character. The C++ code can all be retrieved in Ras elements of a named list object.

Arguments

Details

Usage of RcppResultSet from C++ is fully defined in Rcpp.h. An example for returning data to Rat 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 or rs.add("stlmat", stlmat); // vector< vector > // or 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 rs.add("InputDF", inframe); // RcppFrame rs.add("PreDF", frame); // RcppFrame

rl = rs.getReturnList(); return(rl);

As the Rlevel, 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 Rlevel, 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.

See Also

RcppExample, the vignette RcppAPI.

Examples

Run this code
# 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

Run the code above in your browser using DataLab