[R] Problem with .C

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Oct 6 15:51:30 CEST 2011



On 06.10.2011 15:41, Steve Lianoglou wrote:
> Hi,
>
> 2011/10/6 Uwe Ligges<ligges at statistik.tu-dortmund.de>:
>> On 06.10.2011 14:51, Jan van der Laan wrote:
>>>
>>> An obvious reason might be that your second argument should be a pointer
>>> to int.
>>>
>>> As others have mentioned, you might want to have a look at Rccp and/or
>>> inline. The documentation is good and I find it much easier to work with.
>>>
>>> For example, your example could be written as:
>>>
>>> library(Rcpp)
>>> library(inline)
>>>
>>> test<- cxxfunction(signature(x = "numeric" ) , '
>>> Rcpp::NumericVector v(x);
>>> Rcpp::NumericVector result(v.length());
>>> for (int i = 0; i<  v.length(); ++i) {
>>> result[i] = v[i] + i;
>>> }
>>> return(result);
>>> ', plugin = "Rcpp" )
>>>
>>
>>
>> Oh, come on, this is now really too much of overkill.
>
> I don't agree that it's overkill -- you get to sidestep the whole `R
> CMD SHLIB ...` and `dyn.load` dance this way while you experiment with
> C(++) code 'live" using the inline package.


You need two additional packages now where you have to rely on the fact 
those are available. Moreover, you have to get used to that syntax, and 
part of it seems to be C++ now? At least I do not know why the above 
should work at all, while I know the simple C function does.

Uwe


> It's really handy.
>
>> Just make the original source
>>
>>
>> void test(double *b, int *l)
>> {
>>      int i;
>>      for(i=0; i<  *l ; i++) b[i] += i;
>> }
>>
>>
>> which you would have know after reading the Wriiting R Extensions manual.
>
> I agree that this step is unavoidable no matter which avenue (Rcpp or
> otherwise) one decides to take.
>
> -steve
>



More information about the R-help mailing list