【问题标题】:How to export data from R script within Java using Rserve?如何使用 Rserve 从 Java 中的 R 脚本导出数据?
【发布时间】:2014-09-08 13:24:50
【问题描述】:

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

有人可以帮帮我吗?

【问题讨论】:

    标签: java r file-io rserve


    【解决方案1】:

    您的脚本有错误,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 .. }
    

    【讨论】:

      【解决方案2】:

      错误码127表示解析异常。

      换行:

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

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

      现在它应该可以工作了。

      【讨论】:

        猜你喜欢
        • 2017-06-10
        • 1970-01-01
        • 2021-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多