Siya Matsakarn
Siya Matsakarn

Reputation: 427

How to plot graph in Java using R language (read.csv(file))

I’d like to plot graph in java using r language which import data from file.csv. I have problem my output is blank. In netbeans, the programe run success don't have error but the garph can't show and In R studio, the graph can show normally.

This my code in 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)

this my code in 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();
         }
     }
}

This my output in netbeans:

this my file.csv: https://drive.google.com/drive/folders/0B3ynuWBsKXoHY2tSQVdQZU4tVlE?usp=sharing

I wouldn't to use a JavaGD. Do you have another solution?

Upvotes: 0

Views: 1387

Answers (1)

Siya Matsakarn
Siya Matsakarn

Reputation: 427

I already know my mistake. I change "connection.eval" to "caller.addRCode".

Example:

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();
     }
 }

}

Upvotes: 1

Related Questions