franchong
franchong

Reputation: 1

How to call String[][] in R using rjava

I am preparing an R wrapper for a java code that I didn't write myself (and in fact I don't know java). I am trying to use rJava for the first time and I am struggling to get the .jcall right.

Here is an extract of the java code for which I write a wrapper:

public class Model4R{

[...cut...]

public String[][] runModel(String dir, String initFileName, String[] variableNames, int numSims) throws Exception {

[...cut...]

dir and initFileName are character strings for the directory and file name with initial conditions, variable names is a list of character strings that I would write like this in R: c("var1", "var2", "var3", ...) and can be of length from one to five. Finally, numSim is an integer.

Here is my tentative R code for a wrapper function:

runmodel <- function(dir, inFile, varNames, numSim){

hjw <- .jnew("Model4R")

out <- .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir), as.character(inFile), as.vector(varNames), as.integer(numSim))

return(out)

}

The error in R is:

Error in .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir),
: method runModel with signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)[[Ljava/lang/String not found

I suspect that the JNI type isn't correct for String[][]. Anyhow, any help that could direct me towards a solution would be welcome!

Upvotes: 0

Views: 455

Answers (1)

Lizzie Silver
Lizzie Silver

Reputation: 293

You're missing a semicolon at the end of the JNI for String[][] - it should be "[[Ljava/lang/String;". Also, I think you need to call .jarray instead of as.vector on varNames. The R error is telling you that rJava thinks the class of the third argument is Ljava/lang/String; instead of [Ljava/lang/String;.

Upvotes: 1

Related Questions