0

我正在使用 Rserve 通过我的 Java 项目访问 R 脚本。java 代码要求用户输入文件位置并存储在字符串变量中。然后将该变量传递给应该读取文件位置的 R 函数。但这样做我得到以下错误:

Exception in thread "main" org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234)
at testMain.main(testMain.java:23)

这是我的java代码:

import java.util.Scanner;

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

public class testMain {
    static String dirPath;
    public static void main(String[] args) throws REXPMismatchException, REngineException         
{

        // For user input
        Scanner scanner = new Scanner(System.in );
        System.out.println("Enter the file path: ");

        dirPath = scanner.nextLine();

        RConnection c = new RConnection();
        // source the Palindrome function
        c.eval("source('/home/workspace/TestR/testMain.R')");

        REXP valueReturned = c.eval("testMain(dirPath)");
       System.out.println(valueReturned.asString());
    }
}

这是我的 R 函数:

testMain <- function(dirPath)
{
     p<-dirPath
     return(p)
}

有人可以帮我解决这个问题吗?

4

1 回答 1

1

可能这样的事情会起作用:

REXP valueReturned = c.eval("testMain(\""+dirPath+"\")");

问题是 - 我认为 - 在引用它之前,您没有为 R 上下文设置 dirPath 变量。

于 2014-09-08T12:11:26.950 回答