繁体   English   中英

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

[英]How to pass a file path to Rscript called within Java using Rserve?

我正在使用Rserve通过我的Java项目访问R脚本。 Java代码要求用户输入输入文件位置并存储在String变量中。 然后将此变量传递给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)
}

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM