2

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?

4

1 回答 1

1

您可以使用数值优化以及符号优化将最大值用于定积分。例如

integrate(2 * x, x)

返回 x ^ 2。您可以定义数值计算的下限和上限(定积分的近似值)。您可以使用多个选项调用可执行文件最大值。您可以在文本文件中发送积分(当然还有任何其他数学运算,包括极限和导数),也可以在命令 shell 上发送字符串。例如

maxima -q -r "integrate(2 * x, x);"

计算括号之间的积分并将结果返回到标准输出。

祝你好运。

于 2018-10-01T18:56:06.247 回答