Search code examples
rrjava

Where do I start with rJava?


I am not a Java programmer. I program R and C++. I have some java code that I want to include in an R package. The basics of the program are this:

  1. Read data from standard input.
  2. Run a MCMC chain.
  3. output to a file.

I want to convert this to R where I can run the program from R. I am familiar with Rcpp package and am used to some of the conveniences of it. Where do I start with the rJava package to learn to use this code.

Specifically i have the following questions.

  1. How to I transfer data to java from R, e.g. numeric vector, factor, etc.
  2. How do I run methods of a class?
  3. How do I include java code in a package?

The rJava documentation is not very helpful. Does anyone have a tutorial on this?


Solution

  • There's a "simple" way to do this and a somewhat harder way to do this. I'm a simple man so I lean toward the simple solution:

    myCommand <- paste("/path/to/java", argument1, argument2, sep=" ")
    system(shQuote(myCommand))
    

    Then read in the output file using whatever R function makes sense.

    The somewhat harder solution is to edit your Java code so it doesn't read from stdin, but gets passed a vector, or other Java object. I can't really generalize about how to alter your Java code, but if the Java function ultimately needs to be fed a vector, you'd do it something like this:

    .jinit()
    v <- .jnew("java/util/Vector")
    rVector <- as.character(1:10)
    addToV <- function( item ){
      v$add( item )
    }
    sapply(rVector, addToV)
    

    I always find dealing with types in rJava to be a considerable pain, as you can see above.

    One tip that will save you considerable time is this: When you have a Java object created in rJava you can figure out its methods by typing the name, a dollar sign, and then hit tab. So using the v object created above type "v$" and you should get this:

    1> v$
    v$add(                 v$hashCode()           v$contains(            v$size()               v$elementAt(           v$capacity()           v$containsAll(         v$firstElement()       v$removeElement(       v$iterator()           v$wait()
    v$get(                 v$clone()              v$isEmpty()            v$toArray()            v$remove(              v$ensureCapacity(      v$removeAll(           v$insertElementAt(     v$removeElementAt(     v$listIterator()       v$getClass()
    v$equals(              v$indexOf(             v$lastIndexOf(         v$toArray(             v$elements()           v$trimToSize()         v$retainAll(           v$lastElement()        v$setElementAt(        v$listIterator(        v$notify()
    v$toString()           v$clear()              v$addAll(              v$addElement(          v$set(                 v$subList(             v$copyInto(            v$removeAllElements()  v$setSize(             v$wait(                v$notifyAll()
    1> v$
    

    Good luck, and be sure and yell if you have specific snags.