2

I want to load the rpart package in R from inside C++, use the rpart() function in the rpart package to fit a CART model from inside C++, call the result in C++, and store it for further processing. The code that I tried is given below. I am very new to Rcpp and cpp and would appreciate some input into what I am doing wrong.


#include <RInside.h>                    // for the embedded R via RInside
#include <unistd.h>
using namespace Rcpp;

SEXP testfunc(int argc, char *argv[]) {

    // create an embedded R instance
    RInside R(argc, argv);               

    // running and storing an rpart model utilizing the rpart package from R
    std::string cmd = "rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis);";

    SEXP test = R.parseEval(cmd);
    
    return test;
}

aak201
  • 21
  • 2
  • 2
    Welcome to SO, aak201! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically after `set.seed(1)`), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Aug 14 '20 at 20:38
  • 4
    (And since you tagged [tag:rcpp] and [tag:rinside], you might get Dirk's (author of both) attention. The documentation he has written for both is generally pretty good, so be ready for suggestions to read the docs, read the examples, and explain why your code is different enough that the docs are insufficient. http://dirk.eddelbuettel.com/code/rinside.html) – r2evans Aug 14 '20 at 20:40
  • 4
    The fairly large [set of examples in the RInside package](https://github.com/eddelbuettel/rinside/tree/master/inst/examples/standard) should get you going. – Dirk Eddelbuettel Aug 14 '20 at 22:41

0 Answers0