RcppVector, RcppMatrix and RcppStringVector are
C++ classes that can pass vectors (matrices) of Robjects of
appropriate types to C++ via the .Call() function interface.
They are part of the 'classic' Rcpp API.
The vector and matrix types are templated and can operate on Rtypes
intger and numeric. The RcppVectorView and RcppMatrixView are slighly more
lightweight read-only variants.
Member functions are provided to query the dimension of the vector or
matrix object, convert it in a corresponding C representation,
and also to convert it into a corresponding STL object.
The new API has classes NumericVector, NumericMatrix,
CharacterVector (and also an alias StringVector).
The files RcppVectorExample.cpp and
RcppMatrixExample.cpp provide examples for both the classic and
new APIs.
Note that the
RcppVector, RcppMatrix and
RcppStringVector in C++ is fully defined in
the respective header files. As example, consider a call from Rto C++ such as
# an R example passing one type of each class to a function # someFunction in package somePackage val <- .Call("someFunction", rnorm(100), # numeric vector sample(1:10, 5, TRUE) # int vector search(), # character vector as.matrix(rnorm(100),10,10), # matrix PACKAGE="somePackage")
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 someFunction(SEXP nvec, SEXP ivec,
SEXP svec, SEXP nmat) {
RcppVector
These C++ objects could then be queried via
int n = nv.size();
int d1 = nm.dim1(), d2 = nm.dim2();
to retrieve, respectively, vector length and matrix dimensions.
Moreover, the stlVector() and stlMatrix() member
functions can be used to convert the objects into STL objects:
vector
RcppExample. See the RcppExamples-package for examples of the
recommended
# set up some value
vector <- (seq(1,9))^2
# call the underlying C++ function
result <- RcppVectorExample(vector)
# inspect returned object
resultRun the code above in your browser using DataLab