0

我是javaand的初学者renjin。我需要我的输出结果显示在我的jtextpane,我该怎么做?我好累。如果你知道怎么做,请告诉我。谢谢

RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
        // create a Renjin engine:
        ScriptEngine engine = factory.getScriptEngine();

    try {
        //engine.eval("RealData <- read.csv('E:/R Project/Original Data Cleaning Data Historis.csv', header = TRUE)");                    
        engine.eval("RealData <- read.csv('"+textPath.getText().replace('\\','/')+"', header = TRUE)");                    
        //engine.eval("print(RealData)");
        engine.eval("library(e1071)");
        //engine.eval("tune.out <- tune(svm, hasil~., data=RealData, kernel=\"radial\",ranges = list(cost = c(1:10),gamma = c (1:4)))");
        //engine.eval("summary(tune.out)");
        engine.eval("svmfit <- svm(hasil~., data=RealData, kernel=\"radial\", cost=2, gamma=4)");

        editorSvm.setText(engine.eval("print(svmfit)").toString());


    } catch (ScriptException ex) {
        Logger.getLogger(Klasifikasi.class.getName()).log(Level.SEVERE, null, ex);
    }`
4

1 回答 1

2

print.svm函数总是返回 NULL,因此您将无法通过查看print().

相反,该print()函数会将文本写入 ScriptEngine 的标准输出流,默认情况下会发送到进程的标准输出流。

正如 [documentation][2] 中现在描述的,您可以通过这种方式将 R 的输出重定向到 StringWriter:

StringWriter outputWriter = new StringWriter();   
engine.getContext().setWriter(outputWriter);
engine.eval("print(svmfit)");

String output = outputWriter.toString();

// Reset output to console
engine.getContext().setWriter(new PrintWriter(System.out));

print.svm 函数的输出现在将存储在 Javaoutput变量中。

于 2017-09-05T06:47:15.180 回答