<div dir="ltr"><div>Hi Martin - </div><div><br></div><div>I, too, am unsure about the mechanics of what you are wanting to do.  But this piece: </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> 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.</blockquote><br><div class="gmail_extra">looks similar to what I am doing in a package that I maintain:  The user writes some C++ code, compiles it via `R CMD SHLIB` and loads the shared object into the current R session with `dyn.load()` (actually, I provide an R function or ten to help manage that).  This is that "minimal amount of C++ code" you were referring to.  The user then hands off  the address of those symbols (`?getNativeSymbolInfo`) to the developer-provided code base (in an R package), turn that into pointer to the function(s) that can be called by the code-base as needed.  My package is written with Rcpp and regular old C++;  all of the data that gets handed off to the user-defined functions are C++ data types so I don't need to link back to Rcpp.  But it's easy enough to link up with Rcpp  (or whatever else) when compiling the shared object.</div><div class="gmail_extra"><br></div><div class="gmail_extra">I'm pretty sure Dirk has similar(?) setup in his RcppDE package where users can provide a compiled function for DEoptim to call when needed. deSolve is another package that does something like this, but no Rcpp involvement.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Of course, this requires some coordination between what is going on in the shared object and the main code base, but it's not unreasonably complicated to work out.  The flexibility you get is well worth it.   And absolutely no problem juggling multiple user-defined functions in the same R session. </div><div class="gmail_extra"><br></div><div class="gmail_extra">Best Regards,</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_signature"><div dir="ltr">Kyle Baron<div><div>Metrum Research Group</div><div><a href="https://github.com/metrumresearchgroup/mrgsolve">https://github.com/metrumresearchgroup/mrgsolve</a><br></div></div><div><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 14, 2016 at 10:55 AM, JJ Allaire <span dir="ltr"><<a href="mailto:jj.allaire@gmail.com" target="_blank">jj.allaire@gmail.com</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">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"><div><div class="h5">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></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><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><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><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></div></div><span class="">_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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></span></blockquote></div><br></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><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Kyle Baron<div><div>Metrum Research Group</div><div><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice"><span title="Call with Google Voice">860-735-7043, Ext. 202<br></span></span></span></span></span></span></span></span></span></span></span></div><div><span style="font-family:Helvetica;font-size:12px"><a href="mailto:kyleb@metrumrg.com" target="_blank">kyleb@metrumrg.com</a></span></div><div><br></div></div></div></div>
</div>