novicegeek
novicegeek

Reputation: 773

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

I am using Rserve to access an R script through my Java project. The java code asks for a user input to enter the file location and stores in a String variable. This variable is then passes through to the R function which should read the file location. But doing this I get the following error:

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)

Here is my java code:

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());
    }
}

Here is my R function:

testMain <- function(dirPath)
{
     p<-dirPath
     return(p)
}

Can someone please help me how to solve this?

Upvotes: 0

Views: 2390

Answers (1)

G&#225;bor Bakos
G&#225;bor Bakos

Reputation: 9100

Probably something like this would work:

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

The problem is -I think-, that you have not set the dirPath variable for the R context before referencing it.

Upvotes: 1

Related Questions