How to plot a graph using R, Java and JRI?

2019-03-20 17:53发布

问题:

I am very new to R, so please pardon me if this is a very very basic question.

I have written an R script, which does some calculations and at the end plots Box Plot. If I run this script from the R console, it runs fine and graph is plotted.

If I run the script from the Java program (the sample rtest.java program which is given on the JRI site), the calculations are done fine. But there is some problem with plotting the graph. The frame window opens and remains busy and hangs. (If I try to click on the window, it shows 'Not Responding'.) I am using JRI.

This is how I am plotting the graph in the script:

boxplot(mat);

Above command works fine on the R-console.

I am pretty sure that this is about some import/package or some similar setting. But not able to find out what exactly it is.

Can anyone help me with this?

Thanks.

EDIT 1: Output of my box-plotting command:

Output of this code on the link provided by John:

回答1:

Here is how I did it: Pastebin link. Basically you need to tell the R package JavaGD that you have a JFrame available where the R plots can be displayed.

The example in the answer of @John Colby just stores the plot as a file and then displays it in Java - also an option, but a somewhat indirect way to do it.

(Unfortunately for you my code is in Scala, but as that's quite similar to Java (I translated it from Java), I hope you will be able to translate it back. I used some JRI example as a base for it, and it uses only java classes.)



标签: java r plot jri