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"); 

    } 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章