0

I'm currently using JRI, the Java package in order to use some of the functions provided by R.

Let's say, my R command goes like this:

qqnorm(sql_data[["LifeExpectancy"]],
main="Life Expectancy")

If I put this into my REngine.eval() function, it crashes, because I'm supposed to enter the whole command into one "eval()" command. It works in my normal R command line though...

The major problem is, that it's hard to predict for me, whenever the next order is split up into multiple lines or not. So, how may I fix that problem?

I thought about counting the brackets, but the number of brackets isn't always even. (Let's say, some brackets are contained in Strings) Any other ideas?

Edit: I think, my point is not obvious enough. Try this out:

System.out.println(re.eval("a <- c(3,4)"));
System.out.println(re.eval("a"));

System.out.println(re.eval("b <- c(3,4")); 
System.out.println(re.eval(",5)"));
System.out.println(re.eval("b"));

You'll see, that for the first two commands, a, you receive

[REAL* (3.0, 4.0)] 
[REAL* (3.0, 4.0)]

But the rest, b, goes like this:

[NULL ]
[NULL ]
null

It's a problem with R in general; usually this works, but not in JRI!

The_F
  • 45
  • 4
  • Please show the line of java code that gives the error and show details of the error. – nicola Oct 21 '14 at 13:30
  • It's an error in R, not in Java. – The_F Oct 21 '14 at 13:35
  • Please, post an error messagge wherever you receive it. BTW, didn't the error arise by calling `REngine.eval()`? – nicola Oct 21 '14 at 13:41
  • `REngine.eval()` needs a valid R statement. Why are you breaking into two or more calls? Are you trying to read an `.R` file line by line and executing each line through `REngine.eval()`? If that's the case, just call `REngine.eval("source('rfile.R')")` and the script will be executed. – nicola Oct 21 '14 at 14:00
  • This is possible, whenever using a normal R console. It's just the way, how I get my R commands and I can't really influence it. If you try that out in a normal R command line, it works perfectly. But in JRI, it doesn't! And that's my problem! – The_F Oct 21 '14 at 14:51

1 Answers1

0

First of all, looking at System.out.println(re.eval("b <- c(3,4")); i noticed that your error is in R, (typo - missed the second bracket), see the following code, it works:

package stackoverflow;

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
/**
 *
 * @author yschellekens
 */
public class StackOverflow {  

   public static void main(String[] args) throws Exception {
    String[] Rargs = {"--vanilla"};
     Rengine rengine = new Rengine(  Rargs, false, null);
        rengine.eval("b <- c(3,4)");
          REXP result=  rengine.eval("b[1]");
      System.out.println(result.asDouble());
   }
}

Java output:

run:
3.0

Notice that you can't pass straight into System.out.println an Rengine object, you need to convert it into an REXP object first (see in code).

Also your first example

qqnorm(sql_data[["LifeExpectancy"]],
main="Life Expectancy") 

includes inner " " and hence another reason for a possible crash

Please use : ' ' as following:

 qqnorm(sql_data[['LifeExpectancy']],
    main='Life Expectancy') 
Yehoshaphat Schellekens
  • 2,305
  • 2
  • 22
  • 49