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

From: Jeff Ryan <jeff.a.ryan_at_gmail.com>
Date: Sat, 04 Jun 2011 07:29:58 -0500

Oliver,

For an example of moving averages, take a look at the C source of the xts and TTR packages. The sources are browsable on R-forge.

In short, REAL etc are functions to extract the data of an SEXP. They need to match the types coming in. So your C needs to check the type and branch accordingly.

It is all in the guide as well as in working example code in R sources as well as many hundreds of package sources. You have access to it all, so spend the time just reading the sources is my recommendation.

Best,
Jeff

Jeffrey Ryan | Founder | jeffrey.ryan_at_lemnica.com

www.lemnica.com

On Jun 3, 2011, at 3:19 PM, oliver <oliver_at_first.in-berlin.de> 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:
>>> 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?
>
>
> Ciao,
> Oliver
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 04 Jun 2011 - 12:36:23 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 06 Jun 2011 - 09:10:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive