R code in Java working in Linux but not in Windows

89 Views Asked by At

What am I doing?

I am writing a data analysis program in Java which relies on R´s arulesViz library to mine association rules.

What do I want?

My purpose is to store the rules in a String variable in Java so that I can process them later.

How does it work?

The code works using a combination of String.format and eval Java and RJava instructions respectively, being its behavior summarized as:

  1. Given properly formatted Java data structures, creates a data frame in R.
  2. Formats the recently created data frame into a transaction list using the arules library.
  3. Runs the apriori algorithm with the transaction list and some necessary values passed as parameter.
  4. Reorders the generated association rules.
  5. Given that the association rules cannot be printed, they are written to the standard output with R´s write method, capture the output and store it in a variable. We have converted the association rules into a string variable.
  6. We return the string.

The code is the following:

// Step 1
Rutils.rengine.eval("dataFrame <- data.frame(as.factor(c(\"Red\", \"Blue\", \"Yellow\", \"Blue\", \"Yellow\")), as.factor(c(\"Big\", \"Small\", \"Small\", \"Big\", \"Tiny\")), as.factor(c(\"Heavy\", \"Light\", \"Light\", \"Heavy\", \"Heavy\")))");

//Step 2
Rutils.rengine.eval("transList <- as(dataFrame, 'transactions')");

//Step 3
Rutils.rengine.eval(String.format("info <- apriori(transList, parameter = list(supp = %f, conf = %f, maxlen = 2))", supportThreshold, confidenceThreshold));

// Step 4
Rutils.rengine.eval("orderedRules <- sort(info, by = c('count', 'lift'), order = FALSE)");

// Step 5
REXP res = Rutils.rengine.eval("rulesAsString <- paste(capture.output(write(orderedRules, file = stdout(), sep = ',', quote = TRUE, row.names = FALSE, col.names = FALSE)), collapse='\n')");

// Step 6
return res.asString().replaceAll("'", "");

What´s wrong?

Running the code in Linux Will work perfectly, but when I try to run it in Windows, I get the following error referring to the return line:

Exception in thread "main" java.lang.NullPointerException

This is a common error I have whenever the R code generates a null result and passes it to Java. There´s no way to syntax check the R code inside Java, so whenever it´s wrong, this error message appears.

However, when I run the R code in brackets in the R command line in Windows, it works flawlessly, so both the syntax and the data flow are OK.

Technical information

  • In Linux, I am using R with OpenJDK 10.

  • In Windows, I am currently using Oracle´s latest JDK release, but trying to run the program with OpenJDK 12 for Windows does not solve anything.

  • Everything is 64 bits.

  • The IDE used in both operating systems is IntelliJ IDEA 2019.

Screenshots

Linux run configuration:

Linux run configuration

Windows run configuration:

Windows run configuration

0

There are 0 best solutions below