Renjin : engine.eval returned value

123 views Asked by At

In Renjin, what is the actual value returned by :

engine.eval("...")

For example:

df <- data.frame( 1 ) 
df[["X1"] <- df[["X1"]] * 10

returns a DoubleArrayVector with the single 10 value. I guess this is the last computation.

df <- data.frame( 1 ) 
df[["X1"] <- df[["X1"]] * 10
print(df)

returns a data.frame (ListVector...)

Is there any way to make the eval returns the data.frame df in the example above ?

Thx.

1

There are 1 answers

0
akbertram On

Renjin applies the all the normal rules of R evaluation, and a sequence of expressions will indeed evaluate to the value of the last expression.

If you want the value of df, then you can write in Java:

ListVector df = (ListVector)engine.eval("df");

Or:

ListVector df = (ListVector)engine.get("df"); 

In R, a data.frame is simply a list of columns of class "data.frame". You can see this by running the following in either GNU R or Renjin:

> data(iris)
> str(iris)
'data.frame':   150 obs. of  5 variables:
$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species     : Factor w/ 3 levels "setosa","versicolor",...

From Java, you can access these columns using the ListVector's methods:

ListVector df = (ListVector)engine.get("df"); 
Vector sepalLength = df.getElementAsVector(0);
Vector sepalWidth = df.getElementAsVector(1);

// Find the sepalLength and sepalWidth of the fourth observation
double sepalLength4 = sepalLength.getElementAsDouble(3);
double sepalWidth4 = sepalWidth.getElementAsDouble(4);