Any stats or math geek dopers out there? This is a very strange and specific question.
My boss has given me the wonderful task of writing some c code and implementing it in R (the GNU verion of the S programming language) in both Linux and the Win32 environments.
R link --> http://www.r-project.org/
I’m quite a newbie when it comes to both C and R but I’m afraid my boss likes to say things like “you’re a smart kid, you’ll figure it out”. LOL.
My biggest obstacle so far has been calling the compiled C code in R. Take this C code, for example, to calculate a simple Mean:
#include “Average.h”
void average(float indata, unsigned long n, float *average)
{
unsigned long j;
for (*average=0.0, j=1; j<=n; j++);
{
*average = *average + indata[j];
}
*average = *average/n;
}
With header file Average.h:
void average(float indata, unsigned long n, float *average);
I complied this using GCC in linux to generate my shared library with:
gcc -fPIC -c Average.c ld -shared -soname Average.so.1 -o Average.so.1.0 -lc Average.o
Now, I fire up R and load the library:
> dyn.load("/home/lars/c/Average.so.1.0")
You then have to “map” the function so:
> testRfunc <- function(a,b)
.C(“average”, as.double(a), as.integer(length(a), as.double(average))
When I then call the testRfunc I get a “Segmentation Fault” which shutsdown R. Ughh…
Does anyone have any idea what I’m doing wrong?
Sorry for the lenghty post and obscure cry for help…