简体   繁体   English

从Java调用R-不同的R版本

[英]Call R from java - different R version

On the computers (running fedora) at my work R-2.15 is installed by default. 在我工作的计算机(运行fedora)上,默认安装R-2.15。 I downloaded R-3.0.2 because I need several packages which are only available for R 3. 我下载了R-3.0.2,因为我需要几个仅适用于R 3的软件包。

When i try to call an Rscript from java using Runtime.getRuntime().exec(command); 当我尝试使用Runtime.getRuntime().exec(command);从Java调用Rscript时Runtime.getRuntime().exec(command); with command being the String Array {"/path/to/Rscript/3-0" "name/of/script" "...args..."} I get the error WARNING: ignoring environment value of R_HOME and it says that it couldn't load the needed packages. 命令为字符串数组{"/path/to/Rscript/3-0" "name/of/script" "...args..."}我收到错误WARNING: ignoring environment value of R_HOME它说无法加载所需的软件包。 When i run the very same command in a terminal it works well. 当我在终端中运行相同的命令时,它运行良好。

What is the difference between the call via java and the call on the console. 通过java进行的调用与在控制台上进行的调用有什么区别。 And how can I fix it? 我该如何解决?

EDIT 1: when i run my locally installed R-3 version, libPaths returns 编辑1:当我运行本地安装的R-3版本时, libPaths返回

> .libPaths()
[1] "/home/<homedir>/.bin/R-3.0.2/library"

But when i call an Rscript from Java, libPaths returns 但是当我从Java调用Rscript时, libPaths返回

[1] "/home/<homedir>/R/x86_64-redhat-linux-gnu-library/2.15"
[2] "/usr/lib64/R/library"                                          
[3] "/usr/share/R/library"                                          
[4] "/home/<homedir>/.bin/R-3.0.2/library" 

The problem seems to be that Java adds any environment variables that tell R to load packages from the 2.15 installation. 问题似乎是Java添加了任何环境变量,这些环境变量告诉R从2.15安装中加载软件包。 How can i prevent this? 我该如何预防呢?

EDIT 2: When i list all environment variables using the System.getenv(); 编辑2:当我使用System.getenv();列出所有环境变量时System.getenv(); method nothing R -related is shown, the same is the case when i list the environment variables using printenv in the terminal. 方法没有显示任何与R相关的信息,与在终端中使用printenv列出环境变量时的情况相同。 But when i call Sys.getenv in R the variables R_HOME, R_LIBS, ... are defined. 但是,当我在R中调用Sys.getenv时,将定义变量R_HOME,R_LIBS,...。 How are these defined? 这些是如何定义的?

You can choose the library to look in when you load the package. 您可以选择加载程序包时要查找的库。 For example, when calling the script via Rscript, you could use 例如,通过Rscript调用脚本时,可以使用

library(rJava, lib.loc = .libPaths()[4])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM