java中如何调用r代码_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 (iSystem.out.println("Contents:");

i=0;

while (i}

System.out.println(re.eval("sqrt(36)"));

*/

} catch (Exception e) {

System.out.println("EX:"+e);

e.printStackTrace();

}

re.end();

System.out.println("end");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值