[Rd] call R function in c++ program

Guillaume B. guillaume.proteowiki at free.fr
Thu Aug 16 14:08:53 CEST 2007


Hi all
I don't know if my message are correct in this forums.
I create a program in c++ who use statistical function. I want to execute
this function in R (in particular for use packages ade4, lattice,
bioconductor...)

Until now, my program work for simple function ("plot", "rnorm"...) but I
can't use library

My class are :

// in constructor
     int argc = 1;
     char *argv[] = {"wxR"};
     Rf_initEmbeddedR(argc, argv);
     rho = R_GlobalEnv;

// in destructor
     Rf_endEmbeddedR(0);

// for translate std::vector to SEXP VECTOR
// std_vector are defined as "vector<double>" and iterator as
"vector<double>::iterator"
     PROTECT( vector_SEXP = allocVector(REALSXP, std_vector.size()) );
     int i = 0;
     for(std_iterator s_it=std_vector.begin(); s_it!=std_vector.end();
s_it++)
     {
         REAL(vector_SEXP)[i] = (double) *s_it;
         i++;
     }
     UNPROTECT(1);

// for create a variable in R environement
     defineVar(install("variable_name"), variable_value_SEXP, rho);

// for execute "complex" function (with parser)
     PROTECT( e1 = mkString("plot(variable_name, type=\"l\")") );
     PROTECT( e2 = R_ParseVector(e1, 1, &status, R_NilValue) );
     R_tryEval(VECTOR_ELT(e2,0), rho, &hadError);
     UNPROTECT(2);

// for execute "simple" function (without parser)
     PROTECT( e1 = lang3(install(":"), ScalarInteger(1), ScalarInteger(4))
);
     PROTECT( e2 = lang4(install("matrix"), e1, ScalarInteger(4),
ScalarInteger(1)) );
     PROTECT( e3 = lang2(install("layout"), e2) );
     eval(e3,R_GlobalEnv);
     UNPROTECT(3);

// for call library (using parser)
     PROTECT( e1 = mkString("library(ade4, logical.return=TRUE);
     PROTECT( e2 = R_ParseVector(e1, 1, &status, R_NilValue) );
     e3 = R_tryEval(VECTOR_ELT(e2,0), rho, &hadError);
     UNPROTECT(2);
     if(LOGICAL(e3)[0])
          // succes
     else
          // echec

Until this point, all my function succes !

When I try to call function in ade4 library, I have error 1 :
     PROTECT( e1 = mkString("a<-dudi.pca(b,scannf=FALSE, nf=2)") 
     PROTECT( e2 = R_ParseVector(e1, 1, &status, R_NilValue) );
     R_tryEval(VECTOR_ELT(e2,0), rho, &hadError);
     UNPROTECT(2);

You know where come from the problem ?

Bonus question 1 :
I can't call directely the function plot(x, type="l") without parser.
if I try somthing like this, it's doesn't work :
     PROTECT( e2 = lang4(install("type"), mkChar("l")) );
     PROTECT( e3 = lang3(install("plot"), e1, e2) );
     eval(e3,R_GlobalEnv);
     UNPROTECT(3);
How can I resolve this ?

Bonus question 2 :
When I call some "plot" function, my program crash. And I don't khow why


Thanks for yours suggests
-- 
View this message in context: http://www.nabble.com/call-R-function-in-c%2B%2B-program-tf4279222.html#a12180033
Sent from the R devel mailing list archive at Nabble.com.



More information about the R-devel mailing list