如何使用Rserve将文件路径传递到Java中调用的Rscript?

如何使用Rserve将文件路径传递到Java中调用的Rscript?,java,r,filepath,rstudio,rserve,Java,R,Filepath,Rstudio,Rserve,我正在使用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.eva

我正在使用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)
}

testMain类似的方法可能会奏效:

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

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

@gábor bakos为什么Rserve停止工作,并在运行此代码时抛出“连接重置”?我的R代码与那里的代码完全相似,不同之处在于我提供了一个.csv文件并将其读取到一个data.frame Java
dirPath
“C:\\users\\documents\\some.csv”
而R代码是
testMain@sunitprasad1您的R代码在我看来不太好。你在R控制台上试过吗?(把这类问题作为单独的问题问是个好主意,这样你会得到更多的关注。)事实上,你是对的。那个R代码有问题。当然,我会把它作为一个单独的问题来问。