Java-R: R bridge "JRI" error already initialized

I use JRI as a calculation sheet for statistics from Java. R-calculations are required from time to time, but not too often. So I decided to create a wrapper method for computation that creates a new instance of REngine and also closes it to the end. When you first call the method, everything works like a charm. Unfortunately, calling it a second time causes the error "R is already initialized."

Initialization:

private static Rengine createEngineInstance(){ //Initialise R Engine. Rengine re=new Rengine (new String [] {"--vanilla"}, false, new CallbackListener()); //Wait until REngine-thread is ready if (!re.waitForR()) { System.err.println ("Cannot load R. Is the environment variable R_HOME set correctly?"); System.exit(1); } return re; } 

Wrap Method:

 public static void performR(){ //Create instance of R engine Rengine re = createEngineInstance(); //Perform some R operations re.eval("..."); re.end(); } 

Obviously, the REngine instance is not completed correctly. So I need to know: 1) Is it possible to stop REngine and create a new instance later? How does it work correctly? I know that it is not possible to run multiple R threads at the same time as the JRI, but that is not what I am aiming for. 2) If this is not the case, I would create one instance using the Singleton template. How can I ensure in this case that the R session closes when the program terminates?

Your help is much appreciated! Thanks!

+7
java r singleton jri
source share
2 answers

according to JRI Javadoc ....

Only one instance of Rengine can be used, and the Rengine.end () function has some problems ...

you should not use such code

 Rengine engine = new Rengine(new String[] {"--vanilla"}, false, null); 

you can use rengine with this code

 Rengine engine = Rengine.getMainEngine(); if(engine == null) engine = new Rengine(new String[] {"--vanilla"}, false, null); 
+7
source share

A very old question, but I would add that considering RServe as an alternative is often attractive. The setup is less involved (in my opinion), and the interface is almost identical.

Detailed tutorial given here .

This has the added benefit that if you ever want to make R calls from parallel Java threads, you can do it.

+2
source share

All Articles