可以从 C 或 Fortran 读取 .Rdata 文件格式吗? [英] It is possible to read .Rdata file format from C or Fortran?

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

问题描述

我正在编写一些关于 C 的 R 扩展(要从 R 调用的 C 函数).

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

我的代码需要同时使用 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.

要在 R 调用的 C 上执行此操作,首先我需要加载 R 中的所有数据集以将它们传递给 C 函数调用.但是,理想情况下,如果我能够直接从 C 或 Fortran 访问数据集,则可以按照顺序在内存中同时维护其中的 2 个文件:

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.

推荐答案

使用 .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.

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

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