[Rcpp-devel] How to put the code 2013-01-05-r-function-from-c++.cpp in the src/ directory of a package?

Peng Yu pengyu.ut at gmail.com
Sun May 26 01:05:32 CEST 2013


> You. Are. Still. Doing. It. Wrong.

I know that I did was wrong. Otherwise it should have worked. What's
the point of reiterating it and emphasizing it? Something more
constructive is more helpful.

> Why did you think you'd need dyn.load?  Which of my posts to you had that?

Because dyn.load showed up in the output of sourceCpp(verbose=T) which
is mentioned in your previous email.

Anyway, here is the minimal code that I figured out. Rshl.sh is just a
wrapper for compiling the .so file when Rcpp is used in the source
code. And indeed dyn.load is needed.

~/dvcs_src/rexample/Rexample/cran/Rcpp/sourceCpp/decipher/sourceCppFunction$
cat main.R
library(Rcpp)
system('Rshl.sh callFunction.cpp')

callFunction=Rcpp:::sourceCppFunction(
	func=function(x, f) {}
	, isVoid=F
	, dll=dyn.load('callFunction.so')
	, symbol='callFunction'
	)

callFunction
set.seed(42)
x=rnorm(1e5)
fivenum(x)
callFunction(x, fivenum)

~/dvcs_src/rexample/Rexample/cran/Rcpp/sourceCpp/decipher/sourceCppFunction$
cat callFunction.cpp
#include <Rcpp.h>

using namespace Rcpp;
RcppExport SEXP callFunction(SEXP xSEXP, SEXP fSEXP) {
    NumericVector x = Rcpp::as<NumericVector>(xSEXP);
    Function f = Rcpp::as<Function>(fSEXP);
    NumericVector __result = f(x);
    return Rcpp::wrap(__result);
}
~/dvcs_src/rexample/Rexample/cran/Rcpp/sourceCpp/decipher/sourceCppFunction$
Rscript main.R
> library(Rcpp)
> system('Rshl.sh callFunction.cpp')
make: Nothing to be done for `all'.
>
> callFunction=Rcpp:::sourceCppFunction(
+ 	func=function(x, f) {}
+ 	, isVoid=F
+ 	, dll=dyn.load('callFunction.so')
+ 	, symbol='callFunction'
+ 	)
>
> callFunction
function (x, f)
.Primitive(".Call")(<pointer: 0x103c733c0>, x, f)
> set.seed(42)
> x=rnorm(1e5)
> fivenum(x)
[1] -4.043276349 -0.682384496 -0.002066374  0.673324712  4.328091274
> callFunction(x, fivenum)
[1] -4.043276349 -0.682384496 -0.002066374  0.673324712  4.328091274
>
>


-- 
Regards,
Peng


More information about the Rcpp-devel mailing list