Re: [Rd] getConnection, R_outpstream_st

From: Romain Francois <>
Date: Sun, 08 Nov 2009 10:14:18 +0100


Maybe I should give more background about what we are trying to do with this. We (Dirk, Saptarshi and I) are interfacing R with the protocol buffer C++ library from google [1,2] .

protocol buffers are google's data interchange format, designed to be efficient (much more efficient than XML) and language neutral.

Google supports bindings to java, c++ and python, and there are dozens of third party bindings (perl, C, C#, ...) available. We are doing the one for R, allowing to create and manipulate protocol buffer messages from R. For example with this message type :

package tutorial ;
message Person {

   required int32 id = 1;
   required string name = 2;
   optional string email = 3;

you can create a message like this :

message <- new( tutorial.Person, id = 0, name = "Romain", email = "" )

We can also read/write messages from/to files :

tutorial.Person$read( "somefile" )
serialize( message, "somefile" )

Now we'd like to be able to use the extensive IO support R provides (connections) and essentially read and write messages from and to arbitrary binary connections. Practically, it means calling this method of the google::protobuf::Message c++ class [3,4]

bool Message::ParseFromIstream(istream * input) bool Message::SerializeToOstream( ostream * output) const

This means we need to be able to build istream and ostream pointers working together with a binary R connection. It seemed to me from reading the code of serialize.c in R that we would be able to achieve this by sort of wrapping R_inpstream_st and R_outpstream_st as c++ streams.

The only thing I cannot figure out at that point is how to access a connection pointer (Rconnection). From this :

/* The connection interface is not yet available to packages. To

    allow limited use of connection pointers this defines the opaque     pointer type. */
typedef struct Rconn *Rconnection;
void R_InitConnOutPStream(R_outpstream_t stream, Rconnection con,

			  R_pstream_format_t type, int version,
			  SEXP (*phook)(SEXP, SEXP), SEXP pdata);
void R_InitConnInPStream(R_inpstream_t stream,  Rconnection con,
			 R_pstream_format_t type,
			 SEXP (*phook)(SEXP, SEXP), SEXP pdata);

I get that if I do have a Rconnection, then I can initialize a R_outpstream_t and just use this, but I can't find a way to get one. The internal "getConnection" seems to do just this, but it is not accessible from packages.

We'd appreciate any help.


[3] [4]

On 11/07/2009 11:24 AM, Romain Francois wrote:
> Hello,
> I'm trying to use the limited connections api defined in
> Rinternals.h. I have code that looks like this (inspired from
> do_serializeToConn) :
> SEXP serialize_to_connection( SEXP xp, SEXP connection ){
> Rconnection con ; struct R_outpstream_st out; R_pstream_format_t type
> = R_pstream_binary_format ; SEXP (*hook)(SEXP, SEXP) = NULL ; con =
> getConnection(Rf_asInteger(connection)); R_InitConnOutPStream(&out,
> con, type, 0, hook, R_NilValue );
> return R_NilValue ; }
> The problem I have is that I cannot actually call getConnection since
> it is not part of the api. Is there another way to get the
> Rconnection that is associated with a number.
> Many thanks.
> Romain

Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
|- : celebrating R commit #50000
|- : RGG #158:161: examples of package IDPmisc
`- : New R package : sos

______________________________________________ mailing list
Received on Sun 08 Nov 2009 - 09:23:24 GMT

This archive was generated by hypermail 2.2.0 : Sun 08 Nov 2009 - 17:10:22 GMT