|
@@ -0,0 +1,46 @@
|
|
|
|
+package me.yoqi.main;
|
|
|
|
+
|
|
|
|
+import org.rosuda.REngine.REXPMismatchException;
|
|
|
|
+import org.rosuda.REngine.Rserve.RConnection;
|
|
|
|
+import org.rosuda.REngine.Rserve.RserveException;
|
|
|
|
+
|
|
|
|
+/**java调用R执行数据分析,这样可以在web交互。
|
|
|
|
+ * @author liuyuqi
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+public class Main {
|
|
|
|
+
|
|
|
|
+// install.packages('Rserve')
|
|
|
|
+// install.packages('RODBC')
|
|
|
|
+//
|
|
|
|
+// library('Rserve')
|
|
|
|
+// Rserve(args='--RS-enable-remote')
|
|
|
|
+//
|
|
|
|
+// R.version.string
|
|
|
|
+// rnorm(10)
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ callRserve();
|
|
|
|
+ } catch (RserveException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (REXPMismatchException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static void callRserve() throws RserveException, REXPMismatchException {
|
|
|
|
+// RConnection rConnection = new RConnection("192.168.101.122");
|
|
|
|
+ RConnection rConnection = new RConnection("127.0.0.1");
|
|
|
|
+// 打印R版本
|
|
|
|
+ String rv = rConnection.eval("R.version.string").asString();
|
|
|
|
+ System.out.println(rv);
|
|
|
|
+
|
|
|
|
+// 打印10个随机数
|
|
|
|
+ double[] arr = rConnection.eval("rnorm(10)").asDoubles();
|
|
|
|
+ for (double d : arr) {
|
|
|
|
+ System.out.println(d);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|