gpt4 book ai didi

java - 如何使用 Rserve 从 Java 中的 R 脚本导出数据?

转载 作者:行者123 更新时间:2023-11-29 08:45:22 26 4
gpt4 key购买 nike

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

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

有人可以帮帮我吗?

最佳答案

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

关于java - 如何使用 Rserve 从 Java 中的 R 脚本导出数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com