Basic steps
1. the Rjava package needs to be installed in R.
2. system variable Path plus
C:\Program files\r\r-3.0.1\bin\i386; C:\Program Files\r\r-3.0.1\library\rjava\jri
System variable Classpath plus:
C:\Program Files\r\r-3.0.1\library\rjava\jri\jri.jar
3. Add Jri.jar, Jriengine.jar, Rengine.jar to the project
4. Add Rjava test instance, add Project test, execute success. Configuration is complete.
5. Java Local Testing (Project Test/rtest.java and Test/testdll.java)
Example of C:\Program files\r\r-3.0.1\library\rjava\jri\examples\ with Rjava
The R-pin is executed, and the basic syntax is the same as the Rserve. for loop requires A translation of the Java variable and R variable.
6. Add Jri.jar, Jriengine.jar, Rengine.jar to the Lib directory of the Java Web
C:\Program Files\r\r-3.0.1\library\rjava\jri\i386\jri.dll into Java path or in the Tomcat Bin directory (that is, jri.dll into Java Classpath in the Web project)
7. Instantiate the Rengine object using the Textconsole method from the Rtest instance
Class Textconsole implements rmainloopcallbacks{}
Rengine re=new rengine (args, False, new Textconsole ());
8. After personal testing, multiple calls to rengine re=new rengine (args, False, new Textconsole ()) in the project; the second error will be made.
Finally, the Rengine object instantiation is changed to Singleton mode (important), and the project initialization is instantiated together.
For an external package that needs to be loaded in R, it can be executed in conjunction with the RE instantiation.
[Java]View PlainCopy
- Public void Callr () {
- System.out.println (System.getproperty ("Java.library.path"));
- System.loadlibrary ("Jri");
- try {
- //rengine re = new Rengine (null, FALSE, New Textconsole ());
- Rengine re = Rengine.getinstance ();
- Re.eval ("SETWD (' f:/r_stat/')");
- Re.eval ("Selected_dat <-read.csv (' dat.csv ', head=true,sep= ', ', as.is = T)");
- {
- ...
- Re.eval ("rect (1, 5, 3, 7, col= ' White ')");
- Re.eval ("Dev.off ()");
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
Test passed.
Supplement: The structure of Rengine is set into a single case
[Java]View PlainCopy
- Class rengine{
- private static rengine re;
- Private Rengine () {
- }
- public static Rengine getregineinstance () {
- if (re = =null) {
- Re = new Rengine (null, false, new Textconsole ());
- Re.eval ("library (arules)");
- }
- return re;
- }
- }
Java call R (ii) _jri (EXT)