Problem loading R own created libraries in Java/JRI code

醉酒当歌 提交于 2019-12-10 10:52:51

问题


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...



来源:https://stackoverflow.com/questions/2477398/problem-loading-r-own-created-libraries-in-java-jri-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!