Hi everybody
anybody knows how to talk with R like shown in the Java-Code above?
Perhaps someone can help, or tell me its not possible.
/*
* Java_R.java
*
* Created on November 11, 2003, 5:36 PM
*/
import java.io.*;
/**
*
* @author markus
*/
public class Java_R {
/** Creates a new instance of Java_R */
public Java_R() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("R
--no-save");
//Process process = Runtime.getRuntime().exec("R
--help");
BufferedReader in = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader reader = new BufferedReader(new
java.io.InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(process.getOutputStream()));
String s=null;
String c=null;
while(true) {
try {
while ((s=in.readLine())!=null) {
System.out.println(s);
}
/* The big problem is: it seems like R doesn't come out of this loop, or if
its does I get the following error: java.io.IOException: Broken pipe*/
System.out.print("\n>");
c = reader.readLine();
out.write(c);
out.flush();
}
catch(Exception e) {
System.out.println(e.toString());
System.out.println("Cause : " + e.getCause());
System.out.println("Message : " + e.getMessage());
}
}
}
catch (Exception e) {
System.out.println(e.toString());
System.out.println("Cause : " + e.getCause());
System.out.println("Message : " + e.getMessage());
}
}
}
Thanks everybody who can help me or tries to !
Markus
Hi Markus, My Java is a bit rusty, but I think when reading from an input stream like the one you've set up, Java "blocks" if there's no input, waiting until there is some to return. I don't think the stream ends until the R process closes. You should test ready() to see if there is more output to be read. Hope this is helpful, Hadley