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.
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.)
精彩评论