[Rd] C-Side: Applying a function (given as param) to data (given as param)
oliver at first.in-berlin.de
Sun Jun 5 18:22:20 CEST 2011
On Sat, Jun 04, 2011 at 07:51:08AM -0400, Duncan Murdoch wrote:
> On 11-06-03 4:19 PM, oliver wrote:
> >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:
> >>>I'm implementing a package (C-extension),
> >>>where one function gets data and a function
> >>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.
> You should almost certainly do this in R, not in C. If you are
> doing it in C code you'll just be duplicating the implementation
> from R, and your code will break if that implementation ever
The reason why I wanted to do this in C, not in R,
is speed as well as consistency.
I have about 700 MB data that I want to analyse....
And loops might be to slow in R.
Some of the apply functions might help and I already implemented
some stuff in R, but the result values that some of the apply functions give me,
are changing under certain circumstances, for example list or matrix or vector.
Also a lot of conversions and value-extracting is needed.
It looked to me as if doing in C would be the better way.
So I want to have the possibility to do that in C,
then loops are no problem, and I can create a certain
result value type, as well as some other features I have in mind.
Also in R the automatic coercion to some types sometimes
is not necessary, and working on the raw values might be
better, it seems to me.
Also I want to learn the C-interface, so that in later cases
I know how to use it.
For simple values I already know how to use it.
But the problem is there, when functions are given as parameters.
That is a very powerful feature. If that only is manageable easy in
R, it might always be a performance bottleneck.
> In R you use formals(fn) to extract the function header.
> >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.
> They are not function pointers, they are pointers to R objects.
Aha, good to know.
Do I have to look at S3 and S4 methods and classses, if I wish to
make it in C?
More information about the R-devel