[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
Sat May 25 13:50:39 CEST 2013


Hi Dirk,

Here is what I got. Note that I have to used .Call and change the
arguments to the cpp function as SEXP and convert them to Function and
NumericVector inside the cpp function.

But the example 2013-01-05-r-function-from-c++.cpp takes Function and
NumericVector as arguments directly. This calling syntax can not be
used in src/? In this case, does .Call have to be used in R/ (if
cppFunction is used then .Primitive rather than .Call is used)?

> callFunction
function (x, f)
.Primitive(".Call")(<pointer: 0x103c73120>, x, f)

/tmp/testFunction$ cat R/rcpp_hello_world.R
rcpp_hello_world_R=function(f, x){
  .Call(rcpp_hello_world, f, x)
}

/tmp/testFunction$ cat src/rcpp_hello_world.cpp
#include "rcpp_hello_world.h"

SEXP rcpp_hello_world(
		SEXP f
		, SEXP x
		){
	using namespace Rcpp;

	Function ff(f);
	NumericVector xx(x);
	NumericVector res = ff(xx);

	return res;
}

On Fri, May 24, 2013 at 8:44 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 24 May 2013 at 08:25, Peng Yu wrote:
> | > Stick callFunction() in that package.
> |
> | I have read the documents that you mentioned and I'm able to put other
> | functions in a package. The difference for "callFunction" is that it
> | takes Function as an argument so I'm not able to get the code work in
> | cxxfunction. Is there an exampling using cxxfunction (rather than the
> | sourceCpp and cppFunction examples given by Xiao) for
> | 2013-01-05-r-function-from-c++.cpp? Once I have a working example with
> | cxxfunction, I then should be able to put it in a package.
>
> Read the _basic_ Rcpp documentation and learn about the Function class. There
> is nothing special here. Here is a basic example:
>
> R> cppFunction('NumericVector applyFunction(Function f, NumericVector x) { return f(x); }')
> R> applyFunction(log, 1:10)
>  [1] 0.000000 0.693147 1.098612 1.386294 1.609438 1.791759 1.945910 2.079442 2.197225 2.302585
> R> applyFunction(sqrt, 1:10)
>  [1] 1.00000 1.41421 1.73205 2.00000 2.23607 2.44949 2.64575 2.82843 3.00000 3.16228
> R>
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



-- 
Regards,
Peng


More information about the Rcpp-devel mailing list