我正在尝试使用 Renjin 在 Java 和 R 之间移动数据,反之亦然(作为数据框)。在 Stackoverflow 中搜索我发现了一些起初我认为它会起作用的东西,但它不会因为我在 Java 中获得了 R 数据作为 externalptr。这是代码:
包装测试;
(进口省略)
公共类TesteRenjin {
public static void main(String[] args) throws ClassNotFoundException {
List<Processos_Julgados> processosJulgados = new
ArrayList<Processos_Julgados>();
String driver = "com.mysql.jdbc.Driver";
String pwd = "";
String user = "root";
String url = "jdbc:mysql://localhost/cocaj_miner";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, pwd);
System.out.println("Conexão bem sucedida");
String sql = "select ementa, numProcesso from processos_julgados where
ementa is not Null limit 15";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
Processos_Julgados procJulg = new Processos_Julgados();
procJulg.setNumProcesso(rs.getString("numProcesso"));
procJulg.setEmenta(rs.getString("ementa"));
processosJulgados.add(procJulg);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
//System.out.println("Conexão não foi possível");
} finally {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(TesteRenjin.class.getName()).log(Level.SEVERE, null, ex);
}
}
StringArrayVector.Builder numProcParaR = new StringArrayVector.Builder();
for (Processos_Julgados processos : processosJulgados) {
numProcParaR.add(processos.getNumProcesso());
}
ListVector.NamedBuilder dfProcessos = new ListVector.NamedBuilder();
dfProcessos.setAttribute(Symbols.CLASS, StringVector.valueOf("data.frame"));
dfProcessos.setAttribute(Symbols.ROW_NAMES, new RowNamesVector(processosJulgados.size()));
dfProcessos.add("processos", numProcParaR.build());
RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
engine.put("df", dfProcessos);
try {
engine.eval("str(df)");
} catch (ScriptException ex) {
Logger.getLogger(TesteRenjin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
请问你能帮帮我吗?