Java调用R

java部分:

import java.io.*;
//import java.util.LinkedList;


public class test 
{
//static private LinkedList<String[]> S_data_table; 
/**
* @param args
* @throws IOException 
* @throws InterruptedException 
*/
public static void main(String[] args) throws IOException, InterruptedException 
{
//---------------------------准备数据S_data_table-------------------
String[] head={"sad", "asd"};
String[] line1={"123", "233"};
String[] line2={"34", "342"};



//---------------------------执行R程序------------------------
String command="C:\\Program Files\\R\\R-3.1.0\\bin\\x64\\Rscript test.R";
Runtime r=Runtime.getRuntime(); 
Process p=null;

p=r.exec(command);


//---------------------------java输出到R程序-------------------------------
OutputStream pOutputStream = p.getOutputStream();
PrintStream output = new PrintStream(pOutputStream, true);



output.println(head[0]+","+head[1]);
output.println(line1[0]+","+line1[1]);
output.println(line2[0]+","+line2[1]);



output.flush();
//-------------------------------------------------------



//-----------------------------R程序输入到java----------------------
InputStreamReader q=new InputStreamReader(p.getInputStream());

BufferedReader br=new BufferedReader(q);
//------------------------读取br的内容-----------------------------------
String msg=null;
while(1==1)
{
msg=br.readLine();

if (msg==null)
{
break;
}
System.out.println(msg);
}

br.close();
q.close();
}


}


R部分:

x<-read.table(file="stdin", head=T, nrows=2)
write.table(x, file=stdout(), quote=F)


运行结果:

sad.asd
1 123,233
2 34,342

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值