0

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?

novicegeek
  • 773
  • 2
  • 9
  • 29

1 Answers1

1

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.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
  • @gábor-bakos Why does Rserve stop working and throws me a "Connection Reset" on running this code? My R code is exactly similar to the one there, with a difference that I'm supplying a .csv file and making it read to a data.frame Java `dirPath` is `"C:\\users\\documents\\some.csv"` and R code is `testMain <- function(dirPath) { p<-dirPath return(p) } read.csv(dirPath,stringsAsFactors=FALSE)` – sunitprasad1 Apr 23 '15 at 10:53
  • @sunitprasad1 Your R code does not look good to me. Have you tried it in an R console? (It is a good idea to ask these kinds of questions as separate questions, you will get more attention that way. [ask]) – Gábor Bakos Apr 23 '15 at 11:28
  • actually, you're correct. There is an issue with that R code. Sure, will ask it as a separate question. – sunitprasad1 Apr 23 '15 at 11:45