0

I'm trying to source a R script file from an org.rosuda.REngine.Rserve.RConnection and then calling a function from that script, but am getting, "Error: could not find function "main".

Start Rserve from terminal

> require(Rserve)
Loading required package: Rserve
> Rserve()
Starting Rserve...
 "C:\Users\slenzi\DOCUME~1\R\WIN-LI~1\3.3\Rserve\libs\x64\Rserve.exe"
> Rserve: Ok, ready to answer queries.
Error: could not find function "main"

External script rdbcTest1.R

require(RJDBC)

main <- function() {
    jdbcDriver <- JDBC(driverClass = dbDriverClass, classPath = dbDriverPath)
    jdbcConnection <- dbConnect(jdbcDriver, dbUrl, dbUser, dbPwd)
    dbResult <- dbGetQuery(jdbcConnection, dbQuery)
    dbDisconnect(jdbcConnection)
    return(dbResult)
}

Java Code

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngine;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.RList;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

REXP result = null;
RConnection c = null;
try {

    c = new RConnection();

    String driverPath = "C:/temp/ojbdc6.jar";
    String scriptPath = "C:/temp/rdbcTest1.R";

    c.assign("dbUser", "foo");
    c.assign("dbPwd", "******");// commented out
    c.assign("dbUrl", "jdbc:oracle:thin:@myhost:1511:ecogtst");
    c.assign("dbDriverClass", "oracle.jdbc.driver.OracleDriver");
    c.assign("dbDriverPath", driverPath);       

    // assign query to execute
    c.assign("dbQuery", "SELECT count(*) FROM prs.members");            

    String evalSource = String.format("try(source(\"%s\", local=TRUE), silent=TRUE)", scriptPath);

    c.eval("evalSource");

    // debug
    result = c.eval("ls()");
    logger.info("REXP debug => " + result.toDebugString());         

    result = c.eval("main()");

} catch (RserveException e) {
    logger.error("error running script test, " + e.getMessage());
} finally {
    if(c != null){
        c.close();
    }
}

Output

evaluating => try(source("C:/temp/rdbcTest1.R", local=TRUE), silent=TRUE)
REXP debug => org.rosuda.REngine.REXPString@61c6bc05[6]{"dbDriverClass","dbDriverPath","dbPwd","dbQuery","dbUrl","dbUser"}

Error: could not find function "main"
error running oracle script test, eval failed, request status: error code: 127

Running the script directly from the R terminal it works fine. When ran from RConnection I can see the values of the variables I assign (c.assign(...)) but not the main() function from the sourced script.

What am I missing in regards to sourcing a script? How can I access a function from the script?

Thanks.

** UPDATE **

I got the following to work, but if anyone has any idea why source() doesn't seem to work in my example above, please let me know!

RConnection c = ...
REXP x = c.parseAndEval("try(eval(parse(file=\"C:/temp/rdbcTest3.R\")), silent=TRUE)")
Dilbert
  • 63
  • 6
  • 1
    Have you tried: `String evalSource = String.format("try(parse(source(\"%s\", local=TRUE), silent=TRUE))", scriptPath);` – brijs Aug 05 '16 at 23:28
  • I haven't tried this yet, but I'm guessing it might work because you are calling 'parse' on the source. Thanks. Anyway, I got it working via RConnection.parseAndEval method. – Dilbert Oct 10 '16 at 21:10

0 Answers0