title: How to (easily) wrap a c++ function unto an easy to install R 'package'
body: <p>Suppose I have an c++ code (see below for a simple example). 
  I want to make it easy for a journal referee to install/run 
  it.</p>
  
  <p>So i figured the easiest way is to warp it unto a simplified 
  R package-like tar.gz file so the referee could install it 
  by simply invoking install.packages to a local .tar.gz file.</p>
  
  <p>The reason for this is that i do not know what machine the
   referee is using, but i'm pretty sure the referee would 
  know how to install a R packages so it's much easier for 
  me to warp my code as a R 'package' --or at any rate, 
  something sufficiently similar to it that it could be 
  installed by a simple call to install.package(). </p>
  
  <p>An answer to a earlier <a href="https://stackoverflow.com/a/5172509/189035">question</a> seems to suggest this 
  is indeed possible. I followed the suggestions therein and 
  created a /src directory with my cpp code (the one shown 
  below) and a Makevars.win file containing:</p>
  
  <pre><code>## This assume that we can call Rscript to ask Rcpp about its locations
  ## Use the R_HOME indirection to support installations of multiple R version
  #PKG_LIBS = $(shell $(R_HOME)/bin/Rscript.exe -e "Rcpp:::LdFlags()")
  PKG_CPPFLAGS = -I../inst/include -I.
  PKG_CXXFLAGS = -DEIGEN_DONT_PARALLELIZE $(SHLIB_OPENMP_CXXFLAGS)
  PKG_LIBS = $(shell $(R_HOME)/bin/Rscript.exe -e "Rcpp:::LdFlags()") $(SHLIB_OPENMP_CXXFLAGS)
  </code></pre>
  
  <p>and a Makevars file containing:</p>
  
  <pre><code>## Use the R_HOME indirection to support installations of multiple R version
  #PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` 
  # This was created by RcppEigen.package.skeleton, but the R script that is 
  # called creates error message:
  # PKG_CPPFLAGS = `$(R_HOME)/bin/Rscript -e "RcppEigen:::RcppEigenCxxFlags()"`
  PKG_CPPFLAGS = -I../inst/include
  PKG_CXXFLAGS = -DEIGEN_DONT_PARALLELIZE $(SHLIB_OPENMP_CXXFLAGS)
  PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` $(SHLIB_OPENMP_CXXFLAGS)
  </code></pre>
  
  <p>e.g. I simply followed the answer in the SO post to look around for how 
  this is done in other packages (I also add RcppEigen to the list of dependencies because 
  this guarantees that Eigen is installed on the target machine). I also created a /R directory containing the file MSE.R which contains:</p>
  
  <pre><code>fx01&lt;-function(x){
      x&lt;-as.matrix(x)
      Dp&lt;-rep(0,ncol(x))
      fit&lt;-.C("mse",as.integer(nrow(x)),as.integer(ncol(x)),as.single(x),as.single(Dp))
      as.numeric(fit[[4]])
  }
  </code></pre>
  
  <p>and an empty /inst/include  and a /man directory containing a minimal (but valid) .Rd file. 
  I've added a NAMESPACE file containing:</p>
  
  <pre><code>import(Rcpp)
  import(RcppEigen)
  useDynLib(MySmallExample)
  </code></pre>
  
  <p>Here is the question:</p>
  
  <ul>
  <li>the c++ function otherwise compiles/runs fine. Is there a way to warp it unto a R package so as to make it easy to install/run by a third person.</li>
  </ul>
  
  <p>Here is the c++ code used for this example. </p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include &lt;functional&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  
  #include &lt;Eigen/Dense&gt;
  #include &lt;Eigen/LU&gt;
  #include &lt;Eigen/SVD&gt;
  
  using namespace std;
  using namespace Eigen;
  using Eigen::MatrixXf;
  using Eigen::VectorXf;
  
  
  float median(VectorXf&amp; x) {
      int n=x.rows();
      int half=(n+1)/2;   
      half--;                 
      float med;
      nth_element(x.data(),x.data()+half,x.data()+x.size());  
      if((n%2)==1){
          med=x(half);
      } else {
          float tmp0=x(half);
          float tmp1=x.segment(half+1,half-1).minCoeff(); 
          med=0.5*(tmp0+tmp1);
      }
      return med;
  }
  VectorXf fx01(MatrixXf&amp; x){
      int p=x.cols();
      int n=x.rows();
      VectorXf Recept(n);
      VectorXf Result(p);
      for(int i=0;i&lt;p;i++){
          Recept=x.col(i);
          Result(i)=median(Recept);
      }
      return Result;
  }
  extern "C"{
      void mse(int* n,int* p,float* x,float* medsout){
          MatrixXf x_cen=Map&lt;MatrixXf&gt;(x,*n,*p);  
          VectorXf MedsOut=fx01(x_cen);
          Map&lt;VectorXf&gt;(medsout,*p)=MedsOut.array();
      }
  }
  </code></pre>