How to pass environment to Rcpp chunk with knitr?
I'm trying to take the following code and translate it into a knitr chunk (borrowed from Rcpp examples):
library(Rcpp)
library(inline)
openMPCode <- '
std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
size_t n = x.size();
#pragma omp parallel for shared(x, n)
for (size_t i=0; i<n; i++) {
x[i] = ::log(x[i]);
}
return Rcpp::wrap(x);
'
## modify the plugin for Rcpp to support OpenMP
settings <- getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS <- paste('-fopenmp', settings$env$PKG_CXXFLAGS)
settings$env$PKG_LIBS <- paste('-fopenmp -lgomp', settings$env$PKG_LIBS)
funOpenMP <- rcpp(signature(xs="numeric"), body=openMPCode, settings=settings)
Basically, the main problem is to ensure that the environment is passed into the knitr block so that PKG_LIBS
it is PKG_CXXFLAGS
set appropriately when the code is compiled. An example of what a snippet might look like knitr
:
```{r engine='Rcpp'}
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector openmp_log( NumericVector x ) {
size_t n = x.size();
#pragma omp parallel for
for( size_t i=0; i < n; i++ ) {
x[i] = ::log10( x[i] );
}
return x;
}
```
If I understand correctly, knitr uses sourceCpp
for whatever in the chunk compiles it and passes the parameters specified in engine.opts
- sourceCpp
. Hence, I assume there are two potential directions:
-
setting
engine.opts=list(env=...)
so that the appropriate environment is transferred; however, I am getting an error trying this ("env" formal argument matched by multiple actual arguments). -
The hook / custom chunk parameter can be used to customize the environment, but I'm not sure how exactly this can be done.
This is with knitr 1.0.11 and Rcpp 0.10.2.
I didn't know that users would pass the custom environment to the engine Rcpp
, so I passed the default environment to the argument env
in sourceCpp()
. I have now removed the constraint. You can install the development version on Github.
PKG_LIBS
mentioned by Dirk, there must be another problem here.
Today we realized that the treatment PKG_LIBS
is a mistake: instead of trying, it is overwritten. So I'm afraid this does not currently work with Rcpp attributes.
You can switch to using a package or set compilation flags, etc. somewhere else.
Edit Feb 16 This is now fixed in SVN.