JavaファイルからRスクリプトを実行しようとしています。JavaからRscriptを実行する
Javaコード
public static void main(String a[]) {
RConnection connection = null;
try {
/* Create a connection to Rserve instance running on default port
* 6311
*/
connection = new RConnection();
/* Note four slashes (\\\\) in the path */
connection.eval("source(\'D:/r script/arima with upper limit final.R\')");
//connection.eval("Rserve()");
int lim1=2500;
int lim2=2700;
REXP sum=connection.eval("testfunction()");
//System.out.println("The sum is=" +);
} catch (RserveException e) {
e.printStackTrace();
} //catch (REXPMismatchException e) {
//e.printStackTrace();
//}
}
Rスクリプト
testfunction = function(){
lim1 = 2500
lim2 = lim1+400
start =lim2
modeldata = as.vector(x$Mean.F3Amp)
alarm = as.vector(x$Mean.F3HW)
alarmpart = as.ts(alarm[lim1:lim2])
predictalarm = alarmpart
datapart = as.ts(modeldata[lim1:lim2])
fit = Arima(datapart,order = c(3,1,1))
modelforecast = forecast(fit,30)
uppervaluemean = mean(modelforecast$upper[,2])
prevMean = mean(tail(datapart,30))
newMean =mean(modelforecast$mean)
alarmMean = mean(tail(alarmpart,30))
if(abs(alarmMean-uppervaluemean)>=0.01)
{
uppervaluemean =uppervaluemean+0.005
predictalarm = c(predictalarm,rep.int(uppervaluemean,30))
} else
{
predictalarm = c(predictalarm,rep.int(alarmMean,30))
print("Else")
}
plot(modelforecast,xlim=c(0,lim2-lim1+30),ylim=c(0,0.6))
lines(alarmpart,col=3)
lines(predictalarm,col=5)
filename = paste("D:/Plots123/plot",toString(iteration),".jpg",sep="")
dev.copy(jpeg,filename=filename)
dev.off()
TRUE
}
私はRServeに新しいです。以下は私が得るエラーです。
org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127 at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234) at pkg.Temp.main(Temp.java:23)
23行目:REXP sum=connection.eval("testfunction()");
有馬は、ユーザー定義関数ではありません..私は私はコード内で行方不明ですlibrarisを追加しようとします – Vini