简体   繁体   中英

Differ in Results between running and debugging the JAVA Program with Embedded R code

The Code is below:

      RConnection connection = new RConnection();
        String load_pkgs = "require(Rserve); require(forecast)";
        connection.eval(load_pkgs);
        String strx1 = "xData = read.table(\"D:\\\\R_TESTS\\\\ts_interval_data21.csv\",sep=\"|\",header=FALSE,col.names=c(\"a\",\"b\",\"c\",\"d\",\"xData\",\"f\"))[,\"xData\",drop=FALSE]";
        connection.eval(strx1);
        String strx2 = "x = xData[1:100,1]; fit = auto.arima(x);";
        connection.eval(strx2);
        String strx3 = "result = forecast(fit,h=12);";
        connection.eval(strx3);
        Object result =       (Object)connection.eval("result").asNativeJavaObject();
        HashMap map  = (HashMap)result;
        List<Object> objects  = new ArrayList<Object>(map.keySet());
        double values[] = (double[])objects.get(4);
                  for(int i=0;i<values.length;i++)
        {
            System.out.println((i+1)+":"+values[i]);
        }

actually I'm running R inside the JAVA using Rserve() connection,now every thing works fine but when running the program the output will be totally wrong and while debugging the output is perfect. I'm not able to find the bug in my code please review the code and leave your suggestions. Thank You

I hope the following images will help:

1) Image depicts output in DEBUG mode

此图描述了DEBUG模式下的输出 2) Image depicts the output in normal RUN mode

此图描述了正常RUN模式下的输出

The reason behind this is the fundamental difference in Java and R storage types. As you can read in the following link: Documentation for asNativeObject() this function "tries" to convert R return-types to Java Object class. But this procedure is not successful every time. And thus I would suggest you not to use the asNativeObject() function. Try to find out a workaround using other functions.

The reason you see the difference is because the Java Object created in debug and run modes are different in their content and structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM