R from Within Java

R from within Java

Use JRI: http://www.rforge.net/JRI/. It comes bundled with rJava, including some examples of usage.

A very simple example would be like this:

import java.io.*;
import java.awt.Frame;
import java.util.Enumeration;

import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.RMainLoopCallbacks;

public class rJavaTest {

public static void main(String[] args) {

Rengine re=new Rengine(args, false, new TextConsole());
REXP x;
re.eval("print(1:10/3)");
System.out.println(x=re.eval("iris"));
RVector v = x.asVector();
if (v.getNames()!=null) {
System.out.println("has names:");
for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) {
System.out.println(e.nextElement());
}
}

if (true) {
System.out.println("Now the console is yours ... have fun");
re.startMainLoop();
} else {
re.end();
System.out.println("end");
}
}
}

Call Java from R

I'd go following way here.

  1. Process CSV file inside R

  2. Save this file somewhere and make sure you know explicit location (e.g. /home/user/some_csv_file.csv)

  3. Create adapter class in Java that will have method String processFile(String file)
  4. Inside method processFile read the file, pass it to your code in Java and do Java based processing
  5. Store output file somewhere and return it's location
  6. Inside R, get the result of processFile method and do further processing in R

At least, that's what I'd do as a first draft of a solution for your problem.

Update

We need Java file

// sample/Adapter.java
package sample;

public class Adapter {
public String processFile(String file) {
System.out.println("I am processing file: " + file);
return "new_file_location.csv";
}

public static void main(String [] arg) {
Adapter adp = new Adapter();
System.out.println("Result: " + adp.processFile("initial_file.csv"));
}
}

We have to compile it

> mkdir target
> javac -d target sample/Adapter.java
> java -cp target sample.Adapter
I am processing file: initial_file.csv
Result: new_file_location.csv
> export CLASSPATH=`pwd`/target
> R

We have to call it from R

> library(rJava)
> .jinit()
> obj <- .jnew("sample.Adapter")
> s <- .jcall(obj, returnSig="Ljava/lang/String;", method="processFile", 'initial_file')
> s
I am processing file: initial_file
> s
[1] "new_file_location.csv"

And your source directory looks like this

.
├── sample
│   └──Adapter.java
└── target
     └── sample
         └── Adapter.class

In processFile you can do whatever you like and call your existing Java code.

How do you run an R program from Java?

You might want to take a look at these three projects.

  • JRI - Java/R Interface

  • Rserve

  • RCaller

Run R script using JAVA program

You just want to call an external application: wouldn't the following work?

Runtime.getRuntime().exec("Rscript myScript.R"); 

Credit goes to stackoverflow itself

R integration with Java

  1. rServe is a client-server communication, the R code runs on server and client(here java) gets the results, using usual TCP/IP connection (can also run locally).
  2. rJava is more like a API library to R in java. The R code runs on your locally installed R instance.

selecting one of them is based on your needs. If you want to embed a small amount of R code in java then rJava is the correct choice. If you have large amount of R codes in multiple scripts, or if you want them to be exposed as REST API to multiple java clients then rServe is the way to go.

calling R script from java

You just want to call an external application: wouldn't the following work?

Runtime.getRuntime().exec("Rscript myScript.R"); 

How can execute command R CMD Rserve from Java

i get it work changing the way i was trying to invoke the process. The rzwitserloot's answer was really useful to figure this.
I dont know about the internals of "R CMD" command but it dont like to java process builder.
There is other way to invoke Rserver daemon within R console so i tried to do it this way. This works fine:

List<String> args = List.of("/bin/sh", "-c","echo 'library(Rserve);Rserve(args=\"--no-save --slave\");'| /usr/lib/R/bin/R --no-save --slave");
ProcessBuilder pb = new ProcessBuilder(args);
Process p = pb.start();


Related Topics



Leave a reply



Submit