
That is tremendously helpful.  Thanks Charlie!

--- On Mon, 4/12/10, Sharpie [via R] <ml-node+1837936-614100331-179239@n4.nabble.com> wrote:

From: Sharpie [via R] <ml-node+1837936-614100331-179239@n4.nabble.com>
Subject: Re: Getting started with .C
To: "Jeff Brown" <dopethatwantscash@yahoo.com>
Date: Monday, April 12, 2010, 10:11 PM




Jeff Brown wrote:
Hi,


I'm trying to learn to use .C, which lets one invoke compiled C code from within R.  To do that, one has to first get the C code into R as a shared object, which (I think) means first compiling it (with COMPILE or SHLIB) and then loading it (with dyn.load()).  


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:
The document suggests calling it from R like this (again the details aren't important):


conv <- function(a, b) 

        .C("convolve",

                as.double(a), 

                as.integer(length(a)), 

                as.double(b), 

                as.integer(length(b)), 

                ab = double(length(a) + length(b) - 1))$ab


I wrote a file, "convolve.c", with nothing but the above C code in it.  I can't figure out how to compile it.  I don't understand the syntax (no parentheses?) and I always get the same information-free error message:


> list.files()

[1] "AER"        "convolve.c" "sendmailR" 

> R CMD SHLIB "compile.c"

Error: syntax error

> COMPILE "compile.c"

Error: syntax error

> R CMD SHLIB "compile"

Error: syntax error

> COMPILE "compile"

Error: syntax error

> R CMD SHLIB compile.c

Error: syntax error

> COMPILE compile.c

Error: syntax error

> R CMD SHLIB compile

Error: syntax error

> COMPILE compile

Error: syntax error


I'm using an Intel MacBook Pro running Leopard.  At a console, typing "gcc --version" yields 4.2.1.  I know I'm supposed to be using version 4.2; I thought 4.2.1 would qualify, but please let me know if I'm wrong about that.


For guidance I've been relying on "Writing R Extensions", "R Installatino and Administration", the "R for Mac OS X Developer's Page", and the built-in help.  Please let me know if there are other important resources I've missed.


Many thanks,

Jeff


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!


-CharlieCharlie Sharpsteen

Undergraduate-- Environmental Resources Engineering

Humboldt State University





View message @ http://n4.nabble.com/Getting-started-with-C-tp1837912p1837936.html


To unsubscribe from Getting started with .C, click here.







-- 
View this message in context: http://n4.nabble.com/Getting-started-with-C-tp1837912p1837955.html
Sent from the R devel mailing list archive at Nabble.com.

	[[alternative HTML version deleted]]

