I'm trying to use R within C++ via RInside. I'm having trouble passing armadillo matrices to R and returning a result. Below I am able to return a results from an R library function, however I get the wrong result. I'm using the skewness function from the moments package as an example which works as should in R. I checked the examples from RInside and i'm still unsure how to use RcppArmadillo. How do I properly pass an armadillo matrix in c++ to R?
#include <RInside.h>
#include <RcppArmadillo.h>
using namespace std;
using namespace arma;
int main(int argc, char *argv[]) {
RInside R(argc, argv);
string R_libs = "suppressMessages(library(moments));";
R.parseEvalQ(R_libs);
mat A = randu<mat>(5,5);
R["A"] = A;
string R_skewness = "B <- skewness(A);";
//this fails
mat B = Rcpp::as<mat>(R.parseEval(R_skewness)); //terminate called after throwing an instance of 'Rcpp::not_a_matrix'
//this works but wrong
mat B = Rcpp::as<vec>(R.parseEval(R_skewness)); // returns only 1 number, should be 5 ( 1 for each columnn), same result if i change mat B to vec B
exit(0);
}