calling R script from java calling R script from java r r

calling R script from java


You just want to call an external application: wouldn't the following work?

Runtime.getRuntime().exec("Rscript myScript.R"); 


You can easily adapt this code: http://svn.rforge.net/org/trunk/rosuda/REngine/Rserve/test/StartRserve.java

Among other things it finds R and runs a fixed script in R - you can replace that script with with your script and ignore the last two methods.


Do not wait for the process to finish with Thread.sleep()...

Use the waitFor() method instead.

Process child = Runtime.getRuntime().exec(command, environments, dataDir);int code = child.waitFor();switch (code) {    case 0:        //normal termination, everything is fine        break;    case 1:        //Read the error stream then        String message = IOUtils.toString(child.getErrorStream());        throw new RExecutionException(message);}