R與JAVA的混合編程

我這裏將UJMP包中的Matrix類型轉化爲二維數組再轉化爲,一維數組,再轉換爲R中的矩陣,獲取矩陣返回二維數組也有相應的描述數組

注意:在使用時必定要加載Rserve服務,若是不是R系統中的包,還需加載包名。spa

 int genoRCnt = (int) geno.getRowCount();
        int genoCCnt = (int) geno.getColumnCount();
        int phenoRCnt = (int) pheno.getRowCount();
        int phenoCCnt = (int) pheno.getColumnCount();
        double[] genoArray = new double[genoCCnt * genoRCnt];
        double[] phenoArray = new double[phenoRCnt * phenoCCnt];
        long begin = System.currentTimeMillis();
        {//Translate Matrix geno In Java To Matrix In R
            int k1 = 0;
            for (int i = 0; i < genoRCnt; i++) {
                for (int j = 0; j < genoCCnt; j++) {
                    genoArray[k1++] = geno.getAsDouble(i, j);
                }
            }
            c.assign("genoR", genoArray);
            c.voidEval("GenoInR<-matrix(genoR," + genoRCnt + "," + genoCCnt + ",TRUE" + ")");
//            REXP phenoY = c.eval("GenoInR");
//            MatrixFactory.importFromArray(phenoY.asDoubleMatrix()).showGUI();
        }
        {//Translate Matrix pheno In Java To Matrix In R
            int k2 = 0;
            for (int i = 0; i < phenoRCnt; i++) {
                for (int j = 0; j < phenoCCnt; j++) {
                    phenoArray[k2++] = pheno.getAsDouble(i, j);
                }
            }
            c.assign("phenoR", phenoArray);
            c.voidEval("PhenoInR<-matrix(phenoR," + phenoRCnt + "," + phenoCCnt + ",TRUE" + ")");
        }
相關文章
相關標籤/搜索