更新时间:2023-01-29 18:30:32
使用 .Call
接口从 C 调用 R 函数并不难.因此,编写一个输入数据的 R 函数,并从 C 中调用它.当您完成一个文件时,UNPROTECT() 将您读入的数据.如下所示
It is not too hard to call R functions from C, using the .Call
interface. So write an R function that inputs the data, and invoke that from C. When you're done with one file, UNPROTECT() the data you've read in. This is illustrated in the following
## function that reads my data in from a single file
fun <- function(fl)
readLines(fl)
library(inline) ## party trick -- compile C code from within R
doit <- cfunction(signature(fun="CLOSXP", filename="STRSXP", env="ENVSXP"), '
SEXP lng = PROTECT(lang2(fun, filename)); // create R language expression
SEXP ans = PROTECT(eval(lng, env)); // evaluate the expression
// do things with the ans, e.g., ...
int len = length(ans);
UNPROTECT(2); // release for garbage collection
return ScalarInteger(len); // return something
')
doit(fun, "call.R", environment())
一个更简单的方法是反转问题 - 读取两个数据文件,然后用数据调用 C.
A simpler approach is to invert the problem -- read two data files in, then call C with the data.