Rcaller not giving back variables

487 views Asked by At

When integrating R with Java with RCaller, I never get back any variable that is created within the script. There seems to be a fundamental missunderstanding how RCaller works. Isn't it that all the variables in the Environment can be parsed from Java? How?

@Test
    public void test() {
        RCaller caller = new RCaller();
        RCode code = new RCode();
        caller.setRscriptExecutable("/usr/bin/Rscript");
        caller.runAndReturnResult("source('~/git/conjoint_it/src/main/r/a.R')");
        System.out.println(caller.getParser().getNames());

    }

a.R:

...

m3 <- mlogit(choice ~ 0 + seat + cargo + eng 
             + as.numeric(as.character(price)), 
             data = cbc.mlogit)
su = summary(m3)
m3 #last line

this returns only [visible]

2

There are 2 answers

8
jbytecode On

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.

0
Michael On

Imports:

import com.github.rcaller.rStuff.RCaller;
import com.github.rcaller.rStuff.RCode;

Java code:

RCaller caller = new RCaller();
RCode code = new RCode();
caller.setRscriptExecutable("C:\\Program Files\\R\\R-4.0.2\\bin\\Rscript.exe");
caller.setRCode(code);
code.clear();
caller.cleanRCode();

//Methods to parse variables to the Rscript
code.addInt("mydata1", 5);
code.addDoubleArray("mydata2", new double[]{1, 2, 3, 4, 5});
code.addRCode("mydata3 <- 'Data'");

//Calling the Rscript
code.addRCode("source('./src/test.r')");

//Reciving Values from the Rscript through the result variable
caller.runAndReturnResult("result");
int data = caller.getParser().getAsIntArray("data")[0];
double mean = caller.getParser().getAsDoubleArray("mean")[0];
String text = caller.getParser().getAsStringArray("text")[0];

System.out.println(data);
System.out.println(mean);
System.out.println(text);

test.r:

result1 <- mydata1 * 2
result2 <- mean(mydata2)
result3 <- paste("Result3", mydata3, sep=" ")

result <- list(data=result1, mean=result2, text=result3)

Output:

10
3.0
Result3 Data