我有以下实现:
public static void main(String[] args) {
// new R-engine
Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
if (!re.waitForR()) {
System.out.println("Unable to load R");
return;
} else
System.out.println("Connected to R");
re.eval("library(RMySQL)");
re.eval("library(xts)");
re.eval("library(forecast)");
re.eval("db = dbConnect(MySQL(), user='root', password='', dbname='stats_server', host='localhost')");
re.eval(
"data = fetch(dbSendQuery(db, \"select date(time_stamp) as date, total from history_data where discriminator='D' and hour_of_day=3\"),n=-1)");
re.eval("data$date = as.Date(as.character(data$date), format(\"%Y-%m-%d\"))");
re.eval("dataAsTimeSeries = xts(data$total, data$date)");
re.eval("forecastedData = forecast(auto.arima(dataAsTimeSeries), h=20)");
REXP mean = re.eval("forecastedData$mean");
double[] foreCast = mean.asDoubleArray();
for (int i = 0; i < 10; i++) {
System.out.println(foreCast[i]);
}
re.end();
}
double[] foreCast = mean.asDoubleArray()
行会引发NullPointerException
,因为mean
是null
。我无法弄清楚为什么re.eval("forecastedData$mean")
会返回null
。
更新:经过长时间的调查,我发现它没有加载库(RMySQL,xts,预测)。我已将库添加到PATH
环境变量中,但仍未发现它们。