rJava JRI Error R is already initialized (R Call java(new Rengine in Java ))

rJava JRI Error R is already initialized (R Call java(new Rengine in Java ))

我可以从 Java 调用 R 并从 R 成功调用 Java 函数。 但是,当我尝试使用 rJava JRI 从 R 调用 Java 和 Java 代码实例新 REngine 时。报告失败 R已经初始化 然后直接退出R。

我的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();
    }
}

然后,我从 R 调用 Jar

> 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]#

有人知道为什么吗?关于此错误的任何解决方案?

library(rJava)

.jinit(".")

.jengine(TRUE)

[1]“Java-对象{Thread[Thread-0,5,main]}”

b <- .jnew("testRJava")

b$callSample()