[R] Controlling R from Java

Duncan Temple Lang duncan at research.bell-labs.com
Wed Dec 13 19:21:08 CET 2000

> Date: Tue, 12 Dec 2000 15:38:16 -0500
> From: Marc Grushcow <Marc.Grushcow at ntt.ca>
> Reply-To: Marc.Grushcow at ntt.ca
> Organization: NTT SYSTEMS INC.
> Sender: owner-r-help at stat.math.ethz.ch
> Precedence: bulk
> I'd like to run R (LINUX) under Java. I can create the process and connect to
> stdin and stdout. I can send commands in and get output back.  I don't know how
> to determine when the output is complete - there is no trailing "prompt".
> For example, if I send "ls()\n" what I get back is:
> > ls()
> character(0)
> Each of the above two lines is terminated with a 0x0A. I would have expected
> (liked)...
> ls()
> character(0)
> > 
> Then I could parse for the "\n> " and know when the output is complete. The
> problem with checking for data availability on the input stream is that it is
> unreliable. R's output buffer might flush unpredictably and/or the task
> switching OS might cause R to stop after one buffer is full but before another
> is completed.  In any case, it is the usual problem of one program trying to
> parse another's output in a time sharing environment.

This is a good point and just one of the reasons that communicating
between two systems using strings and pipes, etc. can be a lot of work
and still quite unstable and non-portable.  It is also one of the
reasons that we have spent some time in the Omega project
(http://www.omegahat.org) coming up with a general way to allow
interpreters such as Java, Python and Perl to be embedded within R and

I hesitate in suggesting a totally different approach for your
application, especially since you may have invested some time already
and have other constraints. But here goes.....

You can use the Java package (http://www.omegahat.org/RSJava) to get
communication between an R and a Java interpreter.  You can run an R
session and execute Java code within it (directly from the R prompt),
or alternatively run a Java application and run a (non-interactive) R
session within it and evaluate R expressions.  I have just added
facilities to make the calling R from Java simpler and wrote some
notes which are available from


Not only does this avoid the flushing problems you observere, but it
also avoids having to parse the output generated by R and preserves
much more information in the communication step.  Also, there is a
general mechanism to add ones own converters to translate Java objects
to R and vice-versa.

I hope this gives you another approach to consider for your
application.  If you have any questions, you can send me mail directly
or to the omega development mailing list (omega-devel at omegahat.org).


> I'm sure that this problem has been solved before.  Any suggestions would be
> appreciated.
> Thanks
> Marc
> -- 
> =.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.
> mailto:Marc.Grushcow at NTT.ca		http://www.NTT.ca
> =.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.


Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
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