2

How do we invoke R language / R script from java? Basically I need java kind of wrapper around R script.

  1. Data will be given to java layer say method setData(double[]) which should in turn sends to R script - let us say setDataR(double []) method.

  2. R script will perform some computation say calls method double[] computeR().

  3. Java program will get the computation result by invoking double[] getData() which in turn delegates to R script to get the computated data.

How can this be performed by JRI, Rserv, Rcaller? I do not see any way to invoke Rscript methods? Please send sample. It should be simular to JNI (java, C++) invocations.

  1. Does within jvm I think multiple threads cannot invoke R script call correct? Any work around?

Thanks

4

2 回答 2

0

从 Java 中使用 R 的最简单方法是使用Runtime.exec("")、获取响应并解析它。有关如何运行本机指令的典型示例是:

Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

从这里,您可以读取和处理打印到标准输出并缓冲到stdInput.

于 2013-03-22T18:31:04.843 回答
0

RCaller 正是你想要的。假设您有一个定义为的双数组“a”

double[] a = new double[] {1.0, 2.0, 3.0};

并且您想要计算平均值、中位数和标准差值。创建一个新的 RCaller 实例

RCaller caller = new RCaller();
Globals.detect_current_rscript();
caller.setRscriptExecutable(Globals.Rscript_current);

RCode code = new RCode();


code.addDoubleMatrix("a", a);
code.addRCode("s <- list(mean=mean(a), median=median(a), sd=sd(a))");

caller.setRCode(code);

caller.runAndReturnResult("s");

double mean = caller.getParser().getAsDoubleArray("mean")[0];
double median = caller.getParser().getAsDoubleArray("median")[0];
double sd = caller.getParser().getAsDoubleArray("sd")[0];

并且变量中位数保存从 R 脚本返回的 2.0 的值。有关详细信息,请访问此处的页面

于 2014-04-13T15:08:29.940 回答