When I want to run some code through RCaller, Java throws the following exception:
Exception in thread "JavaFX Application Thread" com.github.rcaller.exception.ExecutionException: Can not send the source code to R file due to: java.io.IOException: The pipe is being closed Maximum number of retries exceeded.
This is my code:
protected void initialize(){
if (!System.getProperty("os.name").contains("Windows")){
rscript = rscript.replace("\\","/");
r = r.replace("\\","/");
}
out.println(rscript + "\n" + r);
caller = RCaller.create(RCallerOptions.create(rscript,r,FailurePolicy.RETRY_1,500,500,RProcessStartUpOptions.create()));
}
private void calculateIntegral(String newValue){
RCode rCode = RCode.create();
rCode.addRCode("func <- function (x) (" + newValue + ")");
rCode.addRCode("x <- integrate(func," + from.getValue() + "," + to.getValue() + ")");
caller.setRCode(rCode);
caller.runAndReturnResult("x"); <- This is where I get the Exception
value.setText(String.valueOf(caller.getParser().getAsDoubleArray("x")[0]));
}
I checked my R installation and it seems to be fine
Edit:
I also tried concatenating rscript and r with "\"" like so:
rscript = "\"" + rscript + "\"";
r = "\"" + r + "\"";
And it didn´t work either :(
Edit 2:
When I try generating a plot like this:
rCode.addRCode("plot(func)");
Java still throws an exception but also generates a pdf with the plot inside
Also...I´m slowly giving up on R...is there another method of calculating integrals from a mathematical function given as a string in Java?
You can use maxima for definite integrals using numerical optimization as well as symbolic ones. For example
integrate(2 * x, x)
returns x ^ 2. You can define the lower and upper bounds for numeric calculations (an approximation of definite integrals). You can call the executable file maxima with several options. You can send integrals (of course any other mathematical operations including limits and derivates) in a text file as well as a string on the command shell. For example
maxima -q -r "integrate(2 * x, x);"
calculates the integral given between parenthesis and returns the result to standard output.
Good luck.