[Rd] C-Side: Applying a function (given as param) to data (given as param)

oliver oliver at first.in-berlin.de
Fri Jun 3 22:19:55 CEST 2011

On Fri, Jun 03, 2011 at 11:14:39AM -0500, Douglas Bates wrote:
> On Fri, Jun 3, 2011 at 5:17 AM, oliver <oliver at first.in-berlin.de> wrote:
> > Hello,
> >
> > I'm implementing a package (C-extension),
> > where one function gets data and a function
> > that needs to be applied to the data.
> >
> > I want to apply the function to (parts of)
> > the data on the C-side.
> >
> > 1) how do I apply a function (given via SEXP) to data
> > 2) how do I select parts of the data (also provided via SEXP)?
> Not to be facetious but you begin by reading the "Writing R Extensions" manual.

I already read inside it.
If there would be no question open i would not have registered to this mailing list
and woulod not have asked that question.
Obviously my question was not answered in the "Writing R Extensions",
so if you can give me a hint this would be nice.

> An alternative is to read the vignette Rcpp-Introduction available as
> http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf and soon

I use C, not C++.

But maybe it helps too.

I already created my own package with R and C successfully some days ago.

But so far I did not used fucntion pointers coming in via SEXP parameters.

And that was my specific question.

> to be in The R Journal.  They show an explicit example of apply in
> compiled code (C++ using the Rcpp structures, in their case).

As just mentioned: I already created successfully a C-extension for R.

But I would like to know, how to call a function that I get via
SEXP as parameter. How can I find out the function definition,
for example the arity of the function and which arguments
a function uses.

The problem is, that the C-standard (at least the first ANSI-C standard)
does not guarantee portability for C-pointers.
To be portable, for example the function pointer you use must
definitely be of same type as the function you use.

So I need to know how I can use the SEXP-function pointers.

To be more concrete, this is how my function's API looks like
at the moment:

SEXP movapply( SEXP data, SEXP width, SEXP func )
  /* some code */

  return ( result );

  data   will be vector or list or matrix
  width  will be int value (I think vector of length 1)
  func   will be a function pointer of the type that is given as argument

I don't know on which kind of pointer to cast.
I think I will cast to a pure C-type, but then all data also must
match to the function definition.

What if the function wants to work on integer, but the data is double?

Somehow this must be handled, and I think the experts here can just point me
directly to some kind of docs or maybe older postinmgs here, which explain this?


More information about the R-devel mailing list