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(){
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!
java r singleton jri
Chris
source share