R 包中的 rJava 类路径...适用于某些系统...不适用于其他系统

发布于 2024-10-19 02:00:02 字数 2894 浏览 3 评论 0原文

我为 R 构建了一个包,将 R 包装在一些 Java 类周围。在我的开发笔记本电脑(Ubuntu)上,这个包加载正确并且运行良好。在另外两台机器(一台 Ubuntu,一台 Debian)上,我尝试使用此包,但 .jpackage() 调用未设置类路径。

所有三台机器都运行 R 2.12.1 和 rJava .8-8,我认为这是最新的。

整个包位于 Google 代码,但以下是zzz.R 文件,用于在一台机器上设置类路径,但不能在其他机器上设置类路径:

##' @import rJava
.onLoad <- function(lib, pkg) {
    pathToSdk <- paste(system.file(package = "GSRadR") , "/gsrad_sample/lib/", sep="")

    jarPaths <- c(paste(pathToSdk, "clima_core-1.0.0.jar", sep=""),
                  paste(pathToSdk, "clima_GSRAD-1.0.0.jar", sep=""),
                  paste(pathToSdk, "colt-1.0.jar", sep=""),
                  paste(pathToSdk, "commons-lang-2.0.jar", sep=""),
                  paste(pathToSdk, "junit-3.8.1.jar", sep=""),
                  paste(pathToSdk, "log4j-1.2.8.jar", sep=""),
                  paste(pathToSdk, "xqore.jar", sep="")
                  )    
    .jpackage(pkg, morePaths=jarPaths)
    attach( javaImport( c("java.lang", "java.io")))
    packageStartupMessage( paste( "GSRadR loaded. The classpath is: ", paste(.jclassPath(), collapse=" " ) ) )        
}

在我的笔记本电脑上,它返回以下内容:

> require(GSRadR)
Loading required package: GSRadR
Loading required package: rJava
GSRadR loaded. The classpath is:  /home/jal/R/library/rJava/java /home/jal/R/library/GSRadR/gsrad_sample/lib/clima_core-1.0.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/clima_GSRAD-1.0.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/colt-1.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/commons-lang-2.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/junit-3.8.1.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/log4j-1.2.8.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/xqore.jar

但在我的其他机器上,它仅返回:

> require(GSRadR)
Loading required package: GSRadR
Loading required package: rJava
GSRadR loaded. The classpath is:  /usr/lib/R/site-library/rJava/java

有关可能导致 .jpackage() 调用以不同方式工作的原因的任何提示在不同的机器上?我之前使用 rJava 构建过包,并为 .onLoad() 函数使用相同的模板,没有出现任何问题。

编辑

因此,在其中一台无法正常工作的机器上,我尝试简单地以“非包”方式向类路径添加路径。结果失败了:

> .jaddClassPath("/home/jal/R/x86_64-pc-linux-gnu-library/2.12/GSRadR/gsrad_sample/lib/clima_core-1.0.0.jar")
> .jclassPath()
[1] "/usr/lib/R/site-library/rJava/java"

嗯...所以我无法在类路径中添加任何内容。但为什么?

编辑 II

当我将自定义库加载到一台无法工作的机器上时,我使用了临时库位置,如下所示:

install.packages("/tmp/GSRadR_0.01.tar.gz", lib=/my/path)

然后像这样加载库:

require(GARadR, lib=/my/path)

通过反复试验,我发现如果删除 lib= 位,它就会正常工作。那么,为什么将使用 rJava 的 R 包加载到自定义库位置会使 .jaddClassPath() 函数无法工作呢?

我也许可以解决这个问题,但我很想知道是什么导致了这种奇怪的(至少对我来说)行为。

I have built a package for R that wraps R around some Java classes. On my development laptop (Ubuntu) this package loads properly and works great. On two other machines (one Ubuntu, one Debian) I have tried to use this package and the classpath is not being set by the .jpackage() call.

All three machines are running R 2.12.1 and rJava .8-8 which I believe to be the most recent.

The entire package is up at Google Code but here's the contents of the zzz.R file which works to set the class path on one machine but not others:

##' @import rJava
.onLoad <- function(lib, pkg) {
    pathToSdk <- paste(system.file(package = "GSRadR") , "/gsrad_sample/lib/", sep="")

    jarPaths <- c(paste(pathToSdk, "clima_core-1.0.0.jar", sep=""),
                  paste(pathToSdk, "clima_GSRAD-1.0.0.jar", sep=""),
                  paste(pathToSdk, "colt-1.0.jar", sep=""),
                  paste(pathToSdk, "commons-lang-2.0.jar", sep=""),
                  paste(pathToSdk, "junit-3.8.1.jar", sep=""),
                  paste(pathToSdk, "log4j-1.2.8.jar", sep=""),
                  paste(pathToSdk, "xqore.jar", sep="")
                  )    
    .jpackage(pkg, morePaths=jarPaths)
    attach( javaImport( c("java.lang", "java.io")))
    packageStartupMessage( paste( "GSRadR loaded. The classpath is: ", paste(.jclassPath(), collapse=" " ) ) )        
}

On my laptop this returns the following:

> require(GSRadR)
Loading required package: GSRadR
Loading required package: rJava
GSRadR loaded. The classpath is:  /home/jal/R/library/rJava/java /home/jal/R/library/GSRadR/gsrad_sample/lib/clima_core-1.0.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/clima_GSRAD-1.0.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/colt-1.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/commons-lang-2.0.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/junit-3.8.1.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/log4j-1.2.8.jar /home/jal/R/library/GSRadR/gsrad_sample/lib/xqore.jar

But on my other machines it returns only:

> require(GSRadR)
Loading required package: GSRadR
Loading required package: rJava
GSRadR loaded. The classpath is:  /usr/lib/R/site-library/rJava/java

Any tips on what might cause the .jpackage() call to work differently on different machines? I've built packages using rJava before and used the same template for the .onLoad() function with no problems.

Edit

So on one of the machines where this was not working, I tried to simply add a path to the class path the "non package" way. And that failed:

> .jaddClassPath("/home/jal/R/x86_64-pc-linux-gnu-library/2.12/GSRadR/gsrad_sample/lib/clima_core-1.0.0.jar")
> .jclassPath()
[1] "/usr/lib/R/site-library/rJava/java"

Um... so I can't add anything to the class path. But why?

Edit II

When I was loading my custom library onto one of the machines that was not working, I was using a temporary library location, like so:

install.packages("/tmp/GSRadR_0.01.tar.gz", lib=/my/path)

then loading the library like this:

require(GARadR, lib=/my/path)

I discovered, through trial and error, that if I remove the lib= bit it would work properly. So why would loading an R package that uses rJava into a custom library location keep the .jaddClassPath() function from working?

I may be able to work around this, but I'd love to know what's causing this odd (at least to me) behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-10-26 02:00:02

我怀疑第一次编辑中的目录或文件不存在:/home/jal/R/x86_64-pc-linux-gnu-library/2.12/GSRadR/gsrad_sample/lib/clima_core-1.0.0。罐子。 (另外,您确定要添加该特定文件或目录吗?)

尝试 file.info("/home/jal/R/x86_64-pc-linux-gnu-library/2.12/GSRadR/ gsrad_sample/lib/clima_core-1.0.0.jar")。

就我而言,我尝试了 .jaddClassPath("/willy/wonka") 但它不起作用。但是当我尝试 .jaddClassPath("/home/voldemort") 时,它起作用了。 (让 Java 成为你的魂器。)

I suspect that the directory or file in the first edit doesn't exist: /home/jal/R/x86_64-pc-linux-gnu-library/2.12/GSRadR/gsrad_sample/lib/clima_core-1.0.0.jar. (Also, are you sure that you want to add that particular file, or the directory?)

Try file.info("/home/jal/R/x86_64-pc-linux-gnu-library/2.12/GSRadR/gsrad_sample/lib/clima_core-1.0.0.jar").

In my case, I tried .jaddClassPath("/willy/wonka") and it didn't work. But when I tried .jaddClassPath("/home/voldemort"), it worked. (Let Java be your horcrux.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文