我正在构建一个Android机器学习应用程序,在java类中我需要调用R脚本。但在此之前,我需要确保R已启动并运行。我怎么做?如何在Android代码中建立Rconnection?
public String getSum() {
RConnection connection = null;
try {
/* Create a connection to Rserve instance running on default port
* 6311
*/
connection = new RConnection();
connection.eval("library(randomForest)");
/* Note four slashes (\\\\) in the path */
connection.eval("source('MyScript.R')");
REXP sum =connection.eval("predictSAL()");
this.sum = sum.asString();
} catch (RserveException e) {
e.printStackTrace();
} catch (REXPMismatchException e) {
e.printStackTrace();
} catch (REngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sum;
}