[R] Flushing the R output buffer.

Richard chergr at bigpond.com
Thu Oct 31 00:22:11 CET 2002

ripley at stats.ox.ac.uk wrote:

> On Wed, 30 Oct 2002, Richard wrote:
> > flusHi,
> ???
> > I am connecting R to a PHP web script.
> >
> > The first effort attempted to use a named pipe
> > but gave up on that because PHP could not read
> > any data from the named pipe.
> >
> > I changed to using a temp file but it also has problems.
> > It appears that the file to which R's output is redirected
> > is not recieving any output until the pipe connection form PHP
> > is closed. I think this means that R is buffering its output
> > and there appears to be no flush function.
> Redirected output is normally buffered: it's an issue for your OS's
> runtime system.  R is just a C program.

Are you saying that R cannot implement a flush function
and that there is a Linux command to accomplish this?

> > I would like to use a sequence like:
> >
> > open pipe to R and incommand redirect R outout to file/fifo
> >
> > open file
> > send R command
> > read response from file
> > close file
> >
> > open file
> > send R command
> > read response from file
> > close file
> > .
> > .
> > close pipe
> >
> > I checked the archives and there a function
> > flush.console() was mentioned -- I looked for this in
> > the docs but could not find it. The function does not appear
> > to affect flushing of the R output to the file.
> Well, it is documented for the Windows GUI, where it does work.
> How can a function you can't find affect R when you can't run it?

I'm not sure how to interpret this. Its not listed as a function
in refman.pdf. It is mentioned in connection with the Windows
version in the list archives and it seemed reasonable that it should be
for any OS. I tried it, it does not produce a run-time error, it simply
does not flush. I can only guess that it is in the interpreter's
table of names but is missing an implementation or is buggy
on Linux.

> > This is a RedHat Linsux 7.3 on x86 install
> Try using connections, in particular a fifo and play with blocking.  It
> would not be too hard to implement a flush() function if you want one
> (there is internal support for it in the design of connections): if
> you do write one, please contribute it.

Well it might be just a few lines of code, but by the time I become
some approximation of a unix systems programmer and study the
R source code to figure out how to modify it and figure out
how to compile R it just might take a long time.

When you say "try using connections" it is not clear if
you are referring to modifying R or doing something
with PHP. PHP has to start up R and at the same time
build some kind of communications link. The cleanest
approach would be a bidirectional pipe but this
appears to be an experimental feature of PHP
(its only available via the CVS distribution according to the docs).
For a production server with 500 student's hanging off
it, experimental features are not ideal.

The other approach, described above, is a unidirectional pipe
combined with
a named pipe or file for the return communications.
(as in: popen("R --silent --slave > named_pipe", "w") )
The lack of a working flush in R makes this approach difficult.

Another approach is to send all of the R script in one go
as part of the command using <<
with popen set to read mode and to use the R print function to surround
each separate item of output with a delimiter and a tag
and then read all of the returned output with PHP
and parse it with PHP to break the output
down into its components. Ick!!

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list