-->

exporting plotted variable shows blank image

2019-08-04 17:00发布

问题:

I am doing java and R integration using JRI. Please find below script

            String path = "C:\\Users\\hrpatel\\Desktop\\CSVs\\DataNVOCT.csv";
            rengine.eval("library(tseries)");
            rengine.eval(String.format("mydata <- read.csv('%s')",path.replace('\\', '/')));
            String exportFilePath= "C:\\Users\\hrpatel\\Desktop\\CSVs\\arima3.jpg";
            rengine.eval("Y <- NewVisits");
            rengine.eval("t <- Day.Index");
            rengine.eval("summary(Y)");
            rengine.eval("adf.test(Y, alternative='stationary')");
            rengine.eval("adf.test(Y, alternative='stationary', k=0)");
            rengine.eval("acf(Y)");
            rengine.eval("pacf(Y)");
            rengine.eval("mydata.arima101 <- arima(Y,order=c(1,0,1))");
            rengine.eval("mydata.pred1 <- predict(mydata.arima101, n.ahead=1000)");
            rengine.eval(String.format("jpeg('%s')",exportFilePath.replace('\\', '/')));
            rengine.eval("plot(t,Y)");
            rengine.eval("lines(mydata.pred1$pred, col='blue',size=10)");
            rengine.eval("lines(mydata.pred1$pred+1*mydata.pred1$se, col='red')");
            rengine.eval("lines(mydata.pred1$pred-1*mydata.pred1$se, col='red')");
            rengine.eval("dev.off()");

In above codebase when i tried plot(t,Y) or plot(Y). it export a blank image, while in case of plot(mydata) it is working file.

One more thing when i run above code in R it creates the image(using JRI it shows blank image).

I have spend 1 day to solve this but i dont found any solution.

Please suggest if you have any alternatives.

Your help is needed.

Thanks in Advance

回答1:

if i understand correctly, you have a data set named mydata, that has two columns, NewVisits, and Day.Index, in that case you need to change:

rengine.eval("Y <- NewVisits");

to

rengine.eval("Y <- mydata$NewVisits");

and

rengine.eval("t <- Day.Index");

to

rengine.eval("t <- mydata$Day.Index");

This also explains why plot(mydata) works for you - because R recognizes it.

if this isn't the solution, then i cant see where you are reading NewVisits and Day.Index from

BTW i stongly recommend to plot using the ggplot package



标签: r plot jri