[Rcpp-devel] Shared code in C++ files

Martin Lysy mlysy at uwaterloo.ca
Thu Apr 14 06:25:06 CEST 2016


Hello Dirk,

For my specific purpose I have some reservations about package creation.
Here is a description of the project I have in mind.

I would like to use Rcpp to create a set of generic MCMC algorithms, into
which a useR could easily hook their own target distributions written in
C++ with minimal effort.  For example, I would provide the following files:

--------------------------------------
GibbsSampler.cpp

//[[Rcpp::export]]
NumericMatrix GibbsSampler(int nIter, NumericVector xInit, List
tuningParams) {
  // run some flavor of the Gibbs sampler
}
---------------------------------------
GibbsSampler.h

double logDensity(NumericVector x);
---------------------------------------

Then the useR would write MyDensityA.cpp, which contains the definition of
logDensity, compile the three files, and have a Gibbs sampler for their
specific density function written in C++ and ready to go in R.  However, a
useR might wish to use the GibbsSampler for a different density tomorrow.
They would have a different definition of logDensity in MyDensityB.cpp.
Ideally, the useR would have access to Gibbs samplers for both densities in
the same R session.

I can think of two ways of doing this with Rcpp:

1.  Compile with sourceCpp (this is what I'm currently doing).  There's the
minor issue of giving separate R names to each Gibbs sampler, but there are
many ways around that.  The major issue is that sourceCpp only accepts a
single .cpp file (or at least as far as my attempts were unsuccessful in
the original post).  So I'm stuck text-processing a bunch of .cpp and .h
files together (the actual project I'm working on has about a dozen of
each).

2.  Compile an entire R package for each of MyDensityA and MyDensityB.
However, it seems somewhat cumbersome to have a package loaded for every
density function in the workspace.  Moreover, naming conflicts are a bit
more tricky.  Right now (with sourceCpp), I'm using an interface of the form

smpA <- gibbs.sampler(density = MyDensityA, niter = 1e4)
smpB <- gibbs.sampler(density = MyDensityB, niter = 1e4)

This is to align with things like lm(formula = MyModel).  However, I can't
quite see how to do this with separate packages loaded.  Rather it seems
I'd need something like

smpA <- gibbs.sampler.MyDensityA(niter = 1e4)
smpB <- gibbs.sampler.MyDensityB(niter = 1e4)

However, to do this with packages I feel like I would still have to do some
text replacement, which I'm already doing with the sourceCpp approach.

In summary, I am not opposed to package creation, but I hope you can see my
reservations at taking this route.  Perhaps you could please suggest a way
to achieve what I'm after with separate Rcpp packages for each density
function.  I take it from your reluctance to answer my original post that
Rcpp only supports compilation of multiple files through the package
creation protocol.  I can think of many applications in which the useR
could supply a minimal amount of C++ code (e.g., a log-likelihood function)
to hook in with a large amount of code provided by the developer in order
to speed things up considerably.  So in my opinion it would be worthwhile
to devise a mechanism to do this correctly.

Best regards,


Martin Lysy
Assistant Professor of Statistics
Department of Statistics and Actuarial Science
University of Waterloo

On Wed, Apr 13, 2016 at 5:34 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

>
> Martin,
>
> Please please please look into creating a package.
>
> If you use RStudio:  File -> New Project -> (New or Existing) Directory ->
> Package and then select Rcpp.
>
> If not, consider install the (very tiny) pkgKitten package and call
> Rcpp.package.skeleton() from Rcpp itself (but enhanced by pkgKitten if
> present) for a saner package.
>
> Cheers, Dirk
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160414/fcf800e5/attachment-0001.html>


More information about the Rcpp-devel mailing list