6

I've got a problem when I try to run a R script from Java Netbeans on Mac OS. 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.

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. 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.

On the other hand, when I try to execute "Runtime.getRuntime().exec("Rscript myScript.R")" like appears in other similar post, nothing happens. 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.

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:

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.

Thomas
  • 43,637
  • 12
  • 109
  • 140
JaviMartinez
  • 61
  • 1
  • 3
  • I don't know what RScript is, but I thought we were not using `Runtime.getRuntime()` anymore. I was under the impression we have moved on to `ProcessBuilder`. Second, have you tried running this RScript from command-line? Last, since I can't see your code, are you reading from both STDOUT and STDERR? It could be that your RScript had some issue, but you are reading from the wrong output stream to be able to see it properly. – CodeChimp Feb 06 '14 at 12:53
  • damn, this looks bad! edit your question and put the code there. – alex Feb 06 '14 at 22:59
  • I have tried to call the library in R script through `library(xts/zoo)`and through `c.eval("library(xts)")` and it doesn't work either. – JaviMartinez Feb 07 '14 at 07:12
  • Why do you mean by : "doesn't give me any result although stops running at least.". Your script writes a hard-coded csv file. Have you tried to see if it was created ??! – Karl Forner Apr 24 '14 at 08:54
  • ^ And: exactly what is the problem? Which is the error you get? – David Merinos May 25 '14 at 22:54

1 Answers1

0

Have you tried using JRI? That might "block" unlike RServe and give you better messages.

For example:

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