[Rd] Calling R function from C

From: Ricardo Rios <alfilnegro.sv_at_gmail.com>
Date: Mon 27 Nov 2006 - 02:54:13 GMT


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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Mon Nov 27 13:57:50 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 27 Nov 2006 - 17:30:51 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.