To go from C++ types to R objects, you can use wrap
. The way I usually construct NumericMatrix
s from a std::vector<double>
is like so:
// with x as a std::vector<double>
using namespace Rcpp;
NumericVector m = wrap(x); // wrap x into an R object
m.attr("dim") = Dimension(<num_rows>, <num_cols>); // set the dimensions
with <num_rows>
and <num_cols>
chosen based on your desired dimensions. m
should then be usable as an R matrix.
In general, you can use as<T>
to go from R types to C++ types, and wrap
to go from C++ types to R types. See section 3 of the Rcpp-Introduction vignette for some more information.