java中运行r脚本_使用JAVA程序运行R脚本

我是R编程的新手.我创建了一个简单的R脚本并尝试使用JAVA类运行它,但我无法做到.

我尝试过使用Rserve和rJava.使用Rserve,代码执行在创建“RConnection”实例后停止,而使用rJava提供异常“java.lang.UnsatisfiedLinkError:jri.dll:找不到依赖库”.

JAVA类代码如下:

对于rJava:

import org.rosuda.JRI.Rengine;

public class Temp {

public static void main(String a[]) {

// Create an R vector in the form of a string.

String javaVector = "c(1,2,3,4,5)";

// Start Rengine.

Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);

// The vector that was created in JAVA context is stored in 'rVector' which is a variable in R context.

engine.eval("rVector=" + javaVector);

//Calculate MEAN of vector using R syntax.

engine.eval("meanVal=mean(rVector)");

//Retrieve MEAN value

double mean = engine.eval("meanVal").asDouble();

//Print output values

System.out.println("Mean of given vector is=" + mean);

}

}

对于Rserve:

import org.rosuda.REngine.REXPMismatchException;

import org.rosuda.REngine.Rserve.RConnection;

import org.rosuda.REngine.Rserve.RserveException;

public class Temp {

public static void main(String a[]) {

RConnection connection = null;

System.out.println("line 10");

try {

// Create a connection to Rserve instance running on default port 6311

System.out.println("line 15");

connection = new RConnection();

System.out.println("line 17");

//Note four slashes (\\\\) in the path

connection.eval("source('D:\\\\RExamples\\\\helloworld.R')");

System.out.println("line 19");

int num1=10;

int num2=20;

int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();

System.out.println("The sum is=" + sum);

} catch (RserveException e) {

e.printStackTrace();

} catch (REXPMismatchException e) {

e.printStackTrace();

}

}

}

如果您的问题不清楚或者您想知道其他任何问题,请告诉我.提前致谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值