Skip to content
Prev 36412 / 63421 Next

Getting started with .C

Jeff Brown wrote:
I would suggest taking it a step further and building an R package to hold
your compiled code.  The pros are:

  *  It keeps the R wrapper scripts and other things you will end up
creating packaged together with your code.

  *  It handles compilation automagically during installation.

  *  It handles loading the dylib for you.

The only con I can think of is:

  *  It takes ~2 extra minutes of your time to set up.  But compared to
other languages I have used this is a ridiculously small price to pay for
the portability and organization offered by packages.

I wrote a post that goes through step-by-step how to do this for the .Call()
interface, including example code.  You can find it at:

 
http://n4.nabble.com/Writing-own-simulation-function-in-C-td1580190.html#a1580423



In "Writing R Extensions", p. 79, they give the following example of a C
program for convolution of two vectors.  (The details aren't important; it's
just a function that does something to some stuff.)

void convolve (double *a, int *na, double *b, int *nb, double *ab) {
	int i, j, nab = *na + *nb - 1;
	for(i = 0; i < nab; i++)
		ab[i] = 0.0;
	for(i = 0; i < *na; i++)
		for(j = 0; j < *nb; j++)
			ab[i + j] += a[i] * b[j]
}
Jeff Brown wrote:
All R CMD commands must be executed at the command line- i.e. in a Windows
CMD shell or Unix/Linux bash shell.  They are not meant for use inside the R
interpreter.

Hope this helps!

-Charlie

-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University