How to display output on textpane in java renjin?

120 views Asked by At

I'm beginner in java and renjin. I need my output result show in my jtextpane, How I can do that? I'm so tired. If you know how to do it, please tell me. thanks

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);
    }`
1

There are 1 answers

0
akbertram On

The print.svm function always returns NULL, so you won't be able to get what you want by looking at the return value of print().

Instead, the print() function will write the text to the ScriptEngine's standard output stream, which is by default sent to the process' standard output stream.

As now described in the [documentation][2], you can redirect the output from R to a StringWriter this way:

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));

The output of the print.svm function will now be stored in the Java output variable.