从 java 调用 R 脚本

2022-09-02 20:43:23

我想从Java调用一个R脚本。我已经对这个主题进行了谷歌搜索,但是我看到的几乎所有结果都需要我向某些第三方库添加依赖项。任何人都可以向我展示一种很好的方法来完成同样的事情,而无需向我的代码添加任何依赖项?

我正在使用Windows计算机,因此也许我可能会使用命令行启动R(如果它尚未打开)并运行特定的R脚本。但是我从来没有写过命令行代码(或者从Java调用它),所以我需要代码示例。

我使用我的命令行想法,包括我为下面的一种可能的方法编写的工作示例代码。在我下面的内联评论中,您可以看到ComplibleDataFile.java中的第三步是我故意留空的。如果您认为可以使命令行想法起作用,那么请在步骤三中向我展示要编写的代码。

另外,请随时建议另一种方法,希望不涉及向我的代码添加更多依赖项。

而且,与往常一样,我非常感谢您可能发布到与此问题相关的文章/教程/等的任何链接。

以下是我到目前为止所拥有的:

汇编数据文件.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class AssembleDataFile {
static String delimiter;
static String localPath = "C:\\test\\cr\\";
static String[][] myDataArray;

public static void main(String[] args) {
    String inputPath = localPath+"pd\\";
    String fileName = "MSData.txt";
    delimiter = "\\t";

    // Step One: Import data in two parts
    try {
        // 1A: get length of data file
        BufferedReader br1 = new BufferedReader(new FileReader(inputPath+fileName));
        int numRows = 0;
        int numCols = 0;
        String currentRow;
        while ((currentRow = br1.readLine()) != null) {
            numRows += 1;
            numCols = currentRow.split(delimiter).length;}
        br1.close();
        //1B: populate data into array
        myDataArray = new String[numRows][numCols+1];
        BufferedReader br2 = new BufferedReader(new FileReader(inputPath+fileName));
        String eachRow;
        int rowIdx = 0;
        while ((eachRow = br2.readLine()) != null) {
            String[] splitRow = eachRow.split(delimiter);
            for(int z = 0;z < splitRow.length;z++){myDataArray[rowIdx][z] = splitRow[z];}
            rowIdx += 1;}
        br2.close();

        // Step Two: Write data to csv
        String rPath = localPath+"r\\";
        String sFileName = rPath+"2colData.csv";
        PrintWriter outputWriter = new PrintWriter(sFileName);
        for(int q = 0;q < myDataArray.length; q++){
            outputWriter.println(myDataArray[q][8]+", "+myDataArray[q][9]);
        }
        outputWriter.close();

        //Step Three: Call R script named My_R_Script.R that uses 2ColData.csv as input
        // not sure how to write this code.  Can anyone help me write this part?
        // For what it is worth, one of the R scripts that I intend to call is included below
        //
        //added the following lines here, per Vincent's suggestion:
            String rScriptFileName = rPath+"My_R_Script.R";
        Runtime.getRuntime().exec("mypathto\\R\\bin\\Rscript "+rScriptFileName);
        //
        //

        //Step Four: Import data from R and put it into myDataArray's empty last column
        try {Thread.sleep(30000);}//make this thread sleep for 30 seconds while R creates the needed file
        catch (InterruptedException e) {e.printStackTrace();}
        String matchFileName = rPath+"Matches.csv";
        BufferedReader br3 = new BufferedReader(new FileReader(matchFileName));
        String thisRow;
        int rowIndex = 0;
        while ((thisRow = br3.readLine()) != null) {
            String[] splitRow = thisRow.split(delimiter);
            myDataArray[rowIndex][numCols] = splitRow[0];
            rowIndex += 1;}
        br3.close();

        //Step Five: Check work by printing out one row from myDataArray
        //Note that the printout has one more column than the input file had.
        for(int u = 0;u<=numCols;u++){System.out.println(String.valueOf(myDataArray[1][u]));}
    }
    catch (FileNotFoundException e) {e.printStackTrace();}
    catch (IOException ie){ie.printStackTrace();}
}
}

My_R_Script

myCSV <- read.csv(file="2colData.csv",head=TRUE,sep=",")  
pts = SpatialPoints(myCSV)
Codes = readShapeSpatial("mypath/myshapefile.shp")  
write.csv(ZipCodes$F[overlay(pts,Codes)], "Matches.csv", quote=FALSE, row.names=FALSE)

编辑:
这是我添加 Runtime.getRuntime().exec(“Rscript ”+rScriptFileName) 时引发的错误消息;到上面的代码:

java.io.IOException: Cannot run program "Rscript": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at AssembleDataFile.main(AssembleDataFile.java:52)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more    

第二次编辑:上面的代码现在有效,因为我遵循了Vincent的建议。但是,我必须输入睡眠命令,以便为 R 脚本提供足够的时间来运行。如果没有 sleep 命令,上面的 java 代码会引发一个错误,指出 Matchs.csv文件不存在。我担心30秒的睡眠期太粗糙了。任何人都可以向我展示让java程序等到R程序有机会创建Matchs.csv的代码吗?我对使用线程工具犹豫不决,因为我已经读到设计不佳的线程可能会导致几乎不可能本地化和修复的错误。


答案 1

您只想调用外部应用程序:以下方法不起作用吗?

Runtime.getRuntime().exec("Rscript myScript.R"); 

答案 2

您可以轻松调整此代码:http://svn.rforge.net/org/trunk/rosuda/REngine/Rserve/test/StartRserve.java

除此之外,它查找R并在R中运行固定脚本 - 您可以使用脚本替换该脚本并忽略最后两个方法。


推荐