R: Reading from an existing connection in compiled code

The R API function R_GetConnection() was added in R 3.3.0. It performs the conversion from SEXP to pointer to Rconn (a.k.a. Rconnection). Hence, the solution is now

#include <R_ext/Connections.h>

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

This was 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.

Tags:

Connection

Api

R