Problem loading R own created libraries in Java/JR

2019-05-28 06:19发布

问题:

I created my own new R library (called "Media"). There is no problem when I try to load it with RGui, and I can call the functions defined in the new package. This is how I load it:

   > library(Media)

But, I'm also trying to call that functions from Java/JRI code, and when I load the new R package, Java doesn't seem to find the pacakge, throwing the message "Error in library(Media) : object 'Media' not found"

This is my current code using JRI:

    REXP rexpSetFolder = re.eval("setwd('C:/Users/Albert/Documents')");
    REXP rexpFolder = re.eval("getwd()");
    System.out.println(rexpFolder.asString());

    REXP rexpLoad = re.eval("library(Media)"); // fails

It also fails without the 'setwd' command, and simple calls to existing R functions work fine. I'm using R 2.10 and the latest JRI 0.5-0 under Windows.

Any help would be appreciated. Thank you very much.

Edit:

The parameter lib.loc seems to work, at least this sentence does not return an error:

library("Media", lib.loc = "c:/Users/Albert/Documents")

But after that, calling a function in the package with re.eval("myfunction()"); still fails, as the function is not properly found.

回答1:

You can modify the library path - see ?.libPaths in R, you simply want to add your private library to the path. The GUI does that for you, but if you are outside it doesn't happen. For example:

 re.eval(".libPaths('c:/users/foo/Documents/R')");

Then load your package.



回答2:

Did you install the library properly first? You might want to try using the lib.loc parameter.

library("Media", lib.loc = "c:/Users/Albert/Documents")


回答3:

My work-around was to copy the package from my personal library (%USERPROFILE%\Documents\R) to the global library (%R_HOME%\library).

It's not the best because this requires Administrator privileges which not all users will have...



标签: java r rjava jri