Abdelilah El-Hadad
Abdelilah El-Hadad

Reputation: 3

encoding error by passing String values from Java to R using the rosuda REngine

After opening a R-engine. I assigned to it a dataframe, that I get successfully from database.

After printing some values from the R-engine I found out, that all the string values have a mojibake like following example:

In Java:

String value = "Hello"
System.out.println(value) ---> "Hello" // it's fine

We assign now the value into the R engine and printing the value there

In R engine:

print(value) ---> "ÿþHelloÿþ"

here is my code:

//open R rngine..
REngine engine = null;
String labSiteCode = null;
//read config files..
List<Database> dbs = Utilities.getDatabases(null);
        
//select the 2 nd. database..
Database db = dbs.get(1);
labSiteCode = db.getLabSiteCode();
        
//database connection..
Connection conn = SqlServerJdbcUtils.getConnection(db);
        
String query = "EXEC [" + db.getDbname() + "].[" + db.getSchemaname() + "].[SP_AI_TestDuration_NewData]";
        
//create resultset(stmt would be closed automatically)..
ResultSet rs = null;
Statement stmt = null;
        
int i = 0;
if (conn != null) {
    try {
        if (!conn.isClosed()) {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);   
            String sampleCode = null;
            ArrayList<Double> Test_Dauer_h = new ArrayList<Double>();                   
            while(rs.next()) {
                if (i == 0) {
                    sampleCode = rs.getString("sampleCode");
                }
                Test_Dauer_h.add(i, Double.parseDouble(rs.getString("Test_Dauer_h")));
                System.out.println(sampleCode+";["+Test_Dauer_h.get(i)+"];");
                i++;
            }
            System.out.println("(" + i + " rows read)");
            //init engine & create R objects..

            if (i > 0) {
                String[] header = {"sampleCode","Test_Dauer_h"};
                String[] _sampleCode = new String[i];
                double[] _Test_Dauer_h = new double[i];
                REXP h = null;
                for(int j = 0; j < i; j++) {
                    _sampleCode[j] = sampleCode;
                }   
                engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", new String[] {"-no-save"}, new REngineStdOutput(), false);
                h = REXP.createDataFrame(new RList(new REXP[] {new REXPString(_sampleCode),new REXPDouble(_Test_Dauer_h)},header)); 
                engine.assign("df_lab", h);       
                engine.parseAndEval("print(labSiteCode);print(max(df_lab$Test_Dauer_h)); print(df_lab$sampleCode[1]); print(df_lab$HGS[1])");
                engine.parseAndEval("library(randomForestSRC)");
                engine.parseAndEval("library(sqldf)");
                engine.parseAndEval("library(stringr)");
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getMessage() + "\n" + query);
    } catch (REXPMismatchException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getMessage());
    } 
    } finally {
        try {
            rs.close();
            stmt.close();

            if (i > 0)
                engine.close();
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage() +"\n" + query);
        }

        SqlServerJdbcUtils.disconnect(conn);
    }
}

What did I wrong in my program?

Output

Upvotes: 0

Views: 104

Answers (0)

Related Questions