有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

通过Java从终端运行R

我正在尝试使用Java运行R。我在Mac电脑上安装了R,我在终端上使用过很多次

在终端中,要启动R,只需键入“R”

例:

 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.

> 

所以,我想做的是通过终端通过Java运行R。因此,我为自己编写了一个Java类:

public class javar {

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

但是,当我编译然后执行时,使用

java javar

我看不到R开始。我只是看到程序执行完毕,然后终端就可以执行另一个命令了

我怎样才能实现我想做的事


共 (1) 个答案

  1. # 1 楼答案

    当我遇到您的问题时,我最终使用了JRI,它不仅创建了一个R/Java连接,而且允许您在两者之间交换矩阵和向量

    虽然我不建议这样做,但您的方法可能会起作用,但R不会在您启动它后“挂起”,它只会不执行任何操作并退出。尝试添加“vanilla”选项,并显然为其提供一些代码,file=yourScript。使用args的R和finally参数

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