1

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

This Image depicts output in DEBUG mode 2) Image depicts the output in normal RUN mode

This Image depicts the output in normal RUN mode

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Brijesh Kumar Sahoo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.