Skip to content

Calling R function from C

1 message · Ricardo Rios

#
Hi wizards, I have the following function, I call  R function from C.
I execute this commands
$mean
[1] 0.65

$median
[1] 0.65


However I would like to call difficult.R  but in C , how in the
following code:
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));
}