where is a good tutorial on creating an R package

2019-01-31 07:28发布

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!!

2条回答
成全新的幸福
2楼-- · 2019-01-31 08:03

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-31 08:04

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.

查看更多
登录 后发表回答