如何使用 R、Java 和 JRI 绘制图表?
我对 R 很陌生,所以如果这是一个非常非常基本的问题,请原谅我。
我编写了一个 R 脚本,它执行一些计算并在最后绘制箱线图。如果我从 R 控制台运行此脚本,它会正常运行并绘制图表。
如果我从 Java 程序(JRI 站点上提供的示例 rtest.java 程序)运行脚本,计算就可以完成。但绘制图表时存在一些问题。框架窗口打开并保持忙碌状态并挂起。 (如果我尝试单击该窗口,它会显示“未响应”。)我正在使用 JRI。
这就是我在脚本中绘制图表的方式:
boxplot(mat);
以上命令在 R 控制台上运行良好。
我很确定这与某些导入/包或某些类似的设置有关。但无法查明它到底是什么。
谁能帮我解决这个问题吗?
谢谢。
编辑1: 箱线图命令的输出:
此代码位于约翰提供的链接上:< /强>
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是这样做的:Pastebin 链接。基本上,您需要告诉 R 包 JavaGD 您有一个可用的 JFrame,可以在其中显示 R 图。
@John Colby 的答案中的示例只是将绘图存储为文件,然后用 Java 显示它 - 也是一个选项,但有点间接的方式。
(不幸的是,我的代码是用 Scala 编写的,但由于它与 Java 非常相似(我从 Java 翻译而来),我希望您能够将其翻译回来。
我使用了一些 JRI 示例作为它的基础,并且它仅使用 java 类。)
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.)