[Rcpp-devel] R package linking to C: coding advice

Dirk Eddelbuettel edd at debian.org
Sun May 31 20:56:06 CEST 2015


Hi Guillaume,

On 31 May 2015 at 20:46, Guillaume Chapron wrote:
| Hello list,
| 
| I am writing a R package for an ecological audience to model some populations. I think at this stage some advise may avoid me make naive mistakes. I have already done quite a lot of work (but before thinking about an R package) and now I need to glue all but this does not look simple!
| 
| The population model is already written in C. It uses the GSL for RNG, the Glib for managing pointer arrays and command line passing, and libdispatch (Grand Central Dispatch on a Mac) for multithreading. I control this model from R using the system() function, and read the csv files the model exports. This is somewhat a caveman approach that works well for my research, however, I think a proper package needs to be done in a much more elegant way. Here is what I intend to do:

a) GSL for RNGs is (probably) inferior to what R does; if your main interest
is in the R package (as opposed to R as well as a standalone program) then it
may make sense to just use R's RNGs.  Rcpp makes that very, very easy.

b) "caveman approach"  :)  Yep, I feel the same way which is why I have
worked so much on better interfaces.  We all started with system() at some
point but it is really, really limited.

| 1) I forget about multi-threading with GCD as this is Mac and Ubuntu derivatives only. I am not aware of a cross platform way of multi-threading C so things will just run more slow even if this is unfortunate.

Good idea. You can probably add it later via logic from configire and some
#ifdef, but that is more work.  Thumbs up for something basic first.
 
| 2) I obviously forget about using the system() function and will call the C model from R using .Call() or (better?) use Rccp instead. Question: what is the best approach to pass a 3 dimensional numerical array? What is the best approach to return several arrays, including a 3 dimensional one of size 10,000 * 100 * 10 (at least)?

I don't do 3-dim arrays often but I seem to recall that i) I have done so in
an Armadillo project and ii) Rcpp itself does it too.  At the end of the day
these are just vectors with a dimension attributes that is not of dim 2.
This should work, but may need some testing.
 
| 3) I also forget about using the Glib functions and rewrite myself the pointer array functions (the model has a lot of arrays of pointers to C structures)

Right. C++, call by reference and STL types should pretty alleviate that need
anyway.  The programming style may be new to you though.
 
| 4) For the RNG, I am not sure what to do. Right now, things work fine with the GSL, but the whole GLS is not easily portable to Windows. I could call the RNG defined in R, however it seems that R's RNG are much slower than the GSL ones. Should I instead rewrite the RNG in the C model so that they are standalone from the GSL or use something else like http://www.pcg-random.org ?

I would use R's RNGs but there _are_ packages that use the GSL RNGs
portably.  My almost-but-no-quite-finished RcppZiggurat package uses the GSL
RNG as another RNG to benchmark against.

Looks like you have a fine handle on things.  I have found over the years
that I work best when disentangling issues.  So maybe you want to work on
small sub-packages dealing with 1) to 4) separately til you feel you have a
handle and only then put them back together into the "opus magnus" :)

| Thanks very much. I think your expert knowledge will allow me to avoid making wrong foundational choices. It will be my second package (after MDPtoolbox) but the technical level will be much higher!

My pleasure.

Amicalement,  Dirk
 
| Thanks!
| 
| Guillaume
| 
| --
| Guillaume Chapron, PhD
| 
| Associate Professor
| 
| Grimsö Wildlife Research Station
| Swedish University of Agricultural Sciences
| SE - 73091 Riddarhyttan, Sweden
| 
| http://www.carnivore.science/
| Twitter @CarnivoreSci
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list