Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using R in Java?

5 views
Skip to first unread message

Nicros

unread,
Feb 19, 2009, 12:23:52 PM2/19/09
to
Hi 2 questions-
1. Is there a package that will allow me to run R scripts (entirely)
from Java?
2. If so, is there a way to capture the output of those scripts,
(including images) and embed them in my SWT java app?

My challenge is I have a java app that does some statistical chores-
it would be fantastic if the users could use their R skills to modify
a script in whatever R environment they like and then my app could use
that script to calculate the results and display them in the app.

I have found StatET and JavaGD with rJava/JRI and read through all the
docs... its seems possible that some combination may give me what I
want, but its not very clear.

Any suggestions anyone?

Thanks!

John B. Matthews

unread,
Feb 20, 2009, 10:42:29 AM2/20/09
to
In article
<8fa02db7-4b61-4389...@j10g2000prn.googlegroups.com>,
Nicros <bcc...@gmail.com> wrote:

[...]


> 1. Is there a package that will allow me to run R scripts (entirely)
> from Java?
> 2. If so, is there a way to capture the output of those scripts,
> (including images) and embed them in my SWT java app?

[...]

I don't know of one, but I have used java.lang.Process to run other
command line applications. "The ProcessBuilder.start() and Runtime.exec
methods create a native process and return an instance of a subclass of
Process that can be used to control the process and obtain information
about it."

<http://java.sun.com/javase/6/docs/api/java/lang/Process.html>

You can capture the output for display in a suitable text widget. I am
unfamiliar with R image format(s):

<code>
import java.io.*;

/** @author John B. Matthews */
class ExecTest {

public static void main (String[] args) {
String s;
try {
Process p = Runtime.getRuntime().exec(
"ls . /foo");
// read from the process's stdout
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
System.out.println(s);
}
// read from the process's stderr
BufferedReader stderr = new BufferedReader (
new InputStreamReader(p.getErrorStream()));
while ((s = stderr.readLine()) != null) {
System.err.println(s);
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
System.err.println("Exit value: " + p.waitFor());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
</code>

<console>
$ java ExecTest
.:
Alpha.java
Beta.java
[...]
ls: /foo: No such file or directory
Exit value: 1
</console>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Graham Jones

unread,
Feb 20, 2009, 1:00:17 PM2/20/09
to

"John B. Matthews" <nos...@nospam.invalid> wrote in message
news:nospam-92CCEC....@news.aioe.org...

> In article
> <8fa02db7-4b61-4389...@j10g2000prn.googlegroups.com>,
> Nicros <bcc...@gmail.com> wrote:
>
> [...]
>> 1. Is there a package that will allow me to run R scripts (entirely)
>> from Java?
>> 2. If so, is there a way to capture the output of those scripts,
>> (including images) and embed them in my SWT java app?
> [...]
>
> I don't know of one, but I have used java.lang.Process to run other
> command line applications. "The ProcessBuilder.start() and Runtime.exec
> methods create a native process and return an instance of a subclass of
> Process that can be used to control the process and obtain information
> about it."
>
> <http://java.sun.com/javase/6/docs/api/java/lang/Process.html>
[...]

I think that is the best approach, at least to start with. I know more about
R than Java. I would just start the R process from Java, passing the name of
a file containing an R script on the command line, wait for the R process to
end, then read output from files (with standard names) generated by the R
script. This would avoid using stdin, stdout, and leave you free to make R
generate multiple text files, bitmaps and PS files for you to display in
Java.

The R script would be generated in your Java app, and written to file, and
could contain user defined R functions as well as your own 'wrapper' code to
output files, etc. You won't need to understand anything more complicated
than 'Appendix B: Invoking R' in the manual 'An Introduction to R'. Apart
from R and Java themselves of course.

Graham

Patricia Shanahan

unread,
Feb 20, 2009, 3:26:05 PM2/20/09
to

I use a similar strategy to access Matlab operations from a Java
program, using ProcessBuilder and File's createTempFile method.

Patricia

Mark Space

unread,
Feb 20, 2009, 4:40:24 PM2/20/09
to
Graham Jones wrote:

>
> I think that is the best approach, at least to start with. I know more
> about R than Java. I would just start the R process from Java, passing
> the name of a file containing an R script on the command line, wait for
> the R process to end, then read output from files (with standard names)
> generated by the R script. This would avoid using stdin, stdout, and
> leave you free to make R generate multiple text files, bitmaps and PS
> files for you to display in Java.


This seems a bit sub-optimal to me, since the file names appear to be
hard-coded in the Java program, and maybe the R program too.

It might be a tad more flexible to pass the names of the files created
to Java via stdout. That way you can decide to put the files anywhere
you want, and the Java program just reads the file names at runtime.
You could also pass the filenames to create to the R program on its
command line at runtime, although letting the R program pick a random
temp name seems more flexible to me.

Just a thought....

Roedy Green

unread,
Feb 20, 2009, 6:13:16 PM2/20/09
to
On Thu, 19 Feb 2009 09:23:52 -0800 (PST), Nicros <bcc...@gmail.com>
wrote, quoted or indirectly quoted someone who said :

> R scripts
what are they?
--
Roedy Green Canadian Mind Products
http://mindprod.com

One path leads to despair and utter hopelessness. The other,
to total extinction. Let us pray we have the wisdom to choose correctly.
~ Woody Allen .

Arne Vajhøj

unread,
Feb 20, 2009, 9:02:12 PM2/20/09
to
Roedy Green wrote:
> On Thu, 19 Feb 2009 09:23:52 -0800 (PST), Nicros <bcc...@gmail.com>
> wrote, quoted or indirectly quoted someone who said :
>> R scripts
> what are they?

Most likely:
http://en.wikipedia.org/wiki/R_(programming_language)

Arne

Arne Vajhøj

unread,
Feb 20, 2009, 9:05:25 PM2/20/09
to

A quick read about R state that it can be called from C.

So:
Java code---(JNI call)---C code---(some call)---R
should be possible.

Arne

0 new messages