R/Java code not working as expected

时间:2017-08-04 12:22:05

标签: java r

I am trying to print an R dataframe in Java but I am not sure of the datatype to be used for that.

I am getting a null value if I use STRING or STRING ARRAY. This is my code:

public static void main(String a[]) {

    // Create an R vector in the form of a string.
    String javaVector = "c(1,2,3,4,5)";
    String name="c('a','b','c','d','e')";

    // Start Rengine.
    Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);

    // The vector that was created in JAVA context is stored in 'rVector' & 'names'  which is a variable in R context.
    engine.eval("rVector=" + javaVector);
    engine.eval("names="+name);

    //Calculate MEAN of vector using R syntax.
    engine.eval("meanVal=mean(rVector)");
    //Making a data frame in R.
    engine.eval("data=data.frame(rVector,names)");
    engine.eval("initial=paste('aakash','singhal')");
    String myname = engine.eval("initial").asString();
    //TRYING TO PRINT THE DATA FRAME (NOT SURE OF THE DATA TYPE)
     REXP datafinal = engine.eval("data");
    //RETRIEVE MEAN VALUE
    double mean = engine.eval("meanVal").asDouble();
    //Print output values
    System.out.println("Mean of given vector is=" + mean);
    System.out.println("Data table is :" + datafinal);
    System.out.println(myname);
}

It prints-

Mean of given vector is=3.0
Data table is :[VECTOR ([REAL* (1.0, 2.0, 3.0, 4.0, 5.0)], [FACTOR {levels=("a","b","c","d","e"),ids=(0,1,2,3,4)}])] 
aakash singhal

edit-

i used RXP so got this new result but not a table.

0 个答案:

没有答案