dcsimg
June 17, 2018
Hot Topics:

Using Java Strings and the Swing API in R

  • February 3, 2017
  • By Deepak Vohra
  • Send Email »
  • More Articles »

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

Using Strings was introduced by creating a new object from the java.lang.String class and a supplied String as constructor arg.

> s <- .jnew("java/lang/String", "Hello World!")

The jstrVal function could be used to output the String value.

> .jstrVal(s)
[1] "Hello World!"
>

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."

> .jcall(s,"I","indexOf","Hello")

The return value is an int with a value of 0, indicating the index of "Hello" in "Hello String!"

[1] 0

Alternatively, the short form could be used to call the indexOf(String str) method as follows to output int 0.

> s$indexOf("Hello")
[1] 0

The R Console output from the preceding method calls is as follows, as shown in Figure 1.

R Console output from the preceding method calls
Figure 1: R Console output from the preceding method calls

As an example, call the static method valueOf(int i) in the String class.

> J("java.lang.String")$valueOf(10)

The int value as a String is returned.

[1] "10.0"
>

The R Console output from the preceding command is shown (see Figure 2).

The R Console output from the preceding command
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.

> .jcall(s,"Ljava/lang/String;","concat",s)

The value output is the Strings concatenated.

[1] "Hello World!Hello World!"
>

The output from the preceding function call is shown in R Console, as referenced in Figure 3.

The output from the preceding function call, shown in R Console
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.

> String <- J("java.lang.String")
>

Or, a new String object reference could be created by using the "new" keyword.

> s <- new(String)

Subsequently, call the concat method on the String object reference to concatenate the "Hello" String literal to the String object "s".

> s <-s$concat("Hello")

Print the String object "s".

> print(s)

A String, "Hello", should get output.

[1] "Hello"
>

The .jequals(a, b, strict = FALSE) function may be used to find if two objects are equal. The .jequals allows a mixed comparison of non-Java objects is "strict" is FALSE, the default. For example, create a String object reference "s" as before but using String ""Hello" instead of "Hello World!"

> s <- .jnew("java/lang/String", "Hello")

Find if the "s" object reference is equal to the R object "Hello".

> .jequals(s, "Hello")

An output of TRUE indicates the objects are equal.

[1] TRUE

In the preceding object comparison, the scalar R object "Hello" is converted to a String object. But, if "strict" is set to TRUE, the result would be otherwise because the R scalar "Hello" is not converted to a String.

> .jequals(s, "Hello", strict=TRUE)
[1] FALSE

If two String objects represent the same Java object, the output would be TRUE even with strict=TRUE. As an example, create another Java object reference called "t" from the object reference s".

> t <- s

Compare "t" and "s" with strict=TRUE.

> .jequals(s, t, strict=TRUE)

An output of TRUE is generated, indicating that the Java objects are equal.

[1] TRUE

The == binary comparison operator also could be used to compare two Strings. As an example, compare the String object "s" with R scalar "Hello".

> s=="Hello"

A value of TRUE is returned. The == operator calls the non-strict version of the .jequals method.

[1] TRUE
>

Creating a Swing Application

A Swing application could be created similarly by calling the .jnew an .jcall functions. For example, create a java.awt.Frame class object with a "Hello World" constructor arg and subsequently call the setVisible method on the Frame object with the boolean arg TRUE to show the Window object.

> f <- .jnew("java/awt/Frame", "Hello World")
> .jcall(f,, "setVisible", TRUE)

The preceding function calls do not generate any output. This is shown in Figure 4.

No output is generated
Figure 4: No output is generated

But, a Window object with the "Hello World" title gets displayed (see Figure 5).

A Window object with the "Hello World" title is displayed
Figure 5: A Window object with the "Hello World" title is displayed

Similarly, other GUI components could be added to the Frame. As an example, add a Button object with the "OK" label to a Window object with the "Hello" title.

> f <- .jnew("java/awt/Frame", "Hello")
> b <- .jnew("java/awt/Button", "OK")
> .jcall(f, "Ljava/awt/Component;", "add",
   .jcast(b, "java/awt/Component"))

Subsequently, pack and show the Window object.

> .jcall(f,, "pack")
> .jcall(f,, "setVisible", TRUE)

The .jcall function call to add the Button to the Frame returns a Java Object, as shown in Figure 6.

A Java Object is returned
Figure 6: A Java Object is returned

Subsequently, a Window with a button is displayed (see Figure 7).

A Window with a button is displayed
Figure 7: A Window with a button is displayed

Finding if an Object is an Instance of a Class

The .jinstanceof function is used to find if a given Java object is an instance of a given Java class. With "o" as the Java object and "cl" as the Java class, the function could be used with either of the following notations.

o %instanceof% cl
.jinstanceof( o, cl )

As an example, find if an object is an instance of class Double. First, create a Java class reference for the Double class using J, the high level API for Java. Subsequently, create a Double object reference from double "1.2".

> Double <- J("java.lang.Double")
> d <- new( Double, "1.2" )

Find if the Double object reference "d" is an instance of the java.lang.Double class.

> d %instanceof% "java.lang.Double"

The output should be as follows.

[1] TRUE

Find if the "d" object reference is an instance of the java.lang.Number class. And, the output is again TRUE.

> d %instanceof% "java.lang.Number"
[1] TRUE

The "d" object reference is definitely not an object of type java.lang.Integer, but to verify, use the .jinstanceof function.

> d %instanceof% "java.lang.Integer"

The output is FALSE.

[1] FALSE

The "Double" class reference also could be used instead of the fully qualified Java class java.lang.Double.

> d %instanceof% Double
[1] TRUE

Output from the preceding function calls in R Console is shown in Figure 8.

Output from the preceding function calls in R Console
Figure 8: Output from the preceding function calls in R Console

In the next article, "Using the rJava R Package to Do More," we shall explore some more features of Java with R, such as checking for Exceptions, using arrays and collections, casting a Java object, and comparing Java references.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date