gpt4 book ai didi

java - 通过 Java 从终端运行 R

转载 作者:行者123 更新时间:2023-11-29 06:13:50 24 4
gpt4 key购买 nike

我正在尝试使用 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 启动。我只是看到程序完成执行,然后终端准备好接收另一个命令。

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

最佳答案

当我遇到你的问题时,我最终使用了 JRI ,它不仅创建 R/Java 连接,还允许您在两者之间交换矩阵和 vector 。

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

public class javar {

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

关于java - 通过 Java 从终端运行 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5833842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com