使用rJava打印NULL而不是整数值

时间:2018-03-12 12:27:29

标签: java r

所有必需的JAR文件都已加载并正常工作,PATHS设置正确。粗略地说,我尝试使用re.eval()进行基本操作,输出正确。 我认为在使用re.eval()时我在TestR.java文件中犯了一个错误。它打印NULL而不是节点的程度,即1001。

这是TestR.java

package rwithjava;



import java.io.File;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class TestR {

 public static void main(String args[]) {
 if (!Rengine.versionCheck()) {
 System.err.println("Java version mismatch.");
 System.exit(1);
 }
 String my[] = { "--vanilla" };
 Rengine re=new Rengine(my,false,new TextConsole());
 if (!re.waitForR()) {
 System.out.println("Cannot load R");
 System.exit(1);
 }

     re.eval(".libPaths('C:/Users/Karen/Documents/R//win-library/3.4')"); 
     re.eval("a<-matrix(1,ncol=1000,nrow=1000)");
     re.eval("library(igraph)");
     re.eval("g<-graph_from_adjacency_matrix(a, mode = c(\"undirected\")");
     REXP x=re.eval("degree(g,v=c(1)))");
     System.out.println(x);

    re.end();

    }
}

这是TextConsole.java

package rwithjava;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.rosuda.JRI.RMainLoopCallbacks;
import org.rosuda.JRI.Rengine;

public class TextConsole implements RMainLoopCallbacks {

 @Override
 public void rWriteConsole(Rengine re, String text, int oType) {
 System.out.print(text);
 }

 @Override
 public void rBusy(Rengine re, int which) {
 System.out.println("rBusy(" + which + ")");
 }

 @Override
 public String rReadConsole(Rengine re, String prompt, int addToHistory) {
 System.out.print(prompt);
 try {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String s = br.readLine();
 return (s == null || s.length() == 0) ? s : s + "\n";
 } catch (Exception e) {
 System.out.println("jriReadConsole exception: " + e.getMessage());
 }
 return null;
 }

 @Override
 public void rShowMessage(Rengine re, String message) {
 System.out.println("rShowMessage \"" + message + "\"");
 }

 @Override
 public String rChooseFile(Rengine re, int newFile) {
 return "";
 }

 public void rFlushConsole(Rengine re) {
 }

 public void rLoadHistory(Rengine re, String filename) {
 }

 public void rSaveHistory(Rengine re, String filename) {
 }
}

0 个答案:

没有答案