尝试将 java resultSet 数据转换为 R 中的 data.frame。最多 5 行没有问题。但是当超过 5 条记录正在转换时,会出现错误“语法错误,意外 SYMBOL,期待 ','”
public static void main(String[] args) throws ScriptException, FileNotFoundException {
RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
engine.eval("source(\"script.R\")");
engine.eval("workflow.predict("+getData()+")");
}
public static ListVector getData() {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
StringArrayVector.Builder userid = new StringArrayVector.Builder();
StringArrayVector.Builder defaultCC = new StringArrayVector.Builder();
StringArrayVector.Builder typeId = new StringArrayVector.Builder();
StringArrayVector.Builder amount = new StringArrayVector.Builder();
StringArrayVector.Builder cc = new StringArrayVector.Builder();
StringArrayVector.Builder activity = new StringArrayVector.Builder();
try{
String query = "select top 6 wi.owner_user_id as userId, u.cost_center_id as defaultCC, li.expense_type_id typeId, wi.doc_specific_amount as amount, al.cost_center_id as cc,wi.activity_id as activity from alwf_work_item wi, alco_user u, aler_expense_line_item li,aler_line_allocation al where u.user_id = wi.owner_user_id and wi.business_object_id=li.parent_id and li.exp_line_item_id=al.exp_line_item_id";
connection = getConnection();
statement = connection.prepareStatement(query);
resultSet = statement.executeQuery();
while(resultSet.next()) {
userid.add(resultSet.getLong("userId"));
defaultCC.add(resultSet.getLong("defaultCC"));
typeId.add(resultSet.getLong("typeId"));
amount.add(resultSet.getLong("amount"));
cc.add(resultSet.getLong("cc"));
activity.add(resultSet.getLong("activity"));
}
}catch (Exception e) {
e.printStackTrace();
}
ListVector.NamedBuilder myDf = new ListVector.NamedBuilder();
myDf.setAttribute(Symbols.CLASS, StringVector.valueOf("data.frame"));
myDf.setAttribute(Symbols.ROW_NAMES, new RowNamesVector(userid.length()));
myDf.add("userId", userid.build());
myDf.add("defaultCC", defaultCC.build());
myDf.add("typeId", typeId.build());
myDf.add("amount", amount.build());
myDf.add("cc", cc.build());
myDf.add("activity", activity.build());
return myDf.build();
}
R 脚本
workflow.predict <- function(abc) {
print(abc)
print(data.frame(lapply(abc, as.character), stringsAsFactors=FALSE))
dataset = data.frame(lapply(abc, as.character), stringsAsFactors=FALSE)
library(randomForest)
classifier = randomForest(x = dataset[-6], y = as.factor(dataset$activity))
new.data=c(62020,3141877,46013,950,3141877)
y_pred = predict(classifier, newdata = new.data)
print(y_pred)
return(y_pred)
}
以下是使用前 6 条记录运行脚本时出现的错误。但对于前 5 名记录,它运行时没有任何错误。提前致谢。
Exception in thread "main" org.renjin.parser.ParseException: Syntax error at 1 68 1 70 68 70: syntax error, unexpected SYMBOL, expecting ','
at org.renjin.parser.RParser.parseAll(RParser.java:146)
at org.renjin.parser.RParser.parseSource(RParser.java:69)
at org.renjin.parser.RParser.parseSource(RParser.java:122)
at org.renjin.parser.RParser.parseSource(RParser.java:129)
at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:127)
at RTest.main(RTest.java:27)