Finally I have implemented the method runAndReturnResultOnline() for running sequential commands in a single process. What does this stand for? Let me give an example to explain this:
Suppose that you want to perform a simulation study to measure the success of your new procedure. For this, you decide to draw random numbers from a distribution and calculate something and handle the results in Java. RCaller creates Rscript processes for each single iteration. This cause to too many operating system calls.
Latest release of RCaller includes the method for this. Lets have a look at the Test file:
@Test public void onlineCalculationTest() { RCaller rcaller = new RCaller(); rcaller.setRExecutable("/usr/bin/R"); rcaller.cleanRCode(); rcaller.addRCode("a<-1:10"); rcaller.runAndReturnResultOnline("a"); assertEquals(rcaller.getParser().getAsIntArray("a")[0], 1); rcaller.cleanRCode(); rcaller.addRCode("b<-1:10"); rcaller.addRCode("m<-mean(b)"); rcaller.runAndReturnResultOnline("m"); assertEquals(rcaller.getParser().getAsDoubleArray("m")[0], 5.5, 0.000001); rcaller.cleanRCode(); rcaller.addRCode("a<-1:99"); rcaller.addRCode("k<-median(a)"); rcaller.runAndReturnResultOnline("k"); assertEquals(rcaller.getParser().getAsDoubleArray("k")[0], 50.0, 0.000001); } }
In first stage,we are creating an integer vector and getting the first element. In the second one, we are creating the same integer vector with a different name and calculating the arithmetic mean. In the last one, we are recreating the vector a and getting the median, which is equal to 50.
This example uses the same RCaller object. In first stage, the R executable file (it is /usr/bin/R in my Ubuntu Linux) is created once. In second stage the same R file is used and no longer processes are created again. In this stage, the vector a is accessible and still remains alive. At the last stage, b is alive again and a is recreated. So this example does not cause the R to open and close three times but only once.
This modification speeds up the RCaller, but it can be still considered as slow.
However, it is still easy to implement and much more faster than the previous implementation.
Have Fun!