Skip to content
Prev 1055 / 10988 Next

[Rcpp-devel] [RcppExamples] -- would someone share the makefile?

Hi,

This is precisely because getting these bits is somewhat involved that 
we provide Rcpp.package.skeleton. Once the package is generated by it, 
you just have to modify the .cpp and .h files to do whatever you want to 
do, as long as you respect the requirements of the .Call interface.

The Rcpp-package vignette explains what Rcpp.package.skeleton does and 
why. There are essentially two important things :

- your package must find Rcpp headers, this is achieved by adding 
LinkingTo: Rcpp in your DESCRIPTION. This will help R CMD INSTALL by 
adding automatically the -I flag so that you can use #include <>

- your package must link against the Rcpp library (this is a C++ library 
provided by Rcpp. This is accomodated by the PKG_LIBS line in the 
Makevars file


Something else you might find interesting is the inline package, it lets 
you write C++ code directly from within the R console (provided you have 
all the required tools).

For example, with the code that is on page 7 of the Rcpp-introduction 
vignette :

fx <- cxxfunction( signature( a = "numeric", b = "numeric" ), '
Rcpp::NumericVector xa(a);
Rcpp::NumericVector xb(b);
int n_xa = xa.size() ;
int n_xb = xb.size() ;
Rcpp::NumericVector xab(n_xa + n_xb - 1);
double* pa = xa.begin() ;
  double* pb = xb.begin() ;
double* pab = xab.begin() ;
int i,j=0;
for (i=0;i<n_xa;i++)
	for(j=0;j<n_xb;j++)
		pab[i + j] += pa[i] * pb[j];
return xab ;

', plugin = "Rcpp" )

x <- rnorm(10)
y <- rnorm(10)
fx( x, y )

You can even do this to make a skeleton package out of this function :

package.skeleton( "wonderland", fx )

Romain


Le 21/08/10 22:30, Johannes Egner a ?crit :