Search code examples
rcpprinside

why can't convert SEXP to Rcpp::NumericMatrix?


I use R by rinside

std::string cmd = "m <-matrix(c(1:9),nrow=3))";
SEXP proxy =  m_R.parseEvalNT(cmd);
Rcpp::NumericMatrix m(proxy); 

the runtime to terminate it in an unusual way

anyone please help me thanks


Solution

  • Odd. I played with this and also got some run-time errors; now they no longer arise. Try this:

    // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
    //
    // cf http://stackoverflow.com/questions/18403402/
    //
    // GPL'ed 
    
    #include <RInside.h>                    // for the embedded R via RInside
    
    int main(int argc, char *argv[]) {
    
        RInside R(argc, argv);              // create an embedded R instance 
    
        std::string cmd = "m <- matrix(c(1:9),nrow=3)";
        Rcpp::NumericMatrix M = R.parseEval(cmd);  // assign mat. M to NumericMatrix
        int n = M.nrow();
        int k = M.ncol();
        std::cout << "All good:" << n << " by " << k << std::endl;
    
        exit(0);
    }
    
    edd@max:~/svn/rinside/pkg/inst/examples/standard$ ./so20130823 
    All good:3 by 3
    edd@max:~/svn/rinside/pkg/inst/examples/standard$ 
    

    which I just copied into the examples/standard/ directory as a simple call to make will then build it.