如何在java renjin中的textpane上显示输出?

myzjeezk  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(267)

我是英语初学者 java 以及 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);
    }`
7bsow1i6

7bsow1i61#

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函数的输出现在将存储在java中 output 变量。

相关问题