3

I am able to perform Remote command execution and function calling in R script through Rserve in my Java application. But when my function is trying to save a dataframe in a csv file using

write.csv(MyData, file = "MyData.csv")

They MyData.csv file is not being generated, and no error is showing. when i am executing the same steps in R console, it working fine.

The Rserve is running in my local machine itself and I am using the following to connect and execute:

RConnection connection = new RConnection();
connection.eval("makecsv()")

p.s. I've omitted the "source the R script" step above

Just for reference this is my Dummy R script that I'm trying to run:

makecsv <- function(){
        x<-rnorm(10)
        y<-rnorm(10)
        df1<-data.frame(x,y)
        write.csv(df1, file = "MyData.csv")
        return(df1)
}
Madhavi Jouhari
  • 2,282
  • 5
  • 25
  • 40

3 Answers3

1

Probably you have to use the absolute path, something like this:

write.csv(MyData, file = "/var/MyData.csv")
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
  • Surprisingly, this fixed my problem :D Could you please let me know why this worked, or why my code was not working previously since I was following everything by the official document? – Madhavi Jouhari Jan 31 '18 at 07:42
  • 1
    I'm happy that my suggestion is useful. I'm using Rserve since a lot and I suppose that it has a different use of the working directory, so you have to use absolute path to read and write files. I'm sorry but I haven't any official documentation to share, just my experiance. – Terru_theTerror Jan 31 '18 at 08:18
  • 1
    Yes, the documentations is very limited for rserve, it is hard to find anything in detail. Thank you for your inputs though! – Madhavi Jouhari Jan 31 '18 at 08:21
0

This can happen if your Rserve is dead. Wrapping in try-catch with proper error handling can help in debugging.

This version works for me:

import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;

public class Main {
    public static void main(String[] args) {
        try {
            RConnection c = new RConnection();

            org.rosuda.REngine.REXP getwd = c.eval("getwd()");
            System.out.println(getwd.asString());

            c.eval("source(\"main.R\")");
            c.eval("makecsv()");
            c.close();
        } catch (REngineException | REXPMismatchException e) {
            e.printStackTrace();
        }
    }
}

The output is:

C:/Users/moon/Documents

Process finished with exit code 0

And in Documents folder I have the MyData.csv.

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
  • It seems pretty legit, I tried wrapping it with exception handling, was not able to generate the file. I checked the Rserve in parallel, it was up and running, since i was able to connect via R console. But thanks for your inputs :) – Madhavi Jouhari Jan 31 '18 at 07:39
0

Here are 2 suggestions:

  1. Try to parse the string to expression first by connection.eval(parse("makecsv()"))
  2. Check the working dir by connection.eval("getwd()")
Borislav Aymaliev
  • 803
  • 2
  • 9
  • 20