R/Java
From QERM Wiki
(Difference between revisions)
(Created page) |
(Added example) |
||
Line 13: | Line 13: | ||
** On a mac, add this line to your .profile file (in the root of your user directory, so ~/.profile) | ** On a mac, add this line to your .profile file (in the root of your user directory, so ~/.profile) | ||
export R_HOME=/Library/Frameworks/R.framework/Resources | export R_HOME=/Library/Frameworks/R.framework/Resources | ||
+ | * Add the native JRI library to the Java library path (either on the command line call to Java or as a VM argument in the eclipse run configuration) | ||
+ | -Djava.library.path=/Library/Frameworks/R.framework/Resources/library/rJava/jri/ | ||
== Calling R == | == Calling R == | ||
+ | Here is a very short example. See the links below and the example directory for more. | ||
+ | // Start R | ||
+ | Rengine re = new Rengine(new String[] { "--no-save" }, false, null); //specify '--save', '--no-save' or '--vanilla' | ||
+ | if (!re.waitForR()) | ||
+ | { | ||
+ | System.out.println("Cannot load R"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // print a random number from uniform distribution | ||
+ | System.out.println(re.eval("runif(1)").asDouble ()); | ||
+ | |||
+ | // close R | ||
+ | re.end(); | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | = Helpful links= | + | == Helpful links== |
* [http://www.cnblogs.com/mavlarn/archive/2012/12/24/2831688.html Example with eclipse] | * [http://www.cnblogs.com/mavlarn/archive/2012/12/24/2831688.html Example with eclipse] | ||
+ | * [http://www.studytrails.com/RJava-Eclipse-Plugin/JRI-R-Java-Data-Communication.jsp Data types] | ||
* [http://www.studytrails.com/RJava-Eclipse-Plugin/Execute-R-From-Java.jsp Example calling functions and assigning/returning values] | * [http://www.studytrails.com/RJava-Eclipse-Plugin/Execute-R-From-Java.jsp Example calling functions and assigning/returning values] | ||
* [http://binfalse.de/2011/02/talking-r-through-java/ Setup for multiple IDEs and simple test case] | * [http://binfalse.de/2011/02/talking-r-through-java/ Setup for multiple IDEs and simple test case] | ||
* [http://permalink.gmane.org/gmane.comp.lang.r.rosuda.devel/1640 Where to get files] | * [http://permalink.gmane.org/gmane.comp.lang.r.rosuda.devel/1640 Where to get files] | ||
+ | * Also see the jri/examples directory for the files rtest.java and rtest2.java | ||
+ | |||
+ | = Other solutions= | ||
+ | * [https://code.google.com/p/renjin/ renjin] this looks promising, a JVM-based interpreter for R, but still beta | ||
+ | * [http://www.rforge.net/Rserve/ Rserve] more complicated client/server setup, so more useful for lots of processes needing R | ||
+ | * [http://www.mhsatman.com/rcaller.php Rcaller] very simple, but looks clunky for doing anything more than a few lines in R |
Revision as of 22:17, 3 May 2013
How to call R from a Java program
Contents |
Using JRI (from RJava)
The RJava package also allows calling Java from R.
Set up
- In R, install the RJava package (necessary?)
- The RJava package includes the JRI.jar, but to get JRIEngine.jar and REngine.jar, go to JRI files download
- Add those 3 jars to your lib directory and classpath
- Make sure the R_HOME environment variable is set
- Type $R_HOME on the command line to see if it is set and what it is set to
- In eclipse, you can add environment variables to the run configuration
- On a mac, add this line to your .profile file (in the root of your user directory, so ~/.profile)
export R_HOME=/Library/Frameworks/R.framework/Resources
- Add the native JRI library to the Java library path (either on the command line call to Java or as a VM argument in the eclipse run configuration)
-Djava.library.path=/Library/Frameworks/R.framework/Resources/library/rJava/jri/
Calling R
Here is a very short example. See the links below and the example directory for more.
// Start R Rengine re = new Rengine(new String[] { "--no-save" }, false, null); //specify '--save', '--no-save' or '--vanilla'
if (!re.waitForR()) { System.out.println("Cannot load R"); } else { // print a random number from uniform distribution System.out.println(re.eval("runif(1)").asDouble ()); // close R re.end(); }
Helpful links
- Example with eclipse
- Data types
- Example calling functions and assigning/returning values
- Setup for multiple IDEs and simple test case
- Where to get files
- Also see the jri/examples directory for the files rtest.java and rtest2.java