[Rd] Pipe / Fork: Partial Solution / Providing Connections from C?

Jan T. Kim jtk at cmp.uea.ac.uk
Fri Feb 11 13:27:39 CET 2005

Dear All,

On Tue, Feb 01, 2005 at 07:50:17PM +0000, Jan T. Kim wrote:
> On Tue, Feb 01, 2005 at 01:44:37PM +0000, Prof Brian Ripley wrote:

> > If we only had to consider standard Unices, pipe() would allow read-write 
> > modes.  As it is, it is easy for you to write an OS-specific extension.

I've looked into this and tried to write a function that would start
an external process and return two connections, one for writing to the
external process and one for reading from it. Unfortunately, I haven't
found a way to implement this in a package, without altering the R
source code itself (details below). As an alternative / workaround,
I coded up a function

   xpipe(cmd, input)

that takes a command to start the external process (cmd) and a character
containing the lines to be written (input), and returns a character
vector containing the output produced by the external process. The
xpipe package is available at


To an extent, this provides the functionality I was looking for, but
it is not satisfactory because the output cannot be processed by R
on line -- xpipe accumulates the entire output and returns it only
after the external process has terminated.

Also technically, it's cumbersome to use: For obtaining something else
than a character value, it seems one has to write the output into an
anonymous file and then use scan, read.table or whatever to read from
that file.

Therefore, I still look for a way to implement the design where the
pipe ends are returned as R connections. The problem in doing so is that
connections are stored in a

    static Rconnection Connections[NCONNECTIONS];

(file src/main/connections.c), and I cannot find any function that
provides an interface for allocating a slot in the Connections array
and storing a connection set up by a the code in my package there.
There is a non-static (i.e. externally visible) NextConnection function
(which is not declared in any header, though), and nothing like

    Rboolean setConnection(int connNumber, Rconnection *conn);
    Rconnection *getConnection(int connNumber);

I haven't found any relevant documentation on these issues (R-exts
doesn't have any info on handling connections in C code at all). Can any
of you direct me to such docs, or point out how I can instantiate and
return connections from within a package?

> Well, that is probably reasonably easy, but (not the least due to that
> fact) I'm still surprised that it has not been done already. I can hardly
> imagine that I'm the first one to want to use some external utility from
> an R program in this way.
> So, what do you R-devel folks do in this case, and what would you
> recommend?

I'm still curious about this one. If there really is no way of running
stuff through external filter processes in R, I'd volunteer to add

Best regards & thanks in advance, Jan
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk at cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*

More information about the R-devel mailing list