yannrichet / rsession

R sessions wrapping for Java
BSD 2-Clause "Simplified" License
52 stars 31 forks source link

Issue to laod R script in to remote R process with r.eval() #47

Closed icejean closed 1 year ago

icejean commented 1 year ago

Hey, I'm accessing R functions provided by Rserve on a Linux server from Java through Rsession, and I've hack the code of RserverConf.java and RservSession to include a property "tls" to support a SSL connection, it works fine with the old version of Rsession. But when I try to upgrade to the newest version of Rsession, it doesn't work again with the function rc.eval("source(file_path)") to load R scripts into the remote R process anymore. Here's an example code and it's output below. I guess there's something wrong with preprocessing of the eval expression string, , any idea? Thanks in advance.

RserveTest.java

package test.jean;

import java.util.Properties;

import org.math.R.RserveSession;
import org.math.R.RserverConf;

public class RserveTest {

    public static void main(String args[]) {
        try {
            Properties prop = new Properties();
            prop.setProperty("tls", "true");
            RserverConf rconf = new RserverConf(host_ip, 6311, "user", "password", prop);
            RserveSession r = (RserveSession)new RserveSession(System.out, null, rconf);
            // create java variable from R command
            double[] rand = (double[]) r.eval("rnorm(10)"); 
            // Try to load source R script into remote R process
            r.eval("source(\"/home/jean/R/testChinese.R\")",false);
                        r.eval("testChinese()");
            System.out.println(r.asString(r.eval("rc$st")));

            r.end();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}

testChinese.R

testChinese<-function(){
  test<-c("Rserve中文测试。")
  rc<<-list(st=test)
  return(rc)  
}

Console outputs, connection is O.K., and r.eval("rnorm(10)") is run, but fail with r.eval("source(\"/home/jean/R/testChinese.R\")",false), the line is O.K. in Rstudio.

Will start Rserve session using: R://rserve:rserve@2022@124.223.110.20:6311

[eval] [?] rnorm(10)

[1.1353798479890087, 0.7775232440858648, 1.6027476936447704, 0.9962751826116198, -0.051552560170046106, -1.0327774491148507, 1.267379942526929, -0.7154783097683929, 0.2075579485456512, 1.2805371369512242]
[eval] source("/home/jean/R/testChinese.R")

java.lang.NullPointerException
    at org.math.R.RserveSession.toString(RserveSession.java:1208)
    at org.math.R.RserveSession.toString(RserveSession.java:1187)
    at org.math.R.Rsession.rawEval(Rsession.java:945)
    at org.math.R.Rsession.eval(Rsession.java:1006)
    at test.jean.RserveTest.main(RserveTest.java:19)
icejean commented 1 year ago

It's addressed by loading R source scripts with r.voidEval().

r.voidEval("source(\"/home/jean/R/testChinese.R\")");