gpt4 book ai didi

java - 如何使用 R 语言在 Java 中绘制图形(read.csv(file))

转载 作者:行者123 更新时间:2023-12-01 09:20:52 25 4
gpt4 key购买 nike

我想使用 r 语言在 java 中绘制图表,该语言从 file.csv 导入数据。我有问题,我的输出是空白的。在netbeans中,程序运行成功,没有错误,但是图形无法显示,而在R studio中,图形可以正常显示。

这是我在 r studio 中的代码:

//import file.csv

RealDataErq <- read.csv("C:.../erq_csv_comma.csv",header = TRUE)

//create garph by Request.By

RealDataErq_RequesterBy <- aggregate(RealDataErq$NUMBER, by=list(Status=RealDataErq$Request.By), FUN=sum)

//plot garph

plot(RealDataErq_RequesterBy)

这是我在 netbeans 中的代码:

package rserveproject;

import java.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import rcaller.RCaller;
/**
*
*/
public class Graph2Erp {
public static void main(String a[]) throws IOException {
RConnection connection = null;
try {
RCaller caller = new RCaller();
caller.setRscriptExecutable("C:\\\\Program Files\\\\R\\\\R-3.3.1\\\\bin\\\\i386\\\\Rscript");
caller.cleanRCode();
connection = new RConnection();
REXP x;
// connection.eval("RealDataErq <- read.csv(file='C:\\\\Users\\\\.....\\\\Desktop\\\\erq_csv_comma.csv', sep=' ', colClasses=c(NA, NA, NA))");
connection.eval("RealDataErq <- read.csv('C:\\\\Users\\\\.....\\\\Desktop\\\\erq_csv_comma.csv',header = TRUE)");
connection.eval("RealDataErq_RequesterBy <- aggregate(RealDataErq$NUMBER, by=list(Status=RealDataErq$Request.By), FUN=sum)");
File file = caller.startPlot();
connection.eval("plot(RealDataErq_RequesterBy)");
caller.endPlot();
caller.runOnly();
ImageIcon ii = caller.getPlot(file);
caller.showPlot(file);

} catch (RserveException e) {
e.printStackTrace();
}
finally{
connection.close();
}
}
}

这是我在 netbeans 中的输出:

这是我的 file.csv: https://drive.google.com/drive/folders/0B3ynuWBsKXoHY2tSQVdQZU4tVlE?usp=sharing

我不会使用 JavaGD。您还有其他解决方案吗?

最佳答案

我已经知道自己的错误了。我将“connection.eval”更改为“caller.addRCode”。

示例:

package rserveproject;

import java.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import rcaller.RCaller;

public class Graph2Erp {
public static void main(String a[]) throws IOException {
RConnection connection = null;
try {
RCaller caller = new RCaller();
caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.3.1\\bin\\i386\\Rscript");
caller.cleanRCode();
caller.addRCode("RealDataErq <- read.csv('C:/Users/...../Desktop/erq_csv_comma.csv',header = TRUE)");
caller.addRCode("RealDataErq_RequesterBy <- aggregate(RealDataErq$NUMBER, by=list(Status=RealDataErq$Request.By), FUN=sum)");

File file = caller.startPlot();
caller.addRCode("plot(RealDataErq_RequesterBy)");
caller.endPlot();
caller.runOnly();
ImageIcon ii = caller.getPlot(file);
caller.showPlot(file);

} catch (RserveException e) {
e.printStackTrace();
}
finally{
connection.close();
}
}

}

关于java - 如何使用 R 语言在 Java 中绘制图形(read.csv(file)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40172318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com