문제

매우 초보자 (Java) 질문 :

rserve 연결을 열었습니다 (http://www.rforge.net/rserve/) LocalHost에서, Rengine 클라이언트 (SRC/Client/Java-New)를 사용하여 연결하고 싶습니다.

컴파일하려면 "rtest.java"(src/client/java-new/rserve에 위치)를 얻으려면 어떻게해야합니까?

나는 org.rosuda.* 라이브러리를 컴파일해야한다고 모았다. Eclipse 3.5를 사용하여 어떻게해야합니까? "SRC/Client/Java -New"폴더를 Java Project의 "SRC"디렉토리에 복사하여 Eclipse-> 빌드 경로를 마우스 오른쪽 버튼으로 클릭하여 소스 폴더로 사용했습니다. 그러나 나는 이것이 "org.rosuda"패키지를 만들기에 충분하지 않다고 생각합니다. 왜냐하면 어디에서나 생성 된 "org/rosuda"디렉토리 구조가 보이지 않기 때문입니다 (그리고 일식의 불길한 빨간 선은 사라지지 않습니다).

최근 에이 작업을 수행 한 사람, 포인터를 제공하기 위해 신경 쓰나요? 감사합니다.

import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;

class TestException extends Exception {
    public TestException(String msg) { super(msg); }
}

public class test {
    public static void main(String[] args) {
    try {
        RConnection c = new RConnection();

        System.out.println(">>"+c.eval("R.version$version.string").asString()+"<<");

        {
            System.out.println("* Test string and list retrieval");
            RList l = c.eval("{d=data.frame(\"huhu\",c(11:20)); lapply(d,as.character)}").asList();
            int cols = l.size();
            int rows = l.at(0).length();
            String[][] s = new String[cols][];
            for (int i=0; i<cols; i++) s[i]=l.at(i).asStrings();
            System.out.println("PASSED");
        }

        {
        System.out.println("* Test NA/NaN support in double vectors...");
        double R_NA = Double.longBitsToDouble(0x7ff00000000007a2L);
        // int R_NA_int = -2147483648; // just for completeness
        double x[] = { 1.0, 0.5, R_NA, Double.NaN, 3.5 };
        c.assign("x",x);
        String nas = c.eval("paste(capture.output(print(x)),collapse='\\n')").asString();
        System.out.println(nas);
        if (!nas.equals("[1] 1.0 0.5  NA NaN 3.5"))
            throw new TestException("NA/NaN assign+retrieve test failed");
        System.out.println("PASSED");
        }

        {
            System.out.println("* Test assigning of lists and vectors ...");
            RList l = new RList();
            l.put("a",new REXPInteger(new int[] { 0,1,2,3}));
            l.put("b",new REXPDouble(new double[] { 0.5,1.2,2.3,3.0}));
            System.out.println("  assign x=pairlist");
            c.assign("x", new REXPList(l));
            System.out.println("  assign y=vector");
            c.assign("y", new REXPGenericVector(l));
            System.out.println("  assign z=data.frame");
            c.assign("z", REXP.createDataFrame(l));
            System.out.println("  pull all three back to Java");
            REXP x = c.parseAndEval("x");
            System.out.println("  x = "+x);
            x = c.eval("y");
            System.out.println("  y = "+x);
            x = c.eval("z");
            System.out.println("  z = "+x);
            System.out.println("PASSED");
        }
        {
            System.out.println("* Test support for logicals ... ");
            System.out.println("  assign b={true,false,true}");
            c.assign("b", new REXPLogical(new boolean[] { true, false, true }));
            REXP x = c.parseAndEval("b");
            System.out.println("  " + ((x != null) ? x.toDebugString() : "NULL"));
            if (!x.isLogical() || x.length() != 3)
                throw new TestException("boolean array assign+retrieve test failed");
            boolean q[] = ((REXPLogical)x).isTRUE();
            if (q[0] != true || q[1] != false || q[2] != true)
                throw new TestException("boolean array assign+retrieve test failed (value mismatch)");
            System.out.println("  get c(TRUE,FLASE,NA)");
            x = c.parseAndEval("c(TRUE,FALSE,NA)");
            System.out.println("  " + ((x != null) ? x.toDebugString() : "NULL"));
            if (!x.isLogical() || x.length() != 3)
                throw new TestException("boolean array NA test failed");
            boolean q1[] = ((REXPLogical)x).isTRUE();
            boolean q2[] = ((REXPLogical)x).isFALSE();
            boolean q3[] = ((REXPLogical)x).isNA();
            if (q1[0] != true || q1[1] != false || q1[2] != false ||
                q2[0] != false || q2[1] != true || q2[2] != false ||
                q3[0] != false || q3[1] != false || q3[2] != true)
                throw new TestException("boolean array NA test failed (value mismatch)");
        }

        { // regression: object bit was not set for Java-side generated objects before 0.5-3
            System.out.println("* Testing functionality of assembled S3 objects ...");
            // we have already assigned the data.frame in previous test, so we jsut re-use it
            REXP x = c.parseAndEval("z[2,2]");
            System.out.println("  z[2,2] = " + x);
            if (x == null || x.length() != 1 || x.asDouble() != 1.2)
                throw new TestException("S3 object bit regression test failed");
            System.out.println("PASSED");
        }

        { // this test does a pull and push of a data frame. It will fail when the S3 test above failed.
            System.out.println("* Testing pass-though capability for data.frames ...");
            REXP df = c.parseAndEval("{data(iris); iris}");
            c.assign("df", df);
            REXP x = c.eval("identical(df, iris)");
            System.out.println("  identical(df, iris) = "+x);
            if (x == null || !x.isLogical() || x.length() != 1 || !((REXPLogical)x).isTrue()[0])
                throw new TestException("Pass-through test for a data.frame failed");
            System.out.println("PASSED");
        }

            { // factors
                System.out.println("* Test support of factors");
                REXP f = c.parseAndEval("factor(paste('F',as.integer(runif(20)*5),sep=''))");
                System.out.println("  f="+f);
                System.out.println("  isFactor: "+f.isFactor()+", asFactor: "+f.asFactor());
                if (!f.isFactor() || f.asFactor() == null) throw new TestException("factor test failed");
                System.out.println("  singe-level factor used to degenerate:");
                f = c.parseAndEval("factor('foo')");
                System.out.println("  isFactor: "+f.isFactor()+", asFactor: "+f.asFactor());
                if (!f.isFactor() || f.asFactor() == null) throw new TestException("single factor test failed (not a factor)");
                if (!f.asFactor().at(0).equals("foo")) throw new TestException("single factor test failed (wrong value)");
                System.out.println("  test factors with null elements contents:");
                c.assign("f", new REXPFactor(new RFactor(new String[] { "foo", "bar", "foo", "foo", null, "bar" })));
                f = c.parseAndEval("f");
                if (!f.isFactor() || f.asFactor() == null) throw new TestException("factor assign-eval test failed (not a factor)");
                System.out.println("  f = "+f.asFactor());
                f = c.parseAndEval("as.factor(c(1,'a','b',1,'b'))");
                System.out.println("  f = "+f);
                if (!f.isFactor() || f.asFactor() == null) throw new TestException("factor test failed (not a factor)");
                System.out.println("PASSED");
            }


        {
            System.out.println("* Lowess test");
            double x[] = c.eval("rnorm(100)").asDoubles();
            double y[] = c.eval("rnorm(100)").asDoubles();
            c.assign("x", x);
            c.assign("y", y);
            RList l = c.parseAndEval("lowess(x,y)").asList();
            System.out.println("  "+l);
            x = l.at("x").asDoubles();
            y = l.at("y").asDoubles();
            System.out.println("PASSED");
        }

        {
            // multi-line expressions
            System.out.println("* Test multi-line expressions");
            if (c.eval("{ a=1:10\nb=11:20\nmean(b-a) }\n").asInteger()!=10)
                throw new TestException("multi-line test failed.");
            System.out.println("PASSED");
        }
        {
            System.out.println("* Matrix tests\n  matrix: create a matrix");
            int m=100, n=100;
            double[] mat=new double[m*n];
            int i=0;
            while (i<m*n) mat[i++]=i/100;
            System.out.println("  matrix: assign a matrix");
            c.assign("m", mat);
            c.voidEval("m<-matrix(m,"+m+","+n+")");
            System.out.println("matrix: cross-product");
            double[][] mr=c.parseAndEval("crossprod(m,m)").asDoubleMatrix();
            System.out.println("PASSED");
        }

        {
            System.out.println("* Test serialization and raw vectors");
            byte[] b = c.eval("serialize(ls, NULL, ascii=FALSE)").asBytes();
            System.out.println("  serialized ls is "+b.length+" bytes long");
            c.assign("r", new REXPRaw(b));
            String[] s = c.eval("unserialize(r)()").asStrings();
            System.out.println("  we have "+s.length+" items in the workspace");
            System.out.println("PASSED");
        }


        { // string encoding test (will work with Rserve 0.5-3 and higher only)
            System.out.println("* Test string encoding support ...");
            String t = "ひらがな"; // hiragana (literally, in hiragana ;))
            c.setStringEncoding("utf8");
            // -- Just in case the console is not UTF-8 don't display it
            //System.out.println("  unicode text: "+t);
            c.assign("s", t);
            REXP x = c.parseAndEval("nchar(s)");
            System.out.println("  nchar = " + x);
            if (x == null || !x.isInteger() || x.asInteger() != 4)
                throw new TestException("UTF-8 encoding string length test failed");
            // we cannot really test any other encoding ..
            System.out.println("PASSED");
        }

        } catch (RserveException rse) {
        System.out.println(rse);
    } catch (REXPMismatchException mme) {
        System.out.println(mme);
        mme.printStackTrace();
        } catch(TestException te) {
            System.err.println("** Test failed: "+te.getMessage());
            te.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}
도움이 되었습니까?

해결책

내 Linux 시스템에서 주목하는 것 외에도 Java 측면을 도와 줄 수는 없습니다 make 트릭 :

/tmp/java-new$ make
javac -d . -source 1.4 -target 1.4 MutableREXP.java REngineException.java REngine.java REXPDouble.java REXPEnvironment.java REXPExpressionVector.java REXPFactor.java REXPGenericVector.java REXPInteger.java REXP.java REXPLanguage.java REXPList.java REXPLogical.java REXPMismatchException.java REXPNull.java REXPRaw.java REXPReference.javaREXPS4.java REXPString.java REXPSymbol.java REXPUnknown.java REXPVector.java RFactor.java RList.java
jar fc REngine.jar org
rm -rf org
javac -d . -cp REngine.jar Rserve/RConnection.java Rserve/RFileInputStream.java Rserve/RFileOutputStream.java Rserve/RserveException.java Rserve/RSession.java Rserve/protocol/jcrypt.java Rserve/protocol/REXPFactory.java Rserve/protocol/RPacket.java Rserve/protocol/RTalk.java
Note: Rserve/protocol/REXPFactory.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
jar fc Rserve.jar org
rm -rf org

당신이 언급 한 예제 디렉토리와 마찬가지로 - 'make'라고 말하는 Linux 시스템에서 잘 구축됩니다. 나는 일식을 사용하지 않으며 그 측면을 도울 수 없습니다.

C ++ 예제도 잘 작동합니다 (메이크 파일을 조정하여 빌드해야합니다).

RSERV의 Rosuda 목록에 요청해야 할 수도 있습니다.

다른 팁

네 확실합니다. Dirk가 지적한대로 항아리를 컴파일 한 다음 Eclipse 오른쪽 클릭 -> 빌드 경로에 추가하면 org.rosuda.* 패키지가 생성되어 예제에서와 같이 가져올 수 있습니다.

FWIW JAR 파일은 RSERVE 웹 사이트에서 직접 제공됩니다.http://www.rforge.net/rserve/files/

그리고 그렇습니다. "Make"는 소스에서 JAR 파일을 만들기 위해 실행 해야하는 모든 것입니다 (Windows를 포함한 모든 플랫폼에서 작동).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top