从我的 Java 代码调用 R 文件时没有任何反应
Nothing happening when a R file is called from my Java code
我有一个 java 代码,其中包含 R 编程步骤,运行 非常好。但是相同的 R 编程步骤被提取到一个 R 文件 (MyScript.r) 中,我试图从我的 java 代码中调用这个文件。当我 运行 我的 java 代码似乎没有发生任何事情。我可能对我想要实现的目标看起来很愚蠢,也许这是真的,因为我不了解 R。所以需要你的帮助。
我的 Java 代码,其中包含 R 编程步骤。
package com.rtest;
import java.io.IOException;
import org.rosuda.JRI.Rengine;
public class RWithJavaTest {
public static void main(String a[]) {
// Create an R vector in the form of a string.
String javaVector = "c(1,2,3,4,5)";
// System.out.println("System.getProperty(\"java.library.path\")>>"+System.getProperty("java.library.path")); //Prints path in env var
// 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);
}
}
以上程序在运行时成功给我输出:3.0
Java 代码,R 文件中包含 R 编程步骤,并从 java 代码调用 R 文件。
package com.rtest;
import java.io.IOException;
public class RWithJavaTest {
public static void main(String a[]) {
try {
System.out.println("before..");
Runtime.getRuntime().exec("Rscript D:\MyScript.R");
System.out.println("after..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
R 脚本:
// Create an R vector in the form of a string.
String javaVector = "c(1,2,3,4,5)";
// System.out.println("System.getProperty(\"java.library.path\")>>"+System.getProperty("java.library.path")); //Prints path in env var
// 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);
我知道我在这里做的是完全错误的,但在这里寻求两件事的帮助。
1) 如何更正我的 R 脚本,使其 运行 没有任何问题
2) Java 调用 R 脚本的代码,所以我可以在 运行 代码后看到输出 3.0。
下面几行代码显然对我有用,它成功调用了 R 文件。
ProcessBuilder pb = new ProcessBuilder("C:/Program Files/R/R-3.4.3/bin/Rscript.exe" ,"D:/RTest/MyScript.R");
pb.start();
我有一个 java 代码,其中包含 R 编程步骤,运行 非常好。但是相同的 R 编程步骤被提取到一个 R 文件 (MyScript.r) 中,我试图从我的 java 代码中调用这个文件。当我 运行 我的 java 代码似乎没有发生任何事情。我可能对我想要实现的目标看起来很愚蠢,也许这是真的,因为我不了解 R。所以需要你的帮助。
我的 Java 代码,其中包含 R 编程步骤。
package com.rtest;
import java.io.IOException;
import org.rosuda.JRI.Rengine;
public class RWithJavaTest {
public static void main(String a[]) {
// Create an R vector in the form of a string.
String javaVector = "c(1,2,3,4,5)";
// System.out.println("System.getProperty(\"java.library.path\")>>"+System.getProperty("java.library.path")); //Prints path in env var
// 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);
}
}
以上程序在运行时成功给我输出:3.0
Java 代码,R 文件中包含 R 编程步骤,并从 java 代码调用 R 文件。
package com.rtest;
import java.io.IOException;
public class RWithJavaTest {
public static void main(String a[]) {
try {
System.out.println("before..");
Runtime.getRuntime().exec("Rscript D:\MyScript.R");
System.out.println("after..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
R 脚本:
// Create an R vector in the form of a string.
String javaVector = "c(1,2,3,4,5)";
// System.out.println("System.getProperty(\"java.library.path\")>>"+System.getProperty("java.library.path")); //Prints path in env var
// 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);
我知道我在这里做的是完全错误的,但在这里寻求两件事的帮助。
1) 如何更正我的 R 脚本,使其 运行 没有任何问题
2) Java 调用 R 脚本的代码,所以我可以在 运行 代码后看到输出 3.0。
下面几行代码显然对我有用,它成功调用了 R 文件。
ProcessBuilder pb = new ProcessBuilder("C:/Program Files/R/R-3.4.3/bin/Rscript.exe" ,"D:/RTest/MyScript.R");
pb.start();