you can handle all of the variables defined in an environment with RCaller.
Now we suppose you use the global environment (this is a special and the top level environment in which you declare variables out of a refclass or a function).
package org.expr.rcaller;
import java.util.ArrayList;
import org.expr.rcaller.Globals;
import org.expr.rcaller.RCaller;
import org.expr.rcaller.RCode;
import org.junit.Test;
import org.junit.Assert;
public class HandlingAllVariablesTest {
private final static double delta = 1.0 / 1000.0;
@Test
public void GetAllVariablesInEnvironmentTest() {
RCaller caller = new RCaller();
Globals.detect_current_rscript();
caller.setRscriptExecutable(Globals.Rscript_current);
RCode code = new RCode();
code.addDouble("x", 5.65);
code.addDouble("y", 8.96);
code.addRCode("result <- as.list(.GlobalEnv)");
caller.setRCode(code);
caller.runAndReturnResult("result");
ArrayList<String> names = caller.getParser().getNames();
System.out.println("Names : " + names);
System.out.println("x is " + caller.getParser().getAsDoubleArray("x")[0]);
System.out.println("y is " + caller.getParser().getAsDoubleArray("y")[0]);
Assert.assertEquals(caller.getParser().getAsDoubleArray("x")[0], 5.65, delta);
Assert.assertEquals(caller.getParser().getAsDoubleArray("y")[0], 8.96, delta);
}}
Results like this:
Names : [x, y]
x is 5.65
y is 8.96
Here is the key point
code.addRCode("result <- as.list(.GlobalEnv)");
so we are defining a variable to capture all of the variables defined in the global environment. as.list() function converts an environment object into a list. The second important point is to transfer this variable into the java by
caller.runAndReturnResult("result");
You can see more examples about capturing specific variables rather than environments by visiting the blog page and the web page.