我已经用Java编写了一个脚本,该脚本依靠rJava通过R执行代码。该脚本在OSX下工作,但是,它在Windows下返回以下错误:
org.rosuda.REngine.REngineEvalException: error during evaluation
at org.rosuda.REngine.JRI.JRIEngine.eval(JRIEngine.java:283)
at org.rosuda.REngine.REngine.parseAndEval(REngine.java:102)
at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
at Test.plot_dataFrame(Test.java:90)
at Test.main(Test.java:35)
我现在要花几个小时来调试此问题,我希望这里的某人可能有解决此问题的想法。
我的脚本:
import java.util.*;
import org.rosuda.REngine.*;
public class Test {
public static void main(String args[]) {
String yName = "value";
String xName = "time";
int N = 10;
int K = 10;
// generating x scale (equidistant here)
double[] xData = numbers1ToN(N);
// generating random data for this example
double[] DAX = cumSum(randomArray(N, K));
double[] MDax = cumSum(randomArray(N, K));
double[] cDAX = cumSum(randomArray(N, K));
double[] TecDax = cumSum(randomArray(N, K));
// filling data into a list of double[] and storing their tags
List<double[]> yData = Arrays.asList(DAX, MDax, cDAX, TecDax);
List<String> classNames = Arrays.asList("DAX", "MDax", "cDAX", "TecDax");
int[] saveSize = new int[] {10, 10};
String path = // On Windows path to Desktop, on mac path to external drive
plot_dataFrame (path, saveSize, xName, xData, yName, yData, classNames, true, true);
}
private static void plot_dataFrame(String path, int[] saveSize, String xName, double[] xData, String yName, List<double[]> yData, List<String> classNames, boolean plot_points, boolean plot_lines) {
REngine re;
try {
re = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", new String[] { "--vanilla" }, null, false);
REXP df = null;
try {
REXP[] data = new REXP[yData.size() + 1];
data[0] = new REXPDouble(xData);
for (int i = 1; i < data.length; i++) {
data[i] = new REXPDouble(yData.get(i - 1));
}
List<String> nameList = new ArrayList<>(classNames);
nameList.add(0, xName);
df = REXP.createDataFrame(new RList(data, nameList.toArray(new String[0])));
} catch (REXPMismatchException e) {
e.printStackTrace();
}
re.assign("df", df);
re.assign("path", path);
re.assign("yName", yName);
re.assign("xName", xName);
re.assign("saveWidth", Integer.toString(saveSize[1]));
re.assign("saveHeight", Integer.toString(saveSize[0]));
re.parseAndEval("require(ggplot2)");
re.parseAndEval("require(reshape2)");
re.parseAndEval("df <- melt(df, id.vars = xName, variable.name = 'class', value.name = yName)");
re.parseAndEval("figure <- ggplot(df, aes_string(x = xName, y = yName))");
if (plot_points) re.parseAndEval("figure <- figure + geom_point(aes(colour = factor(class))) ");
if (plot_lines) re.parseAndEval("figure <- figure + geom_line(aes(colour = class)) ");
re.parseAndEval("ggsave(figure, file=path, width=as.numeric(saveWidth), height=as.numeric(saveHeight))");
re.close();
} catch(Exception e) {
e.printStackTrace();
}
}
private static double[] numbers1ToN(int N) {
double[] var = new double[N];
for (int i = 1; i <= N; ++i) {
var[i - 1] = (double) i;
}
return var;
}
private static double[] cumSum(double[] array) {
for (int i = 1; i < array.length; i++) {
array[i] += array[i - 1];
}
return array;
}
private static double randomFill(int K) {
Random rand = new Random();
return rand.nextInt(K);
}
}
让我感到困扰的是,完全相同的代码在Mac上评估时没有问题。我尝试按照以下步骤在OSX上复制Java / R设置:
现在,我被困在这一点上。 Java似乎找到了JRI本机库并尝试执行该代码,但是,在遵循在我的其他系统上运行并生成图像的相同指令时,它却失败了。由此产生的错误,您可以在本文开头找到。
这里有人知道我可能会缺少什么吗?非常感谢您的投入!