1

我正在尝试使用 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);
    }

}

}

请问你能帮帮我吗?

4

1 回答 1

0

你快到了。

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

换行:

engine.put("df", dfProcessos);

至:

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

它应该工作。

于 2018-01-27T14:43:10.930 回答