2

我正在使用 Rserve 通过我的 Java 项目访问 R 脚本。java 代码要求用户输入文件位置并存储在字符串变量中。然后将该变量传递给 R 函数,该函数应该读取文件位置执行一些过程,然后创建一个新文件夹并将处理后的数据写入单个文件中,然后在控制台上打印出所有文件都已生成。我最初用较小版本的程序检查了 R 连接,它工作正常。但是,当我包含将数据写入文件的步骤时,它显示以下错误:输入文件路径:

/home/workspace/TestR/test_file
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)

此外,代码也不会在控制台上打印任何必须通过 R 从 Rscript 打印的语句。这是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/Main.R')");

        REXP valueReturned = c.eval("Main(\""+dirPath+"\")");
        //c.eval("Main(\""+dirPath+"\")");
        System.out.println(valueReturned.length());
    }

}

而且,这里是 R 脚本:

Main <- function(FILE_PATH)
{
  ## load libraries
  library(MALDIquant)
  library(MALDIquantForeign)
  library(dcemriS4)
  require(gridExtra) # also loads grid
  library(lattice)
  library(fields)
  library(matlab)
  library(rJava)

  #Call the source files of the function which this script will use
  source('/home/workspace/TestR/importAnalyzeFormat.R', echo=TRUE)
  source('/home/workspace/TestR/exportFile.R', echo=TRUE)
  source('/home/workspace/TestR/readRecalibratedSpectra.R', echo=TRUE)

  spectralDataObjects <- importAnalyzeFormat(FILE_PATH)

  p <- detectPeaks(spectralDataObjects, method="MAD", halfWindowSize=1, SNR=1)

  # Assign the p to preprocessedDataObjects
  preprocessedDataObjects<-p

  dir.create("PreprocessedSpectra", showWarnings = FALSE)
  setwd("PreprocessedSpectra")

  for(i in 1:length(preprocessedDataObjects))
  {
     coordinateValue<-metaData(preprocessedDataObjects[[i]])
     coordinates<-coordinateValue$imaging$pos 
     mzValues<-mass(preprocessedDataObjects[[i]])
     intensityValues<-intensity(preprocessedDataObjects[[i]])
     exportFile(coordinates,mzValues,intensityValues)
  }

  print("Files exported. Program will now terminate")
  print("############################################################")

  return(preprocessedDataObjects)
}

有人可以帮帮我吗?

4

2 回答 2

0

您的脚本中有错误,127 表示存在解析异常。

如果你使用这样的东西,它会在脚本中打印出错误。

在这种情况下,c 是 rserve 连接。

  c.assign(".tmp.", myCode);
    REXP r = c.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)");
    if (r.inherits("try-error")) System.err.println("Error: "+r.toString())
    else { // success .. }
于 2014-09-11T20:42:42.483 回答
0

错误代码 127 表示解析异常。

更改您的线路:

c.eval("source('/home/workspace/TestR/Main.R')");

c.eval("source(\"/home/workspace/TestR/Main.R\")");

现在它应该可以工作了。

于 2015-08-13T18:59:15.263 回答