Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Modern Interfaces to C Code

The .Call function offers a higher degree of flexibility than .C in terms of passing and returning R arrays or dataframes. The example below shows how to use .Call to implement the same functionality as the .C example above. The type SEXP represents an R object. The REAL macro returns a pointer to the corresponding memory for reading or writing to it. Here's an example:

#include <R.h>
#include <Rinternals.h>
#include <Rmath.h>
#include <math.h>
int i, j, n = length(aR);
double *a = REAL(aR), *b = REAL(bR);
SEXP Rval = allocVector(REALSXP, n);
for (i = 0; i < n; i++) {
REAL(Rval)[i] = 0;
for (j = 0; j < n; j++)
REAL(Rval)[i] += pow(a[j] + i + 1, b[j]);
return Rval;
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content