That is tremendously helpful. Â Thanks Charlie! --- On Mon, 4/12/10, Sharpie [via R] <ml-node+1837936-614100331-179...@n4.nabble.com> wrote:
From: Sharpie [via R] <ml-node+1837936-614100331-179...@n4.nabble.com> Subject: Re: Getting started with .C To: "Jeff Brown" <dopethatwantsc...@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]]
______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel