Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R external interface

Tags:

c

r

I would like to implement some R package, written in C code.

The C code must:

  • take an array (of any type) as input.
  • produce array as output (of unpredictable size).

What is the best practice of implementing array passing?

At the moment C code is called with .C(). It accesses array directly from R, through pointer. Unfortunately same can't be done for output, as output dimensions need to be known in advance which is not true in my case.

Would it make sense to pass array from C to R through a file? For example, in ramfs, if using linux?

Update 1: Here the exact same problem was discussed. There, possible option of returning array with unknown dimensions was mentioned:

Splitting external function into two, at the point before calculating the array but after dimensions are known. First part would return dimensions, then empty array would be prepared, then second part would run and populate the array in R.
In my case full dimensions are known only once whole code is executed, so this method would mean running C code twice. Taking a guess on maximal array size isn't optional either.

Update 2: It seems only way to do this is to use .Call() instead, as power suggested. Here are few good examples: http://www.sfu.ca/~sblay/R-C-interface.ppt.

Thanks.

like image 404
alm Avatar asked Dec 21 '12 01:12

alm


1 Answers

What is the best practice of implementing array passing?

Is the package already written in ANSI C? .C() would then be quick and easy.

If you are writing from scratch, I suggest .Call() and Rcpp. In this way, you can pass R objects to your C/C++ code.

Would it make sense to pass array through a file?

No

Read "Writing R Extensions".

like image 133
power Avatar answered Oct 03 '22 21:10

power