From alfilnegro.sv at gmail.com  Mon Nov 27 03:54:13 2006
From: alfilnegro.sv at gmail.com (Ricardo Rios)
Date: Sun, 26 Nov 2006 20:54:13 -0600
Subject: [Rd] Calling R function from C
Message-ID: <50a711930611261854q51ceffat30e1b42e2cbc4f5c@mail.gmail.com>

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



[Rd] Calling R function from C
Ricardo Rios