R Meets Java: An Absolute Beginners’ Introduction
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
- Start a JVM
- Let Java know where it can find the .class file you want to run
- Create an instance of the class
- 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)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.