Grokbase Groups R r-devel April 2010
FAQ

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!

-Charlie

-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
--
View this message in context: http://n4.nabble.com/Getting-started-with-C-tp1837912p1837936.html
Sent from the R devel mailing list archive at Nabble.com.

Search Discussions

  • Jeff Brown at Apr 13, 2010 at 2:42 am
    That is tremendously helpful.  Thanks Charlie!

    --- On Mon, 4/12/10, Sharpie [via R] 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.
  • Dirk Eddelbuettel at Apr 13, 2010 at 2:47 am

    On 12 April 2010 at 18:11, Sharpie wrote: | Jeff Brown wrote:
    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.
    All good reasone, but see below for an even easier solution to get going.
    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]
    }
    And all this is even easier if you use the excellent inline package. No
    Makefiles, no linking, no loading, it all "just works" on all three major
    platforms:

    - define the code you want in a variable, here 'code'
    this does not include the function header

    - define the function signature

    - call the 'cfunction' from package inline to compile, link and load the
    generated function

    - use it!

    Here is a live example:

    R> library(inline) # load inline
    R> code <- "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];
    + }"
    R> fun <- cfunction(signature(a="numeric", na="numeric", b="numeric", nb="numeric", ab="numeric"),
    + code, language="C", convention=".C")
    R> str(fun) # check what the new fun object is
    Formal class 'CFunc' [package "inline"] with 2 slots
    ..@ .Data:function (a, na, b, nb, ab)
    ..@ code : chr "#include <R.h>\n\n\nvoid file46e87ccd ( double * a, double * na, double * b, double * nb, double * ab ) {\nint i, j, nab = *na "| __truncated__
    R> fun( 1:10, 10, 4:12, 9, numeric(18))$ab
    [1] 4 13 28 50 80 119 168 228 300 372 400 413 410 390 352 295 218 120
    R>

    Voila, and we extended R at the command prompt. I'd still recommed .Call
    over .C, whether you use C++ (which I also recommend :) or C.

    I have a number of examples for this in the 'Introduction to High-Performance
    Computing with R' tutorials I have given the last few years, see the slides
    at

    http://dirk.eddelbuettel.com/presentations.html

    as well as the recent UCLA talks on more inline examples with Rcpp (if you
    want C++).

    Cheers, Dirk

    --
    Registration is open for the 2nd International conference R / Finance 2010
    See http://www.RinFinance.com for details, and see you in Chicago in April!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-devel @
categoriesr
postedApr 13, '10 at 2:11a
activeApr 13, '10 at 2:47a
posts3
users3
websiter-project.org
irc#r

People

Translate

site design / logo © 2022 Grokbase