From murdoch.duncan at gmail.com  Sat Jun  4 13:51:08 2011
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sat, 04 Jun 2011 07:51:08 -0400
Subject: [Rd] C-Side: Applying a function (given as param) to data
 (given as param)
In-Reply-To: <20110603201955.GA2058@siouxsie>
References: <20110603101712.GD6048@siouxsie>	<BANLkTi=DkQzC74HP5WEiN-eyBiBV0yUDew@mail.gmail.com>
	<20110603201955.GA2058@siouxsie>
Message-ID: <4DEA1C2C.6060504@gmail.com>

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:
>>> 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.

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 changes.

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.

Duncan Murdoch

>
>
> 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



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