能够从C或Fortran的读.Rdata文件格式? [英] It is possible to read .Rdata file format from C or Fortran?

查看:581
本文介绍了能够从C或Fortran的读.Rdata文件格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写在C语言带来的R附加(C功能进行从研发调用)。

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

我的code需要计算使用2个不同的数据集在同一时间一个统计,我需要与所有可能的对组合执行此。然后,我需要所有这些统计(非常大的阵列)继续在C面的计算。这些文件非常大,通常约为40GB,这是我的问题。

My code needs to compute a statistic using 2 different datasets at the same time, and I need to perform this with all possible pair combinations. Then, I need all these statistics (very large arrays) to continue the calculation on the C side. Those files are very large, typically ~40GB, and that's my problem.

要做到这一点基于C由R叫,首先我需要装载R中的所有数据集,然后将它们传递到C函数调用。但是,理想地,有可能只对存储器的那些文件2的同时保持,以下的顺序,如果我们能够从直接C或Fortran的访问的数据集

To do this on C called by R, first I need to load all the datasets in R to pass them then to the C function call. But, ideally, it is possible to maintain only 2 of those files on memory at the same time, following the sequence if I were able to access the datasets from C or Fortran directly:

open  file1 - open file2 - compute cov(1,2)
close file2
hold  file1 - open file3 - compute cov(1,3)
... // same approach

这是R上很好,因为我可以加载/卸载文件,但调用C或Fortran语言时,我没有任何机制来加载/卸载文件。所以,我的问题是,我直接读Fortran语言或C .Rdata文件,能够打开/关闭它们?任何其他方法的问题?

This is fine on R because I can load/unload files, but when calling C or Fortran I haven't any mechanism to load/unload files. So, my question is, can I read .Rdata files from Fortran or C directly, being able to open/close them? Any other approaches to the problem?

据我读过,答案是否定的。所以,我考虑从RDATA移动到HDF5。

As far as I've read, the answer is no. So, I'm considering to move from Rdata to HDF5.

推荐答案

这是不是太难的C,调用R的功能和使用 .CALL 接口。所以写了输入数据的R函数和调用从C.当你与一个文件来完成,不保护()您已经阅读中的数据。这在说明以下

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.

这篇关于能够从C或Fortran的读.Rdata文件格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆