Additional R Packages

Jan 25, 2011 at 5:11 PM

This is an excellent extension, I've been able to implement R into my application with R.Net seamlessly however I can only access the base package.  Is there any way to import other packages into the engine?

Thanks,

Chris

Coordinator
Jan 26, 2011 at 4:00 AM
cr3116 wrote:

This is an excellent extension, I've been able to implement R into my application with R.Net seamlessly however I can only access the base package.  Is there any way to import other packages into the engine?

Thanks,

Chris

Hi Chris,

R.NET can use packages in usual 'library' and 'require' fucntions. These functions look for the specified package within the R library trees. The location of the library trees can be checked to call '.libPaths' function.

foreach (string path in engine.EagerEvaluate(".libPaths()").AsCharacter())
{
   Console.WriteLine(path);
}

If you cannot find the location in which you installed the package you want to use, you should tell the R environment about the location. There are several ways:

1. Calling '.libPaths' function

engine.EagerEvaluate(".libPaths(\"X:/Path/To/Library\")");

2. Setting Windows environment variable

Special environment variables, R_LIBS, R_LIBS_USER, and R_LIBS_SITE, defines the search paths; for instance, you set R_LIBS_USER as C:/Users/Somebody/AppData/Roaming/R/Library. For more information, read the R documentations (R Installation and Administration; R for Windows FAQ).

 

Thank you for using R.NET,

Kosei.

 

Apr 1, 2011 at 1:41 AM

I am failing to to execute "engine.EagerEvaluate("library(SGP)")" command.

I get a "System Error"  message box with following Message 

"The program can't start because RlaPack.dll is missing from your computer. Try reinstalling the program to fix this problem." 

Following is the  Exception:

The thread '<No Name>' (0x12fc) has exited with code 0 (0x0).Error in inDL(x, as.logical(local), as.logical(now), ...) :   unable to load shared object 'C:/Users/....../Documents/R/win-library/2.12/quantreg/libs/x64/quantreg.dll':  LoadLibrary failure:  The specified module could not be found.

Error: package 'quantreg' could not be loadedA first chance exception of type 'RDotNet.ParseException' occurred in R.NET.dll'TestR.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

-------

quantreg.dll exists in the specified location. I am able to run same command fine from R Console. Any help is appreciated. Thank you.

Coordinator
Apr 2, 2011 at 1:07 PM

I confirmed your problem. As the error message says, the problem occurs because your process cannot find RlaPack.dll. I didn't check why such a situation occurred, but I succeeded to make your code (engine.EagerEvaluate("library(SGP)")) work to put RlaPack.dll into the directory in which an executable file locates.

Thank you for your information.

May 18, 2011 at 5:59 PM

This really is an excellent project...MUCH easier to use R this way than wrestling with rscproxy, StatConnector, etc.

Just FYI, I was having this same issue, where I needed to put Rlapack.dll and lapack.dll in the R_HOME\bin directory to make things work. When I gave the code to a colleague, it didn't work and I spent a lot of time trying to re-solve the issue on the new machine. I finally realized that it was because, on the new machine, R_HOME\bin wasn't on the PATH environment variable. Adding the following line to my C# code cleared up the problem:

System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("R_HOME") + @"\bin");

No idea whether this would help in fixing the issue of a library that's there not being found. But it's odd that R, while under control of RDotNet, is looking for the libraries in the executable directory (PATH) rather than the library directories (R_LIBS, R_LIBS_USER, R_LIBS_SITE).

Again, great work and thanks! 

Mar 3, 2013 at 3:16 AM
I ran into similar issues on Win 7 x64 with R.Net 1.5 / R 2.15.2 .. I, too, was receiving the rlapack.dll missing error, even though I had pathed the bin\x64 directory (where rlapack.dll existed).

I found that copying the dll's to my app's bin directory solved the problem, but this was fairly unsatisfactory; after a number of iterations, I found that what resolved the problem was to add both the directory specified in R_HOME (in my case, c:\Apps\R) and the .\bin\x64 directory to my path.

I think there are a number of things going on here (some changes in R for versions prior to 2.14; different behavior for SetDllLocation in x64 vs. x32, etc. I was only able to track this down by downloading the source and stepping through the code in a test harness... here's how my final code looks.
   var path = System.Environment.GetEnvironmentVariable("Path");
      path = @"c:\apps\R\bin;c:\apps\R\bin\x64;" + path;

      System.Environment.SetEnvironmentVariable("Path",path);

      REngine.SetDllDirectory(@"C:\APPS\R\bin\x64");
      using (REngine engine = REngine.CreateInstance("RDotNet",new[] {@"R_HOME=c:\APPS\R",@"R_USER=c:\APPS\R"})) 
      {

        var o = engine.EagerEvaluate("2+2").AsNumeric()[0];

        Console.WriteLine(o);
        // .NET Framework array to R vector.
        NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
        engine.SetSymbol("group1", group1);
        // Direct parsing from R script.
        NumericVector group2 = engine.EagerEvaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();

        // Test difference of mean and get the P-value.
        GenericVector testResult = engine.EagerEvaluate("t.test(group1, group2)").AsList();
        double p = testResult["p.value"].AsNumeric().First();

        Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
        Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
        Console.WriteLine("P-value = {0:0.000}", p);
        
        Console.ReadLine();

      }

      Console.ReadLine();
Hope this helps