R:从已编译代码中的现有连接读取

R: Reading from an existing connection in compiled code

我希望能够从任意 R 连接(在 ?connections 的意义上)进行读取,该连接将由用户传递给 R 函数,然后通过.Call.

文件 R_ext/Connections.h 中的 R API 指定了一个函数 R_ReadConnection,该函数将指向 Rconn 结构的指针作为其第一个参数,并执行我想要的是。结构本身也在 header 中定义,但除了 getConnection(C 函数)之外,我看不到任何检索该类型结构的方法,它不是 [=26= 的一部分].据我所知,与连接关联的外部指针也不直接指向结构。

所以,谁能告诉我是否有一种受支持的方法可以将合适的 SEXP 转换为指向关联 Rconn 结构的指针?

提前致谢。

我认为没有(我认为这是一种疏忽)。解决方法是声明一个适当的原型并使用它

Rconnection getConnection(int n);

SEXP connect_me(SEXP conn) {
    getConnection(INTEGER(conn)[0]);
    return R_NilValue;
}

在 R 3.3.0 中添加了 R API 函数 R_GetConnection()。它执行从 SEXP 到指向 Rconn 的指针的转换(a.k.a。Rconnection)。因此,现在的解决方案是

#include <R_ext/Connections.h>

SEXP myfunction (SEXP conn_)
{
    Rconnection conn = R_GetConnection(conn_);
    // Do something with the connection
    
    return R_NilValue;
}

这是 documented in NEWS:

R_GetConnection() which allows packages implementing connections to convert R connection objects to Rconnection handles. Code which previously used the low-level R-internal getConnection() entry point should switch.