29

I'm at a loss when I start looking at configure scripts. I'm not sure how to go about creating an R package which has several functions built from C/C++ in such a way that it's portable between Windows & Linux. My attempts to modify the guts of existing packages have been unfruitful.

Any help or links would be greatly appreciated.

Update: If possible I would like to link against: Boost, CUDA, & hwloc

However, I realize that Boost will be a nightmare & hwloc won't be much better. So I'd settle for just CUDA. This was why I dove in the deep end and tried modifying some existing packages to suit my needs (rgl & rglpk). But I'm willing to start out without dependencies and build from the ground up. Thank you everyone for your suggestions!!

M. Tibbits
  • 8,400
  • 8
  • 44
  • 59
  • 1
    It would be helpful if you could tell us what is insufficient about the manuals that come with your R installation. – Joshua Ulrich Mar 02 '11 at 18:18
  • 2
    I've read "Writing R Extensions". I get lost quite quickly in Section 1.2. My C++ code has a Makefile, but it isn't portable. So I'd like to use the Makevars.in and configure functionality, but I'm not sure how to proceed. Is there an easily understandable example somewhere with say two C++ source files, three header files, and R-code which is all wrapped into an R package? – M. Tibbits Mar 02 '11 at 18:29
  • Usually a configure script is only necessary if you are linking against an external library not provided by R. If you are doing this, it would be helpful to let us know which external libraries you are trying to use. – Sharpie Mar 02 '11 at 20:15

2 Answers2

23

I was also quite lost when writing my first package with compiled code. Here are a few tips, but there is probably better material out there.

The main piece about writing R packages is "Writing R extensions". This is a very complete guide, but that also makes it abit hard to read through: http://cran.r-project.org/doc/manuals/R-exts.pdf

Here is a small tutorial I found on google once which I used first, containing how to use C code: http://www.stat.columbia.edu/~gelman/stuff_for_blog/AlanRPackageTutorial.pdf

Another guide on R packages in general, but not with C code: http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf

Rcpp is a very useful package for C++ code, but I haven't used it a lot yet (3 days in fact). It has a lot of documentation in the package itself.

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
14

My default approach is to learn how others have solved the problem. There are 2800+ CRAN packages, and many have been there for over a decade. The problem is solvable, and has been solved.

Now, it is also true that the documentation is there, but maybe scattered too much. Moreover, targets shift. For example, years ago, we still used src/Makefile, these days that is very much recommended against because of the need of multiarch builds (on OS X, on Windows, and one day also on Linux).

So trying to keep it simple helps. You can in fact have a valid C++ project ... without anything. Just drop the sources files in src/ of your package foo, and R will know how to build libfoo.so or libfoo.dylib or ..., depending on the platform. And if you need other header files, try using src/Makevars. For external dependencies it gets trickier and one what have to learn autoconf et al, but many packages skate by with something simple.

So please do expand your question, show what is failing and document what you tried. I am sure we can help you along.

Edit: And in case you want to this with the Rcpp package (which help with R and C++ integration), then there is an entire vignette about to do this in your own package.

Edit 2: Now that expanded your question, CUDA is a completely different beast. That is more difficult as you mix different compilers etc pp. There are two example packages on CRAN, study those.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    If I'm willing to wrap my C++ code with C functions, then I wouldn't need to use Rcpp would I? – M. Tibbits Mar 02 '11 at 20:39
  • No, but Rcpp is here to help rather than hinder. Many of us feel it makes things easier by *freeing you from having to write repetitive wrappers*. Look at some of the examples, you may like it. But to reiterate: you do *not* need a `src/Makevars` if your project is simple, and you do *not* have to use Rcpp. But some of us think it helps :) – Dirk Eddelbuettel Mar 02 '11 at 20:58
  • Thanks so much for the insight! I'll check it out. – M. Tibbits Mar 03 '11 at 00:57
  • 2
    Excuse me Dirk, can you point out which two packages in CRAN you refered to? – natorro Aug 07 '13 at 03:09