2016-10-21 68 views
-1

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

这是我的代码中的R演播室:

//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这是我的输出:

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

我不会使用JavaGD。你有另一种解决方案吗?

+0

此我中的R工作室输出:https://i.stack.imgur.com/PjSzu.png –

回答

1

我已经知道我的错误了。我将“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(); 
    } 
} 

}