In the first article, » Getting Started with R Using Java ,» we started with installing R and installing the rJava package. We discussed initializing the JVM, setting the classpath, creating a Java object, and calling a Java method. In this tutorial, we shall discuss the rJava package to use Java Strings and the Swing Java API from R. This tutorial has the following sections:
Using Strings was introduced by creating a new object from the java.lang. String class and a supplied String as constructor arg.
The jstrVal function could be used to output the String value.
Any of the String class methods could be called by using the String object «s». As an example, call the instance method indexOf(String str) to find the index of a substring such as «Hello. »
The return value is an int with a value of 0, indicating the index of «Hello» in «Hello String! »
Alternatively, the short form could be used to call the indexOf(String str) method as follows to output int 0.
The R Console output from the preceding method calls is as follows, as shown in Figure 1.
Figure 1: R Console output from the preceding method calls
As an example, call the static method valueOf(int i) in the String class.
The int value as a String is returned.
The R Console output from the preceding command is shown (see Figure 2).
Figure 2: The R Console output from the preceding command
As another example, call the concat(String str) method in the java.lang. String class using the.jcall(obj, returnSig = «V», method, …) function, supplying the object reference as the «s» String object used from the start in this tutorial, a return type signature for the String class, and the method to call as «concat» with the «s» object reference as the concat method arg.
The value output is the Strings concatenated.
The output from the preceding function call is shown in R Console, as referenced in Figure 3.
Figure 3: The output from the preceding function call, shown in R Console
A new String class reference could be created by using the J high level API, as follows.