Search in sources :

Example 1 with RserveException

use of org.rosuda.REngine.Rserve.RserveException in project rsession by yannrichet.

the class RserveSession method set.

/**
 * Set R data.frame in R env.
 *
 * @param varname R list name
 * @param data numeric data in list
 * @param names names of columns
 * @return succeeded ?
 */
@Override
public boolean set(String varname, double[][] data, String... names) {
    RList list = buildRList(data, names);
    log(HEAD_SET + varname + " <- " + list, Level.INFO);
    try {
        synchronized (R) {
            R.assign(varname, REXP.createDataFrame(list));
        }
    } catch (REXPMismatchException re) {
        re.printStackTrace();
        log(HEAD_ERROR + " RList " + list.toString() + " not convertible as dataframe.", Level.ERROR);
        return false;
    } catch (RserveException ex) {
        log(HEAD_EXCEPTION + ex.getMessage() + "\n  set(String varname=" + varname + ",double[][] data, String... names)", Level.ERROR);
        return false;
    }
    return true;
}
Also used : RList(org.rosuda.REngine.RList) REXPMismatchException(org.rosuda.REngine.REXPMismatchException) RserveException(org.rosuda.REngine.Rserve.RserveException)

Example 2 with RserveException

use of org.rosuda.REngine.Rserve.RserveException in project dataverse by IQSS.

the class RRequest method eval.

/**
 * Evaluate script
 * @return R-Expression
 */
public REXP eval() {
    REXP result = null;
    try {
        open();
        result = mRC.eval(mScript);
    } catch (RserveException e) {
        LOG.warning(String.format("RRequest: %s", e.getMessage()));
    } finally {
        close();
    }
    return result;
}
Also used : REXP(org.rosuda.REngine.REXP) RserveException(org.rosuda.REngine.Rserve.RserveException)

Example 3 with RserveException

use of org.rosuda.REngine.Rserve.RserveException in project rsession by yannrichet.

the class RserveSessionTest method testRFileIO.

@Test
public void testRFileIO() throws Exception {
    // get file test...
    String remoteFile1 = "get" + rand + ".csv";
    File localfile1 = new File(tmpdir.getParent(), remoteFile1);
    System.out.println("GET :" + localfile1.getAbsolutePath());
    s.voidEval("aa<-data.frame(A=c(1,2,3),B=c(4,5,6))");
    s.voidEval("write.csv(file='" + remoteFile1 + "',aa)");
    InputStream is1 = null;
    OutputStream os1 = null;
    try {
        // System.out.println("openFile");
        is1 = s.R.openFile(remoteFile1);
        // System.out.println("OK");
        os1 = new FileOutputStream(localfile1.getAbsolutePath());
        byte[] buf = new byte[65536];
        try {
            s.R.setSendBufferSize(buf.length);
        } catch (RserveException ex) {
            ex.printStackTrace();
        }
        int n = 0;
        while ((n = is1.read(buf)) > 0) {
            os1.write(buf, 0, n);
        }
    // os1.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (os1 != null) {
                os1.close();
            }
            if (is1 != null) {
                is1.close();
            }
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }
    assert localfile1.exists();
    // check csv file is written
    StringBuffer b = new StringBuffer();
    try {
        FileInputStream fis = new FileInputStream(localfile1);
        Reader r = new BufferedReader(new InputStreamReader(fis));
        int n = 0;
        while ((n = r.read()) > 0) {
            b.append((char) n);
        }
        r.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    assert b.charAt(4) == 'A' : b.charAt(4);
    s.rawEval("rm(aa)");
    // localfile1.delete();
    // put file test...
    String remoteFile2 = "put" + rand + ".csv";
    File localfile2 = new File(tmpdir.getParent(), remoteFile2);
    System.out.println("PUT :" + localfile2.getAbsolutePath());
    String content = "A,B,C\n1,2,3\n";
    try {
        FileOutputStream fos = new FileOutputStream(localfile2);
        Writer w = new BufferedWriter(new OutputStreamWriter(fos));
        w.write(content);
        w.flush();
        w.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // check csv file is written
    try {
        FileInputStream fis = new FileInputStream(localfile2);
        Reader r = new BufferedReader(new InputStreamReader(fis));
        int n = 0;
        while ((n = r.read()) > 0) {
            System.out.print((char) n);
        }
        r.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    InputStream is2 = null;
    OutputStream os2 = null;
    try {
        // System.out.println("createFile");
        os2 = s.R.createFile(remoteFile2);
        // System.out.println("OK");
        is2 = new FileInputStream(localfile2);
        byte[] buf = new byte[65536];
        try {
            s.R.setSendBufferSize(buf.length);
        } catch (RserveException ex) {
            ex.printStackTrace();
        }
        int n = 0;
        while ((n = is2.read(buf)) > 0) {
            System.out.print(buf);
            os2.write(buf, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (os2 != null) {
                os2.close();
            }
            if (is2 != null) {
                is2.close();
            }
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }
    s.rawEval("ABC<-read.csv(file='" + remoteFile2 + "', header = TRUE,sep=',')");
    System.out.println(s.toString(s.cast(s.rawEval("ABC"))));
    assert ((REXP) s.rawEval("ABC$A")).isNumeric();
    s.rawEval("rm(ABC)");
    localfile2.delete();
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) REXP(org.rosuda.REngine.REXP) RserveException(org.rosuda.REngine.Rserve.RserveException) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

RserveException (org.rosuda.REngine.Rserve.RserveException)3 REXP (org.rosuda.REngine.REXP)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 Test (org.junit.Test)1 REXPMismatchException (org.rosuda.REngine.REXPMismatchException)1 RList (org.rosuda.REngine.RList)1