I want to create a java applet that takes various files and runs statistical tests on them. 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?
-
1Can you run R scripts (code ..whatever) in *any* Java app.? If so, it is probably possible to run them in an applet. Re the applet itself, a desktop app. launched using [JWS](http://stackoverflow.com/tags/java-web-start/info) would be easier to deploy, and provide a better user experience. – Andrew Thompson Oct 01 '13 at 14:42
2 Answers
The kind of functionality you are looking for is readily available in R itself. The Shiny package, created by RStudio, does exactly what you need: provide a web interface to R including simple GUI elements. The advantage of using Shiny is that it takes care of all the nitty gritty details, letting you focus on the analyses. It literally took me 2 hours to install Shiny and create my first web app.
Disclaimer: I do not have any affiliations with RStudio, I'm just a happy user :).

- 59,984
- 12
- 142
- 149
Is there a way to "bridge" R and Java?
There certainly is. You can use 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):
// 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:
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.
Alternatively you just create R scripts that you execute from Java using RScript.exe
. For example:
Rscript my_script.R
You will find plenty of information on the internet explaining how to execute command line commands from Java. I don't know enough about it to give you an example.

- 1
- 1

- 7,306
- 1
- 29
- 45