1

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]

Roland Kofler
  • 1,332
  • 1
  • 16
  • 33

2 Answers2

1

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.

Community
  • 1
  • 1
jbytecode
  • 681
  • 12
  • 29
  • Ok, but what if I run an entire R script as in my example? – Roland Kofler May 20 '15 at 06:40
  • after a code.addRCode("source('~/git/conjoint_it/src/main/r/a.R')"); statement, add code.addRCode("result <- as.list(.GlobalEnv)"); so you can get generated variables – jbytecode May 20 '15 at 08:36
  • 1
    and i will implement a method getAllVariablesFromScript('ascript.R'); in next version – jbytecode May 20 '15 at 08:39
  • I see that my script executes cbc.df <- read.csv("http://goo.gl/5xQObB", colClasses = c(seat = "factor", price = "factor")) which seems not work with RCaller. Is there something that hinders external data loading? – Roland Kofler May 20 '15 at 10:46
  • I see I cannot get data from a script with an one-liner: attrib <- list(seat = c("6", "7", "8")); but I can get data from a script like: attrib <- 2+2; – Roland Kofler May 20 '15 at 10:56
  • Please track the examples and read the links then you will get your code success. You are not yet familiar with the library. – jbytecode May 20 '15 at 13:20
  • Well, it has been proven that the library has serious issues, if a list can't be retrieved, but a number can. – Roland Kofler May 21 '15 at 06:30
  • numbers, vectors and lists can be retrieved by the library. – jbytecode May 21 '15 at 07:49
0

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
Michael
  • 1
  • 1
  • 3