[Rd] call R function in c++ program

From: Guillaume B. <guillaume.proteowiki_at_free.fr>
Date: Thu, 16 Aug 2007 05:08:53 -0700 (PDT)

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

// for translate std::vector to SEXP VECTOR
// std_vector are defined as "vector<double>" and iterator as

     PROTECT( vector_SEXP = allocVector(REALSXP, std_vector.size()) );
     int i = 0;
     for(std_iterator s_it=std_vector.begin(); s_it!=std_vector.end();
         REAL(vector_SEXP)[i] = (double) *s_it;

// 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);

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) );

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.

R-devel_at_r-project.org mailing list
Received on Thu 16 Aug 2007 - 13:42:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 16 Aug 2007 - 17:38:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.