2

I could call R from Java and call Java function from R successfully both. However, when I'm trying to use rJava JRI to call Java from R, and Java code instance new REngine. It failed reports R is already initialized and then quit R directly.

My class:

public class testRJava {
    public void testOK(){
        System.out.println("test this funciton could be called OK");
    }
    public void callSample(){
        Rengine re = Rengine.getMainEngine();
        if(re == null){
            re=new Rengine (new String [] {"--vanilla"}, false, null);
            if (!re.waitForR())
            {
                System.out.println ("Cannot load R");
                return;
            }
        }
        // print a random number from uniform distribution
        System.out.println (re.eval ("runif(100)").asDouble());
        re.end();
    }
}

then, I call the Jar from R

> b <- .jnew("testRJava")
> b$testOK()
test this funciton could be called OK
> b$
b$callSample()  b$main(         b$wait()        b$toString()    b$getClass()    b$notifyAll()
b$testOK()      b$wait(         b$equals(       b$hashCode()    b$notify()
> b$callSample()
R is already initialized
[root@ home]#

Any one know why? Any solution about this error ?

Dawei
  • 41
  • 5
  • Java -cp could run the jar successfull as below [lib]# java -cp "/usr/lib64/R/library/rJava/jri/REngine.jar:/usr/lib64/R/library/rJava/jri/JRI.jar:/usr/lib64/R/library/rJava/jri/JRIEngine.jar:./rjava.jar" testRJava 0.9858369643334299 [REAL* (1.0, 3.0, 2.0, 5.0)]result:[STRING* ("-0.04885", "-1.680110", "-1.292430", "-0.748090", " 0.008118", " 0.642391", " 1.174235")] histval$mids:[REAL* (-1.75, -1.25, -0.75, -0.25, 0.25, 0.75, 1.25, 1.75, 2.25, 2.75)] histval$counts:[INT* (6, 6, 20, 21, 17, 17, 7, 4, 1, 1)] – Dawei Sep 03 '15 at 13:47

1 Answers1

1

library(rJava)

.jinit(".")

.jengine(TRUE)

[1] "Java-Object{Thread[Thread-0,5,main]}"

b <- .jnew("testRJava")

b$callSample()

Community
  • 1
  • 1
Dawei
  • 41
  • 5