<div dir="ltr">I don't know enough about the mechanics of your scenario to comment intelligently, but I would in general agree with Dirk that if there is a way to put something like this in a package that's the way it ought to be done. You could certainly dispatch to different C++ functions within a package using a variety of mechanisms including C++ templates, C-style function pointers, etc. (the mechanics of doing so for your situation I'm unsure of, but it should be possible).</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 14, 2016 at 12:25 AM, Martin Lysy <span dir="ltr"><<a href="mailto:mlysy@uwaterloo.ca" target="_blank">mlysy@uwaterloo.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div>Hello Dirk,<br><br></div>For my specific purpose I have some reservations about package creation. Here is a description of the project I have in mind.<br><br>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:<br><br>--------------------------------------<br>GibbsSampler.cpp<br><br>//[[Rcpp::export]]<br>NumericMatrix GibbsSampler(int nIter, NumericVector xInit, List tuningParams) {<br> // run some flavor of the Gibbs sampler<br>}<br>---------------------------------------<br>GibbsSampler.h<br><br>double logDensity(NumericVector x);<br>---------------------------------------<br><br>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.<br><br>I can think of two ways of
doing this with Rcpp:<br><br>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).<br><br>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<br><br></div>smpA <- gibbs.sampler(density = MyDensityA, niter = 1e4)<br></div>smpB <- gibbs.sampler(density = MyDensityB, niter = 1e4)<br></div><div><br></div>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<br><br></div>smpA <- gibbs.sampler.MyDensityA(niter = 1e4)<br></div>smpB <- gibbs.sampler.MyDensityB(niter = 1e4)<br><br></div>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.<br><br>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.<br><br></div>Best regards,<br><div><div><div><div><div><div><div><div><div><br></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><span class=""><br clear="all"><div><div><div dir="ltr">Martin Lysy<br>Assistant Professor of Statistics<br>Department of Statistics and Actuarial Science<br>University of Waterloo</div></div></div>
<br></span><span class=""><div class="gmail_quote">On Wed, Apr 13, 2016 at 5:34 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Martin,<br>
<br>
Please please please look into creating a package.<br>
<br>
If you use RStudio: File -> New Project -> (New or Existing) Directory -><br>
Package and then select Rcpp.<br>
<br>
If not, consider install the (very tiny) pkgKitten package and call<br>
Rcpp.package.skeleton() from Rcpp itself (but enhanced by pkgKitten if<br>
present) for a saner package.<br>
<br>
Cheers, Dirk<br>
<span><font color="#888888"><br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
</font></span></blockquote></div><br></span></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>