Java call R (ii) _jri (EXT)

Source: Internet
Author: User

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
  1. Public void Callr () {
  2. System.out.println (System.getproperty ("Java.library.path"));
  3. System.loadlibrary ("Jri");
  4. try {
  5. //rengine re = new Rengine (null, FALSE, New Textconsole ());
  6. Rengine re = Rengine.getinstance ();
  7. Re.eval ("SETWD (' f:/r_stat/')");
  8. Re.eval ("Selected_dat <-read.csv (' dat.csv ', head=true,sep= ', ', as.is = T)");
  9. {
  10. ...
  11. Re.eval ("rect (1, 5, 3, 7, col= ' White ')");
  12. Re.eval ("Dev.off ()");
  13. }
  14. } catch (Exception e) {
  15. E.printstacktrace ();
  16. }
  17. }

Test passed.


Supplement: The structure of Rengine is set into a single case

[Java]View PlainCopy
  1. Class rengine{
  2. private static rengine re;
  3. Private Rengine () {
  4. }
  5. public static Rengine getregineinstance () {
  6. if (re = =null) {
  7. Re = new Rengine (null, false, new Textconsole ());
  8. Re.eval ("library (arules)");
  9. }
  10. return re;
  11. }
  12. }



Java call R (ii) _jri (EXT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.