在Java中调用R

//http://weblog.dotjava.nl/
// 在R中运行install.packages('rJava',,'http://www.rforge.net/')
//在环境变量里面要配R_HOME  C:\Program Files\R\R-2.10.0
//PATH里面配 C:\Program Files\R\R-2.10.0\bin

//PATH一定要设对
package com.wdf;
import java.io.*;
import java.awt.Frame;
import java.awt.FileDialog;

import java.util.Enumeration;

import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RList;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.RMainLoopCallbacks;

class TextConsole implements RMainLoopCallbacks
{
    public void rWriteConsole(Rengine re, String text, int oType) {
        System.out.print("rWriteConsole");
    }
  
    public void rBusy(Rengine re, int which) {
        System.out.println("rBusy("+which+")");
    }
  
    public String rReadConsole(Rengine re, String prompt, int addToHistory) {
        System.out.print(prompt);
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String s=br.readLine();
            return (s==null||s.length()==0)?s:s+"\n";
        } catch (Exception e) {
            System.out.println("jriReadConsole exception: "+e.getMessage());
        }
        return null;
    }
  
    public void rShowMessage(Rengine re, String message) {
        System.out.println("rShowMessage \""+message+"\"");
    }

    public String rChooseFile(Rengine re, int newFile) {
FileDialog fd = new FileDialog(new Frame(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE);
fd.show();
String res=null;
if (fd.getDirectory()!=null) res=fd.getDirectory();
if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile());
return res;
    }
  
    public void   rFlushConsole (Rengine re) {
    }

    public void   rLoadHistory  (Rengine re, String filename) {
    }  
  
    public void   rSaveHistory  (Rengine re, String filename) {
    }  
}

public class rtest {
    public static void main(String[] args) {
if (!Rengine.versionCheck()) {
     System.err.println("** Version mismatch!");
     System.exit(1);
}
        System.out.println("Creating Rengine (with arguments)");
  Rengine re=new Rengine(args, false, new TextConsole());
        System.out.println("Rengine created, waiting for R");
        if (!re.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }

  try {
   REXP x;
  // re.eval("data(iris)",false);
            //re.eval("contributors()");
   //x=re.eval(arg0);
   x=re.eval("eval(expression(3+2))");
   Double d=x.asDouble();
   //String str=x.getContent();
   System.out.println(d.toString());
           System.out.println(re.eval("sqrt(36)").asDouble());
              System.out.println(re.eval("3+2").asDouble());
   /*
   RList l = x.asList();
   if (l!=null) {
    int i=0;
    String [] a = l.keys();
    System.out.println("Keys:");
    while (i<a.length) System.out.println(a[i++]);
    System.out.println("Contents:");
    i=0;
    while (i<a.length) System.out.println(l.at(i++));
   }
   System.out.println(re.eval("sqrt(36)"));
   */
  } catch (Exception e) {
   System.out.println("EX:"+e);
   e.printStackTrace();
  }
     re.end();
     System.out.println("end");

    }
}

你可能感兴趣的:(java,C++,c,.net,C#)