It is possible to read .Rdata file format from C or Fortran?

后端 未结 1 1464
臣服心动
臣服心动 2020-12-20 07:31

I\'m working writing some R extensions on C (C functions to be called from R).

My code needs to compute a statistic using 2 different datasets at the same time, and

1条回答
  •  生来不讨喜
    2020-12-20 08:09

    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())
    

    A simpler approach is to invert the problem -- read two data files in, then call C with the data.

    0 讨论(0)
提交回复
热议问题