使用 renjin 在 Java 和 R 之间移动数据

Moving data between Java and R using renjin

我正在尝试使用 Renjin 在 Java 和 R 之间移动数据,反之亦然(作为数据框)。在 Whosebug 中搜索我发现了一些东西,起初我认为它会起作用,但它不会,因为我在 Java 中获得了 R 数据作为外部指针。这是代码:

测试包;

(省略进口)

public class 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);
    }

}

}

你能帮帮我吗?

你快到了。

java 对象 dfProcessos 包含 ListVector.NamedBuilder,而不是 ListVectorListVector 实现了 SEXP 接口,因此将被视为普通的 R 对象(或 "S-Expression"),而所有其他 Java 对象,包括 Builder 对象,都需要包装在外部指针 R 对象。

换行:

engine.put("df", dfProcessos);

收件人:

engine.put("df", dfProcessos.build());

它应该可以工作。