开发者

Running R from terminal via Java

I'm trying to run R using Java. I have R installed on my Mac and I've used it plenty of times from the terminal.

In the terminal,开发者_StackOverflow中文版 to start R, one simply types "R"

Ex:

 Macintosh-11:Desktop myname$ R

R version 2.12.2 (2011-02-25)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 

So, what I would like to do is run R through Java, via the terminal. So, I wrote myself a Java class:

public class javar {

        public static void main(String[] args) throws Exception {
                Runtime.getRuntime().exec("R");
        }
}

However, when I compile and then execute, using

java javar

I don't see R start. I simply see the program finish executing, and then the terminal is ready for another command.

How can I achieve what I'm trying to do?


when I encountered your problem, I ended up using JRI, which not only creates an R/Java connection, but allows you to exchange matrices and vectors to/from the two.

Although I don't suggest it, your approach may work, but R will not "hang" after you launch it, it will just execute nothing and exit. Try adding the "--vanilla" option and obviously feed some code to it, with --file=yourScript.R and eventually arguments using --args

public class javar {

        public static void main(String[] args) throws Exception {
                Runtime.getRuntime().exec("R --vanilla --file=yourScript.R");
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜