From ripley at stats.ox.ac.uk  Wed Nov 16 18:06:49 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 16 Nov 2005 17:06:49 +0000 (GMT)
Subject: [Rd] two-way communication using Unix pipes
In-Reply-To: <437B5722.9050302@lancaster.ac.uk>
References: <f68da330511160715m6ad1ab40s6744c67aed1998ff@mail.gmail.com>
	<437B5722.9050302@lancaster.ac.uk>
Message-ID: <Pine.LNX.4.61.0511161653290.14633@gannet.stats>

On Wed, 16 Nov 2005, Barry Rowlingson wrote:

> Jonathan Callahan wrote:
>
>> Can someone please explain to me exactly what R is doing with the the
>> standard IO handles and whether or not there is any simple way to convince
>> it to behave as if it were talking to a user at the other end of a keyboard
>> and terminal? I've already tried '--no-readline' but that doesn't solve my
>> problem.
>>
>
> This little noddy example works for me:
>
> #!/usr/bin/perl
>
> use FileHandle;
> use IPC::Open2;
>
> $pid=open2(*Reader, *Writer, "R --no-save");
> print Writer "x=runif(10);print(mean(x))\n";
> while($got=<Reader>){
>     print "Got ".$got;
> }
>
> Of course, without the \n in the command string it doesnt work at all,
> but I dont see any problems with R reading from stdin and writing to
> stdout....
>
> This is on a Linux box, I dont think you mentioned an OS or platform...

Well `Unix' is in the subject line so I was assuming not Windows and 
probably not MacOS.

To answer the (somewhat unrelated) question, R for Unix-alikes does

system.c:    R_Interactive = isatty(0);

and so it will not `behave as if it were talking to a user at the other 
end of a keyboard' unless stdin is `connected to a terminal'.  (I am 
pretty sure that pty's count, as that is what ESS uses, but this could be 
OS-dependent.)  Otherwise R does nothing with stdin, so standard rules 
about e.g. line-buffering apply.

And --no-readline is only relevant if R is `connected to a terminal', 
since R_ReadConsole talks directly to stdin (via fgets) if !R_Interactive.

R is Open Source and you can get definitive answers by reading the code.

-- 
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



[Rd] two-way communication using Unix pipes
Prof Brian Ripley