简体   繁体   English

从Java运行R脚本

[英]Running an R script from Java

I've got a problem when I try to run a R script from Java Netbeans on Mac OS. 当我尝试在Mac OS上从Java Netbeans运行R脚本时,我遇到了问题。 I truly look for an answer of this problem on internet but nothing works. 我真的在互联网上寻找这个问题的答案,但没有任何作用。

I've used Rserve and Runtime.getRuntime().exec("Rscript myScript.R") but neither of them works with my program. 我已经使用了Rserve和Runtime.getRuntime()。exec(“Rscript myScript.R”),但它们都不能用于我的程序。

When I use Rserve, I run Rserve(args="--no-save") on R console and Rconnection.eval("\\myscript.R") on Java program and when I execute it, the program continues running without any response, no errors either and no stops. 当我使用Rserve时,我在R控制台上运行Rserve(args =“ - no-save”),在Java程序上运行Rconnection.eval(“\\ myscript.R”),当我执行它时,程序继续运行而没有任何响应,没有错误也没有停止。 In fact when I try to execute a more simply R script, like calculate the mean or something like that, it works, but when I try to coerce a data.frame in a xts/zoo time series or just to load xts/zoo library first in my script, the program doesn't stop running and does nothing. 事实上,当我尝试执行更简单的R脚本时,比如计算平均值或类似的东西,它可以工作,但是当我尝试在xts / zoo时间序列中强制data.frame或者只是为了加载xts / zoo库首先在我的脚本中,程序不会停止运行并且什么也不做。

On the other hand, when I try to execute "Runtime.getRuntime().exec("Rscript myScript.R")" like appears in other similar post, nothing happens. 另一方面,当我尝试执行“Runtime.getRuntime()。exec(”Rscript myScript.R“)”就像出现在其他类似帖子中一样,没有任何反应。 The program looks to execute the script but it doesn't give me any result although stops running at least. 程序看起来执行脚本但它不会给我任何结果,尽管至少停止运行。 Maybe it is because of Mac OS and that I couldn't indicate to Java what is the Rscript or R.app path, I don't really know. 也许是因为Mac OS而且我无法向Java表明什么是Rscript或R.app路径,我真的不知道。

Thank you very much in advance and I wish you could help me. 非常感谢你,我希望你能帮助我。

Javi. 哈维。

The file code is: 文件代码是:

public void Rconnection () {

RConnection c=new RConnection();
System.out.println("INFO : Trying to Connect to R");                          
c.parseAndEval("source(\"/scriptname.R\")");
System.out.println("Greeting from R:" + result.asString());
c.close();
}

And the R script is: R脚本是:

EU.df <- read.csv("/myinput.csv",header=T)
EU.xts <- xts(EU.df[,2:5],seq(as.Date("1970-01-02"),len=nrow(EU.df),by="day"))
write.csv(EU.df, file = "/myoutputfile.csv",row.names=FALSE)

Maybe it's because of some problems with R libraries or because of MAC OS. 也许是因为R库或MAC OS存在一些问题。

Have you tried using JRI ? 你尝试过使用JRI吗? That might "block" unlike RServe and give you better messages. 与RServe不同,这可能会“阻止”并为您提供更好的消息。

For example: 例如:

REngine re = new JRIEngine(new String[] { "--no-save" }, new RCallback(), false);
re.parseAndEval("source(\"/scriptname.R\")");
re.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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