[R] Evaluating expressions from within C code

Timur Elzhov Timur.Elzhov at jinr.ru
Mon Jan 13 11:03:02 CET 2003


Dear R experts!

I want to evaluate expressions from within C code. For instance,
I want this code:

    /* ============ -- C code -- ============== */
    
    #include <R.h>
    #include <Rinternals.h>
    
    SEXP foo(SEXP expr, SEXP rho)
    {
        SEXP x;
    
        PROTECT(x = allocVector(REALSXP, 1));
        REAL(x)[0] = 1;
    
        UNPROTECT(1);
    
        return eval(expr, rho);
    }
    /* ======================================== */


    .Call("foo", quote(x + 1), new.env())

to return 1 + 1 == 2. But it certainly does not work:

    .Call("foo", quote(x + 1), new.env())
    Error: Object "x" not found

and if I define x in R, it works:

    x <- 1
    .Call("foo", quote(x + 1), new.env())
    [1] 2

So, is there a way to solve my problem, namely to
find and evaluate expressions that appears in C code?

Thank you very much.

--
WBR,
Timur.




More information about the R-help mailing list