R Meets Java: An Absolute Beginners’ Introduction

March 10, 2012
By

(This article was first published on Gage Theory » R, and kindly contributed to R-bloggers)

My guess is R is most commonly integrated with C/C++ to handle heavy-duty computing. (thanks in no small part due to the productivity of Dick Eddlebuttle!) That said, if you’re like most statisticians and physical scientists and aren’t already a programming superstar, Java is a good language to use in conjunction with R for a few reasons. (Fair Warning: hardcore C programmers will sneer at Java and corporate software engineers will mock you for not using C#)

  • Java is the language used to create Weka and Hadoop
  • Java provides some performance improvements over Python and eliminates some of the C/C++ headaches like garbage collection
  • Java is portable (if it runs on your JVM it should run anywhere!)
  • The rJava package provides at least as good (and probably better) of an interface to Java as .Call() provides for C/C++
    • Although probably not quite as good and well-maintained as the Rcpp integration

Get Java

Here’s how to get started from scratch. First you need Java! You can download the Java Runtime Environment (JRE) and Java Development Kit (JDK) here.

The JRE is probably already installed on your machine. To check if you have the JRE installed successfully you can go to the command line and run:

$ java -version

And it should return the version of the JRE you have installed. To check if you have the JDK installed successfully you can go to the command line and run:

$ javac –version

If you’re a Windows user and receive a “command not recognized error” double check that you have Java added to your Path and ClassPath environment variables.

Get a Development Environment

You’ll also probably want an Integrated Development Environment (IDE) for Java. Yes, you can write a Java program in Notepad just like you can write an R program in a notepad. But, just like RStudio or Revolution Analytics IDEs’ makes working with R easier, a Java IDE will make working with the language faster and friendlier. The 2 most popular are Eclipse and NetBeans but anything will work.

Work with Java

Let’s write the standard “Hello World!” program in 2 steps with a twist. To keep it simple, I’ll show what this looks like using a notepad and the command line to avoid any confusion caused by the barrage of details and magic the Eclipse and NetBeans IDEs bring.

Mashup R and Java

Next fire up R and load the rJava package. The helloJavaWorld vignette and Darren Wilkinson offer good examples on how to get started for people with some experience with Java. We’ll follow the same steps as the helloJavaWorld vignette, but break it down into more detail. There are a few simple steps to follow:

  1. Start a JVM
  2. Let Java know where it can find the .class file you want to run
  3. Create an instance of the class
  4. Then invoke the method that will return the result

Here’s what it looks like:

# Initialize a JVM
.jinit()
# Add a path to the class file
.jaddClassPath(path="C:/Users/username/workspace/src")
# Create an instance of the RHelloWorld class
hello.obj <- .jnew("RHelloWorld")
# Invoke the sayHello method and return results as a "S" = string
result <- .jcall(hello.obj, "S", "sayHello") # .jcall(class, return type, method)

To leave a comment for the author, please follow the link and comment on his blog: Gage Theory » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.