Feature: R interface
Using the most primitive R FFI (called .C
) we should be able to write an R client without relying on any of the R headers.
The only unfortunate thing is that this interface imposes two fairly strict requirements:
- All function arguments must be pointers.
- The return type of all the functions must be void.
So we will need to define tiny shims which dereference most of arguments and call the originals. These should be all very tiny functions.
For example, here is working code for the constructor (assumes #7 (closed)):
extern "C" {
// ...
void construct_R(void**, char**, int*, int*);
}
//...
void construct_R(void** out, char** data, int* rng, int* chain){
*out = construct(*data, *rng, *chain );
}
And then in R:
dyn.load("multi_model.so")
ptr <- as.raw(rep(0,8))
data <- "multi_model.data.json"
seed <- 1234
chain <- 0
.C("construct_R", out=as.raw(ptr), data=data, rng=as.integer(seed), chain=as.integer(chain))$out
# prints "[1] b0 ae 03 39 bb 55 00 00"
I can't find any doc which promises that .C
uses the cdecl
calling convention on all platforms, but I'm assuming it does.
Besides being a bit of a chore on the C++ side, I don't know R programming quite well enough to build the object this all needs to live inside, but it should be fairly straightforward to do.
Edited by Brian Ward