In this final part of the rJava series, learn about checking for exceptions, using arrays, and other important tasks.
In “ Getting Started with R Using Java ,“ you learned how to initialize the JVM to use Java with R, set the classpath, and invoke a Java object you created. In “ Using Java Strings and the Swing API in R ,“ you learned to use Strings, create a Java Swing application, and find if an object is an instance of a class. In this tutorial, we shall discuss using the rJava R package for exception handling, arrays, collections, casting and comparing Java objects and also to get System properties. This tutorial has the following sections.
As mentioned before, the .jnew(class, …, check=TRUE, silent=!check) function has the provision to check for exceptions by setting the check arg to TRUE , which is the default. Because the „silent“ arg is !check by default, an error message gets output if an error exists. As an example, create a new Java object from the non-existent class hello/world with check=TRUE.
The following output indicates an error.
The following functions are provided to check for exceptions and throw exceptions.
If check=FALSE in the .jnew function call, an error message is not generated, the exception is not cleared, and the exception object may be obtained. As an example, call.jnew with a non-existent class and check=FALSE. No error message is generated.
To find if a Java exception was raised, call the .jgetEx function on the implicit exception object e and, if the exception object is not null, output a message to indicate that an exception was raised.
The exception message may be output.
The .jcheck function clears the exception and, if called twice consecutively, an output of FALSE or 0 indicates the same.
The rJava package provides the .jarray function to create a Java array reference from a vector or a list of Java references.
The contents.class is used only if a list of Java references is used to create an array and indicates the Java class to use for the objects in the array. The default is the java.lang. Object class.
For example, the following creates an array of ints 1 to 5.
The .jevalArray returns the contents or elements of an array. For example, output the elements in array „a“. The ints 1 to 5 are output.
The .jarray returns a Java array reference jarrayRef. As another example, create an array from a vector with the vector being created using the R function „c“.
Subsequently, evaluate the array using .jevalArray to output the elements of the array.
Collections could be created just as any other Java objects. As an example, create a class reference for the java.util. Vector class using the J high-level API for Java.
Create a Java object reference called „v“ from the class reference using the „new“ operator.
Add elements to the Vector object.
Output from the preceding function calls is shown in R Console (see Figure 1).
Figure 1: Output from the preceding function calls, shown in R Console
Print the first element using the Vector class method firstElement() .
Output the size of the array with the size() method.
Output the Object reference for the array using the toArray()) method of Vector .
Output the Vector class as a Sting using the toString() method.
Output the last element in the Vector reference with the lastElement() method.
Find if the Vector is empty with the isEmpty() method.
Remove an element with the remove() method.
Subsequently, if the first element is printed, „item2“ is output and not „item1“.
Remove all elements with removeAllElements() method in the Vector class.
Subsequently, find if the Vector object is empty and TRUE is returned.
As another example of Collections create an object „h“ of type java.util. HashMap.
Put a few key/value pairs in the HashMap .
Print the size of the HashMap object using the size() method.
Output the values in the HashMap using the values() method in HashMap .
Get the value for a key with the get() method.
The .jcast function casts a Java object to another class and has the following usage.
The „obj“ is the Java object to cast and the class is the class to cast to. If check=TRUE , a check is performed using the.jinstanceof function to verify that the object is an instance of the class. If check=FALSE , which is the default, a check is not performed and an error is generated if the object is not an instance of the class. The convert.array =FALSE does not convert an array object to a jarrayRef reference. As an example, create a Vector class object called „v“.
Cast the object to class java/lang/Object using .jcast .
Output from the preceding function calls is shown in R Console, as shown in Figure 2.
Figure 2: Output from the preceding function calls, shown in R Console
The system properties may be output using the .jcall function by supplying the system property to get as an arg to the getProperty method of java/lang/System with the value returned as a String .
Java references could be compared by using binary comparison operators such as ==, , =. As an example, create three objects of type Double, two of which would be from the same double value 10.0 and one from double 5.0.
Find if d1 is equal to d2 with the == binary operator.
The output is as follows.
Compare if d1 is less than d2 with = .
The output is as follows.
Find if d1 is less than d2 with
© Source: http://www.developer.com/java/ent/using-the-rjava-r-package-to-do-more.html
All rights are reserved and belongs to a source media.