[Rd] Calling R function from C

Ricardo Rios alfilnegro.sv at gmail.com
Mon Nov 27 03:54:13 CET 2006


Hi wizards, I have the following function, I call  R function from C.
I execute this commands

> dyn.load("difficult.so");
> out <- .Call("difficult");
$mean
[1] 0.65

$median
[1] 0.65


However I would like to call difficult.R  but in C , how in the
following code:
> pch <- array(dim=c(2,1));
> pch[1] = 0.5;
> pch[2] = 0.8;
> out <- difficult(pch)$mean;

does somebody know how to do it ? Thanks in advance.



// C function
#include <Rdefines.h>
#include <Rinternals.h>
#include <stdlib.h>

SEXP difficult()
{
    SEXP fun, pch;
    SEXP e;
    SEXP value;

    PROTECT(e = allocVector(LANGSXP, 2));
    fun = findFun(install("difficult"), R_GlobalEnv);
    SETCAR(e, fun);
    pch = allocVector(REALSXP, 2);
    REAL(pch)[0] = 0.5;
    REAL(pch)[1] = 0.8;
    SETCADR(e, pch);
    value = eval(e, R_GlobalEnv);
    UNPROTECT(1);

	return (value);     	
}


difficult <- function(vector)
{
   mean <- mean(vector);
   median <- median(vector);

   return (list(mean=mean,median=median));
}




-- 
personal web site:
http://www.geocities.com/ricardo_rios_sv/index.html



More information about the R-devel mailing list