简体   繁体   English

有没有办法在Java小程序中使用R?

[英]Is there a way to use R in a java applet?

I want to create a java applet that takes various files and runs statistical tests on them. 我想创建一个Java小程序,它接受各种文件并对其进行统计测试。 The user would choose a file and press the appropriate button for a statistical test. 用户将选择一个文件,然后按适当的按钮进行统计测试。 All of the statistical tests would be coded in R. Is there a way to "bridge" R and Java? 所有统计测试都将用R编码。是否有办法“桥接” R和Java?

The kind of functionality you are looking for is readily available in R itself. 您正在寻找的功能类型可以在R本身中轻松获得。 The Shiny package , created by RStudio, does exactly what you need: provide a web interface to R including simple GUI elements. 由RStudio创建的Shiny软件包完全满足您的需求:为R提供一个Web界面,包括简单的GUI元素。 The advantage of using Shiny is that it takes care of all the nitty gritty details, letting you focus on the analyses. 使用Shiny的优点是,它可以处理所有细微的细节,让您专注于分析。 It literally took me 2 hours to install Shiny and create my first web app. 从字面上看,我花了2个小时来安装Shiny并创建了我的第一个Web应用程序。

Disclaimer: I do not have any affiliations with RStudio, I'm just a happy user :). 免责声明:我与RStudio没有任何隶属关系,我只是一个快乐的用户:)。

Is there a way to "bridge" R and Java? 有没有办法“桥接” R和Java?

There certainly is. 当然有。 You can use JRI . 您可以使用JRI

Here is a JRI Java to R example that I provided in a previous answer (passing a double array and having R sum it): 这是我在上一个答案中提供的JRI Java到R的示例(传递双精度数组并将R求和):

// Start R session.
Rengine re = new Rengine (new String [] {"--vanilla"}, false, null);

// Check if the session is working.
if (!re.waitForR()) {
  return;
}

re.assign("x", new double[] {1.5, 2.5, 3.5});
REXP result = re.eval("(sum(x))");
System.out.println(result.asDouble());
re.end();

The above is basically the equivalent of doing this in R: 以上基本上等效于在R中执行此操作:

x <- c(1.5, 2.5, 3.5)
sum(x)

Note: If you're using Eclipse and JRI I highly recommend this plugin to set it up. 注意:如果您使用的是Eclipse和JRI,我强烈建议您使用此插件进行设置。


Alternatively you just create R scripts that you execute from Java using RScript.exe . 或者,您只需创建使用RScript.exe从Java执行的R脚本。 For example: 例如:

Rscript my_script.R

You will find plenty of information on the internet explaining how to execute command line commands from Java. 您将在Internet上找到大量信息,说明如何从Java执行命令行命令。 I don't know enough about it to give you an example. 我对此了解不足,无法举一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM