[R] Flushing the R output buffer.

Richard chergr at bigpond.com
Fri Nov 1 11:38:22 CET 2002


Hi David,

thats interesting, I'll keep it in mind for the future.

The solution I came up with involved using PHP to attach a script string
to the end of the command line that starts up R like:

$R_script = "R --silent --slave 2>&1 << 'END'" . $R_script;
$R_script .= "quit()\n";
$R_script .= "END\n";
$file_pointer = popen($R_script. "r");
$R_script_result = "";
while(!feop($file_pointer))
{
    $R_script_result .= fread($file_pointer, 1024);
}
pclose($file_pointer);
The script string was then massaged to extract the values I needed.
In my case I used R to format a string that was a PHP script,
extracted this from $R_script_result  and performed a PHP eval on it
so I did not have to do any complex parsing stuff.

Hopes this helps you some time.

Regards from Richard

"David B. Dahl" wrote:

> Richard,
>
> I run into this problem when trying to control R from a Java program.
> Hopefully my solution can help you.
>
> After poking around the source code, I realized that when R starts up, it
> choose between interactive or non-interactive mode, based on whether there is
> a controlling TTY.  Specifically, line 237 of src/unix/system.c has the line
> "R_Interactive = isatty(0);"  My solution is to insert the following line
> immediately after line 237:
> "if ( getenv("R_FORCE_INTERACTIVE") != NULL ) R_Interactive = TRUE;"
> I then compile R.  (You implied that you were adverse to learning how to
> compile from source, but it really isn't hard.  See "R Installation and
> Administration" documentation from the R webpage.)
> Then, within Java, I set the environment variable "R_FORCE_INTERACTIVE" to
> "true" and launch R.  I can then write commands to R and read the response,
> all without buffering or flushing issues.
>
> I hope this helps.  I would be interest in knowing how well this works for you
> or what other solution you come up with.
>
> - - David Dahl

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