O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To use Java objects in R, follow these steps:

  1. From within R, start the JVM, check the Java version, and set classpath:
> .jinit() 
 
> .jcall("java/lang/System", "S", "getProperty", "java.runtime.version") 
[1] "1.8.0_25-b17" 
 
> .jaddClassPath(getwd()) 
 
> .jclassPath() 
[1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rJava/java" 
[2] "/Users/sv/book/Chapter11"  => my working directory 
  1. Perform these Java string operations in R:
> s <- .jnew("java/lang/String", "Hello World!") 
> print(s) 
 [1] "Java-Object{Hello World!}" 
 
> .jstrVal(s) 
[1] "Hello World!" 
 
> .jcall(s,"S","toLowerCase") 
[1] "hello world!" 
 
> .jcall(s,"S","replaceAll","World","SV") 
[1] "Hello SV!"
  1. Perform these Java vector operations:
> javaVector ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required