Search code examples
javarjri

Where does a variable created from JRI gets stored on my disk


I have a simple JRI Code :

<%
    Rengine re = Rengine.getMainEngine();
    if (re==null){
        re = new Rengine (new String [] {"--vanilla"}, false, null); 

    }    

    re.eval("pull_data2 <- function(...){df3 <<- read.csv(file=\"/tmp/data.csv\", header=T)}");   
    re.eval("pull_data2()");
    String val = re.eval("df3").toString();


    %>

    <h3><%=val%></h3>

The values are getting printed on my browser indicating df3 is populated.

But when I login to R on my disk, there is no such "df3" variable. So, how can I see the variable from backend?

> df3
Error: object 'df3' not found 
>

Solution

  • Consider using the org.rosuda.REngine.REngine engine class. With this one you can easily configure a R console output which allows to track R variable assignments and R data processing through your server (back-end) logs. This is how you can inspect the data actually loaded in the REngine instance that runs with your JVM (which AFAIK is not the same as the R console that you would open on your computer)

    See below example.

    REngine re = REngine.getLastEngine();
    if (re==null){
        re = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine",
                                    new String [] {"--vanilla"},
                                    new REngineStdOutput(), // R console output
                                    false); 
    }    
    
    re.parseAndEval("myfunction <- function() {myvar <<- \"something\"; print(paste0(\"logging 'myvar' in R:\", myvar));}");   
    re.parseAndEval("myfunction()");
    
    REXP jriObj = re.parseAndEval("myvar");
    String myvar = jriObj.asString();
    System.out.println("logging 'myvar' in Java: " + myvar);
    

    The log will be as follow:

    [1] "logging 'myvar' in R:something"
    logging 'myvar' in Java: something