1

I am trying to execute a R file from java, here is the code i have tried.

RCaller caller = new RCaller();
RCode code = new RCode();  
caller.setRscriptExecutable("D:\\R\\R-3.0.2\\bin\\Rscript.exe");    
code.clear();
caller.setRCode(code);
code.R_source("D:\\Data\\Workspace\\Cpackage\\try.R");
caller.setRCode(code);
caller.runOnly();

try.R file

myinput<-function(){
//loading a csv file,reading it and creating an excel file(Working when it is run from r directly)
}

myinput()

The above rcaller java code does not do anything.Please help if i am doing anything wrong,i need to do this very badly. If there is anyother way to achieve this please suggest!

Tim
  • 41,901
  • 18
  • 127
  • 145
Magic
  • 505
  • 2
  • 6
  • 19

1 Answers1

0

RCaller does not change the Java path format that is given in

code.R_source("D:\\Data\\Workspace\\Cpackage\\try.R");

to R format. This produces a R code like

source("D:\\Data\\Workspace\\Cpackage\\try.R")

so you need to change it to

code.R_source("D:/Data/Workspace/Cpackage/try.R");

It is good to have a look at the source code at Rcaller source, it can be seen that

public void R_source(String sourceFile) {
    addRCode("source(\"" + sourceFile + "\")\n");
}

defines the function with literally given R file as an R string.

jbytecode
  • 681
  • 12
  • 29
  • Tried, but no difference. – Magic May 07 '14 at 10:30
  • But when i do caller.runAndReturnResult("myinput()"); instead of caller.runonly() it gives Can not handle R results due to : rcaller.exception.RCallerParseException: Can not parse the R output: org.xml.sax.SAXParseException; Error. – Magic May 07 '14 at 10:31
  • And you don't need to use RCaller for this. You don't handle any returned value so just use Java's Runtime and Process classes. – jbytecode May 07 '14 at 10:32
  • caller.runAndReturnResult("myinput()"); is not a correct use of RCaller. Please read the blog entries and home page to get an idea of using it. – jbytecode May 07 '14 at 10:34
  • Runtime.getRuntime().exec("D:/Data/Workspace/Cpackage/try.R"); is what you suggested rite? it gives java.io.IOException: Cannot run program "D:/Data/Workspace/Cpackage/try.R": CreateProcess error=193, %1 is not a valid Win32 application...if i am doing anything wrong,please help! – Magic May 07 '14 at 10:37
  • My suggestion is to learn Windows first, than Java and finally R and RCaller for doing these stuff. – jbytecode May 07 '14 at 10:40