> Comment: A concise example that uses an XPtr in a C++ function is > still lacking.
Sorry I missed Manuel's example when I last posted. I guess I was thinking something like below, which is a direct analog of apply. This concisely mirrors the semantics in RcppDE (interchangable work function for a well-defined task). Here, the user needs only change inc. Only thing is, this example seems annoyingly long for inclusion in the quickref. Also, I'm not sure whether using inline here is more or less confusing to the overall idea... Any thoughts on documentation priorities? -xian require(inline); require(Rcpp) inc <- ' // Define a simple function double myFun(SEXP xs) { // sum Rcpp::NumericVector x(xs); double sum = std::accumulate(x.begin(), x.end(), 0.0); return(sum); } ' src <- ' // Define a pointer to the above function typedef double (*funcPtr)(SEXP); return(XPtr<funcPtr>(new funcPtr(&myFun))); ' src1 <- ' // Define a pointer to the above function typedef double (*funcPtr)(SEXP); XPtr<funcPtr> myptr(infun); // apply myptr to rows/columns of x NumericMatrix xx(x); int margin = as<int>(margin_); int nr = xx.nrow(); int nc = xx.ncol(); if ( margin == 1 ) { NumericVector ret(nr); for( int i = 0; i<nr; i++) { ret[i] = (*myptr)( wrap(xx(i,_)) ); }; return(ret); }; if ( margin == 2 ) { NumericVector ret(nc); for( int i = 0; i<nc; i++) { ret[i] = (*myptr)( wrap(xx(_,i)) ); }; return(ret); }; ' mksum <- cxxfunction(signature(), body = src, includes = inc, plugin="Rcpp") myapply <- cxxfunction(signature(x='numeric', margin_='integer', infun='XPtr'), body = src1, includes = inc, plugin="Rcpp") aa = matrix(1:100, nrow=5) res1 = myapply(aa, 1, mksum() ) res1R = apply(aa, 1, sum) res2 = myapply(aa, 2, mksum() ) res2R = apply(aa, 2, sum) -- A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama! _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel