[R] Talk with from Java

Markus Helbig markus.helbig at gmx.net
Wed Nov 12 08:17:10 CET 2003


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




More information about the R-help mailing list