I have a function like so:
callFunc <- function (f) {
f(1)
}
f
could be (for example) f <- function (x) x
. To simplify things, let's say that I know that f should return a numeric and take in a single numeric.
I'd like to move callFunc
to C, but still have the function f
defined in R, i.e.
.Call('callFunc', function (x) x)
I'm struggling with how to evaluate my callback on the C side. I have it like this at the moment:
#include <R.h>
#include <Rdefines.h>
SEXP callFunc (SEXP i_func) {
return i_func(1);
}
(Then to test it:
R CMD SHLIB test.c
# then in R
dyn.load('test.so'); .Call('callFunc', function (x) x)
)
Of course, the above does not work because
- I have not coerced
i_func
into the appropriate closure form; I'm not sure how to do this (there areAS_foo
macros inRdefines.h
, but noAS_CLOSURE
). - I haven't even told the C code that
i_func
should take in a numeric and return a numeric, so how can it even evaluate?
Could anyone give me pointers on how to go about doing this?
I'm working my way through writing R extensions but this is rather long and I haven't found what I'm after yet. Also there is this question on R-help but the answer looks like they implemented the callback f
in C as well, rather than leaving it as an R object.