Reputation: 7840
I used following code to calculating prediction in R using Java that code work fine it shows me output but, I want draw plot using same code on my predicted output code was given below
import org.rosuda.REngine.Rserve.RConnection;
public class demo{
public static void main(String args[]){
try{
System.out.println("INFO: Trying to connect to R ");
RConnection c = new RConnection();
c.eval("library('RMongo')");
c.eval(" db <- mongoDbConnect('dbname', '127.0.0.1', '27017')");
c.eval("query <- dbGetQuery(db,'collection_name','{\"hostId\" : \"300.3.3.3\"}')");
c.eval("date <- query$Date");
c.eval("cpu <- query$cpuUtilization");
c.eval("memory <- query$memory");
c.eval("df <- data.frame(date=1377843220)");
c.eval("res <- lm(cbind(memory,cpu)~ date -1 )");
int[] d= c.eval("predict(res,df)").asIntegers();
for (Integer td : d) {
System.out.println(td);
}
c.close();
}
catch(Exception e){
System.out.println("ERROR: In Connection to R ");
System.out.println("The Exception is "+ e.getMessage());
e.printStackTrace();
}
}
}
So any one knows how to draw plot using R in java code ?
Upvotes: 1
Views: 2025
Reputation: 156
Use a JFrame with a JGDPanel from JavaGD and plot something via JRI. Look into JGD: http://cran.r-project.org/web/packages/JavaGD/JavaGD.pdf Look into JRI: http://rosuda.org/JRI/
Another alternative is using R to generate the data you want and using native Java graphics libraries.
Upvotes: 2