简体   繁体   中英

R external interface

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/RC-interface.ppt .

Thanks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM