[R] Calling R functions from C

Florent Baty florent.baty at unibas.ch
Fri Oct 28 18:01:14 CEST 2005


Dear R users,

I read on the "Introduction to the .C Interface to R" by Peng & Leeuw 
(http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf) that it is 
possible to use a few R functions (such as "dnorm") within C by 
including the "Rmath.h" header file in your C code:
e.g.

#include <R.h>
#include <Rmath.h>
void kernel_smooth(double *x, int *n, double *xpts, int *nxpts,
double *h, double *result)
{
int i, j;
double d, ksum;
for(i=0; i < *nxpts; i++) {
ksum = 0;
for(j=0; j < *n; j++) {
d = xpts[i] - x[j];
ksum += dnorm(d / *h, 0, 1, 0);
}
result[i] = ksum / ((*n) * (*h));
}
}


In the manual "Writing R extensions" there is also a list of special 
functions which can be called in C.

I was wondering whether there is a way to call any other R functions 
similarly. Is there any documented exemple available somewhere?

Thanks a lot for your help,

Florent




More information about the R-help mailing list