Suppose that you have got a Java class which has got some variables with data types int, short, long, float, double and String. This class also includes some arrays of types int[], double[], ..., String[]. Of course it may include some functions, constructors or anything else. But we don't care about this for now. How about passing this class with its publicly defined variables to R? Yeah! It is possible in its last submitted revision.
Have a look at the Java class below:
class TestClass { public int i = 9; public float f = 10.0f; public double d = 3.14; public boolean b = true; public String s = "test"; }
This class simply includes five publicly defined variables with basic data types. Our other class inherits the TestClass and defines some additional arrays:
class TestClassWithArrays extends TestClass { public int[] ia = new int[]{1, 2, 3, 4, 5}; public double[] da = new double[]{1.0, 2.0, 3.0, 4.0, 9.9, 10.1}; public String[] sa = new String[]{"One", "Two", "Three"}; public boolean[] ba = new boolean[]{true, true, false}; }
Ok, they are very simple but there is no reason those classes not to have any methods. Whatever those classes have methods, we consider them as data structures.
Lets pass this data structure to R:
TestClassWithArrays tcwa = new TestClassWithArrays(); JavaObject jo = new JavaObject("tcwa", tcwa); RCaller rcaller = new RCaller(); rcaller.setRscriptExecutable("/usr/bin/Rscript"); rcaller.cleanRCode(); rcaller.addRCode(jo.produceRCode()); rcaller.runAndReturnResult("tcwa");
Well, if there is no expection we have the results in a R list named "tcwa". This R list includes all of the elements that included in TestClassWithArrays and TestClass with their values.
This is an example of proof, the related @Test method is ready for browsing in the Google Code:
@Test public void TestClassWithArrays() throws IllegalAccessException, IOException { TestClassWithArrays tcwa = new TestClassWithArrays(); JavaObject jo = new JavaObject("tcwa", tcwa); RCaller rcaller = new RCaller(); rcaller.setRscriptExecutable("/usr/bin/Rscript"); rcaller.cleanRCode(); rcaller.addRCode(jo.produceRCode()); rcaller.runAndReturnResult("tcwa"); int[] expectedIntArray = rcaller.getParser().getAsIntArray("ia"); for (int i = 0; i < tcwa.ia.length; i++) { assertEquals(expectedIntArray[i], tcwa.ia[i]); } double[] expectedDoubleArray = rcaller.getParser().getAsDoubleArray("da"); for (int i = 0; i < tcwa.da.length; i++) { assertEquals(expectedDoubleArray[i], tcwa.da[i], delta); } String[] expectedStringArray = rcaller.getParser().getAsStringArray("sa"); for (int i = 0; i < tcwa.sa.length; i++) { assertEquals(expectedStringArray[i], tcwa.sa[i]); } }
It is shown that in examples, in R side, we can access to elements with their original names that defined in the Java class. That sounds good.
Finally, we can pass our Java objects with defined contents. This use of RCaller narrows the code of addDoubleArray, addIntArray and reduce all of them to simple command of
JavaObject jo = new JavaObject("tcwa", tcwa); . . . rcaller.addRCode ( jo.produceRCode() );
It is simplicity...