[R] read/write connections

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jul 30 08:20:15 CEST 2009


>From the help:

      Not all modes are applicable to all connections: for example URLs
      can only be opened for reading.

You omitted to tell us your platform (and requests to include that and 
not send HTML mail are in the posting guide), and pipe() differs by 
platform.  But AFAIR pipe() is one-way in all implementations.

On Wed, 29 Jul 2009, Markus Loecher wrote:

> Dear fellow R users,
> I would very much like to see an example of read/write connection (open =
> "r+" ) for e.g. pipe() or any other R connection.

There is an example for file() on the very same help page!

> I have a standalone program which accepts input from stdin, performs some
> processing and returns the results on stdout. Is it possible at all to open
> a connection to that program, write to it (i.e. to stdin of that process)
> and read back the results ?

Not with a conventional pipe: look up your undisclosed OS's 
documentation for what a pipe is.  E.g. mine says

   The pipe() function creates a pipe (an object that allows
   unidirectional data flow) and allocates a pair of file descriptors.
   The first descriptor connects to the read end of the pipe; the
   second connects to the write end.

Note *unidirectional*.  However 'man popen' mentions 'bidirectional 
pipes' on Mac OS X (but not on Linux).

You can probably use system() to do what you ask, including the 'silly 
example'.

> As a silly example, imagine the following use of the Unix function head:
>
> zz <- pipe(" head ", open ="r+");
> cat(rnorm(10), file = zz);
>
> Error in cat(rnorm(10), file = zz) : cannot write to this connection
>
> While I am not surprised that this does not work, I would love to know a
> solution to this general problem.
>
> Thanks
>
> Markus
>
> 	[[alternative HTML version deleted]]

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list