0

I need to run the same R script over and over with different parameters for each run. I've written the R portion and it works. I'm trying to integrate this process with some other Java tools I've written that will need to use on the files changed by the R script. I'm therefore trying to run my R file from Java. I've tried to run it using the command line prompt: RScript TestR.R (this command works in terminal), but that didn't work. I am therefore in the process of trying to run my R script from a bash file. I am using the same command as above and my bash file can execute other commands but for some reason nothing happens when I try to run the R file. The java command I'm using is

Process proc = Runtime.getRuntime().exec("absolute-path/testbash.sh");

I'm not sure what format of output to look for. Currently, this runs without any runtime errors.

UPDATE: when I checked the ErrorStream, it said "Rscript: command not found"

UPDATE: I figured it out by using "/usr/local/bin/RScript" as the command

Agastya Sharma
  • 79
  • 1
  • 1
  • 6
  • try putting the absolute path of the .sh script? Also, if "exec()" is unsuccessful, the Process should have holds some status. – Minh Kieu Jun 06 '17 at 16:07
  • Have you tried one of the other SO examples e.g.: [https://stackoverflow.com/questions/3468987/executing-another-application-from-java]. – morecore Jun 06 '17 at 16:07
  • Agastya, your question isn't super well formatted, could you spend a little more time to organize the question? To give us a better picture of where things are, etc. – alexmherrmann Jun 06 '17 at 16:18
  • What do you mean running the R code against `RScript` didn't work? This is currently the solution I use to run my R code from Java. // There's also a few R-Java interop libraries, but I never had much luck. – KDecker Jun 06 '17 at 16:23
  • @KDecker: What normally happens when you run your R code from Java? My R script contains a print statement-where would this appear? – Agastya Sharma Jun 06 '17 at 16:40
  • @MinhKieu I've tried using the absolute path and it's still not working. How can I view statuses in Process? Thanks! – Agastya Sharma Jun 06 '17 at 16:41
  • @alexmherrmann is there anything in particular you need clarified? I'm new to SO, so I apologize if I've forgotten anything important. – Agastya Sharma Jun 06 '17 at 16:42
  • @AgastyaSharma - The Process class has 3 methods for getting the streams. Try getting the error or the output stream to see what has written. – Minh Kieu Jun 06 '17 at 16:45
  • Apart from Java, when you run R with `RScript` whatever happens in the R code "happens". If you print, it prints to console, if you call a function, the function is called. In Java, if you use `Runtime` and `Process` to run `RScript` with your R code file as an argument and capture the console output, you can then parse that, and obtain whatever solution or answer the R code provides. – KDecker Jun 06 '17 at 16:55

2 Answers2

0

You can do it the following way:

new ProcessBuilder("<absolute-path-to-script>/testbash.sh").start();
VHS
  • 9,534
  • 3
  • 19
  • 43
0

With simple R code I have found it easier to call RScript from the terminal, within Java, and parse the output of the R code. If you print whatever information you want in an easily parsed format this can work well.

For instance I will call a function like so in Java

private ArrayList<String> runCommand(String command)
{
    try
    {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(command);

        BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(proc.getInputStream()));

        String lastReadLine = null;
        ArrayList<String> output = new ArrayList<>();
        while((lastReadLine = stdInput.readLine()) != null)
            output.add(lastReadLine);
        return output;
    }
    catch(Exception ex) { throw new RuntimeException("[RunCommand]: Exception when trying to execute command: " + ex.getMessage()); }
}

with the argument \pathTo\RScript \pathTo\MyRCode.R and parse the resulting ArrayList<String> in Java to obtain whatever information the R code produced.

There are other ways to do this to. You could potentially write out some JSON file from the R code and return a path to this in the R code output, Java could then parse and read this file. Or you could use one of the many R-Java interop libraries out there (Just google).

Hope this helped.

KDecker
  • 6,928
  • 8
  • 40
  • 81