[R] A question about embedded R

Song Baiyi song.baiyi at udo.edu
Tue Apr 6 12:26:49 CEST 2004


Hello everyone,

I met a strange problem when I call R expression from C++, here is the 
details:

I edited a function eval_R_command();

double eval_R_command(const char *funcName)
{
    SEXP exp,e;
    ParseStatus status = PARSE_OK;
    int i,n;
    double val;

    PROTECT(exp=NEW_CHARACTER(1));
    SET_STRING_ELT(exp, 0, COPY_TO_USER_STRING(funcName));
   
    PROTECT(e = R_ParseVector(exp, 1, &status));
    n = GET_LENGTH(e);
    for(i = 0; i < n ; i++)
        val = (double)REAL(eval(VECTOR_ELT(e,i), R_GlobalEnv))[0];
    UNPROTECT(2);

    return(val);
}

Then if i call with

eval_R_command("mean(1:5)")
it worked fine and got 3,

but

eval_R_command("sum(1:5)")
it produced a strange number 7.41098e-323! not 15

What is the problem?
By the way which is the better way to call the function in R from C++, 
or they are same?

1. Pass the whole expression as string to R
2. Use Rf_findFun

Thank you very much!

Baiyi Song

CEI
Dortmund University, Germany




More information about the R-help mailing list