Rengine.eval returns null

2019-06-09 18:38发布

问题:

I have the following implementation:

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();
}

The line double[] foreCast = mean.asDoubleArray() throws NullPointerException, because mean is null. I can't figure out why re.eval("forecastedData$mean") returns null.

UPDATE: After a long investigation, I found out that it is not loading the libraries (RMySQL, xts, forecast). I have added the libraries to PATH environment variable but still they are not discovered.