Dinesh
Dinesh

Reputation: 663

passing files from R to Java

I m passing multiple tab delim files into R via Java.The R programm merges those tab delim files as single file and sends back to java and it is captured in the variable "name".Now I want to rename and save that file stored in "name" as tab delim using save dialog box in windows.Any help highly appreciated.Here is the java code:

import org.rosuda.REngine.*;

public class rjava {
    // Before this run Rserve() command in R


    public String ana(String filenames)
    {
        String name = "";
            try{

                System.out.println("INFO: Trying to connect to R ");
                RConnection c = new RConnection();
                System.out.println("INFO: Connected to R");
                System.out.println("INFO: The Server version is "+ c.getServerVersion());
            //  c.voidEval("source('D:/combine/combining_files.r')");
                c.voidEval("source('D:/combine/merge.r')");
                c.assign("file",filenames);
            //  name = (c.eval("fn(file)").asString());
                name = (c.eval("combine (file)").asString());
                c.close();
                }

    catch(Exception e)
    {
            System.out.println("ERROR: In Connection to R");
            System.out.println("The Exception is "+ e.getMessage());
            e.printStackTrace();
    }

    return name;
    }
}

Upvotes: 0

Views: 804

Answers (2)

JD Long
JD Long

Reputation: 60756

I find passing complex objects between R and Java to be a pain the ass. I would not pass the full data, but rather would pass only file names as a string. Either have Java tell R to write out the new file (my pref) or have Java read in the file and then write out with a new name.

Upvotes: 2

yottamoto
yottamoto

Reputation: 391

Can you modify the R program, so that it outputs files in the same path with a given file name, such as [path]/filename.out?

Otherwise, you can modify the execte string so that the R program outputs in a given location. See http://cran.r-project.org/doc/manuals/R-intro.html#Invoking-R-from-the-command-line

When working at a command line on UNIX or Windows, the command ‘R’ can be used both for starting the main R program in the form R [options] [<infile] [>outfile]

-- EDIT I just saw that you are using an RConnection. According to the R docs, you can define where to pipe stdout

The function sink, sink("record.lis") will divert all subsequent output from the console to an external file, record.lis.

Upvotes: 0

Related Questions