2

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.

Marek
  • 49,472
  • 15
  • 99
  • 121
Guido
  • 46,642
  • 28
  • 120
  • 174

3 Answers3

3

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.

Simon Urbanek
  • 13,842
  • 45
  • 45
1

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")
Shane
  • 98,550
  • 35
  • 224
  • 217
  • I'll try it, but the strange thing is that I can execute the same code without problems directly in RGui. Thank you. – Guido Mar 19 '10 at 14:59
  • The sentence seems to work with the argument "lib.loc" but, after that, calling a function in the package called "media" with re.eval("media( t )"); still fails, with a message that says that it was not able to find "media" function... – Guido Mar 22 '10 at 09:00
1

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

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Chris Pudney
  • 371
  • 2
  • 5
  • Sadly I didn't find a solution (yours seems a nice workaround). I finally inlined the functions in a re.eval call, instead of making them part of a library (as I was trying to do). – Guido Dec 09 '10 at 22:33