This project is read-only.

DLLNotFoundException with R.NET (Visual Studio 2010)

Apr 19, 2012 at 9:34 AM
Edited Apr 19, 2012 at 9:35 AM

Hello,

I'm having trouble setting up and running the example from http://rdotnet.codeplex.com/. I keep on running into a DLLNotFoundException and I'm not sure what else to check.

I downloaded the dll "R.NET"

Added it to my project as a reference.

Installed R and created a R_HOME environment variable.

Try to run the example and the exception occurs.

Tried looking at the sub block of code below, renaming the instance of R to R.NET it didn't work.

 

using (REngine engine = REngine.CreateInstance("RDotNet", new[] {""}))  // quiet mode
              //using (REngine engine = REngine.CreateInstance("R.NET", new[] { "-q" }))  // quiet mode

 

The code is the same as the example:

 

//http://rdotnet.codeplex.com/.

using System;
using System.Linq;
using RDotNet;


namespace RNetExample
{
    public class Program
    {

        static void Main(string[] args)
        {
          
            System.Environment.SetEnvironmentVariable("R_HOME", "C:\\Program Files\\R\\R-2.15.0\\bin");
            // Set the folder in which R.dll locates.
            REngine.SetDllDirectory(@"C:\LIB\DLL");

            Console.WriteLine(System.Environment.GetEnvironmentVariable("R_HOME"));

            try
            {
               using (REngine engine = REngine.CreateInstance("RDotNet", new[] {""}))  // quiet mode
              //using (REngine engine = REngine.CreateInstance("R.NET", new[] { "-q" }))  // quiet mode
                {
                    // .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);
                }
            }catch(DllNotFoundException e)
            {
                Console.WriteLine("DLL not found in location");
            }
            
        }
    }
}


Thanks for looking at my post.

Apr 20, 2012 at 9:42 AM

 

Hi Jules,

Not sure what is going on exactly from your description, but you may want to stick with the locations where you installed R, unless you have a good reason to use your folder  "c:\lib\dll". I just re-ran the same sample code as you, and it worked with the following setup phase for me. I believe that now R has distinct folders for 32/64 architectures, so your use of the "bin" location may be the issue. On a Win7 x64 I have a couple of folders x64 and i386. Not sure how it would look for a 32 bits OS. I think that the R "bin" folder now only has bootstrapping code. Below is also the list of files you would expect under your x64 or i386 folder.

Hope this helps. Cheers.

 

// main(string[] args):
var rdllloc = args.Length > 0 ? args[0] : @"c:\bin\R\R\bin\x64";
REngine.SetDllDirectory(rdllloc);
using (REngine engine = REngine.CreateInstance("RDotNet", new[] { @"R_HOME=c:\bin\R\R" }))
{
// body
}

 

R.dll        Rblas.dll  Rdll.lib       Riconv.dll   Rterm.exe
R.exe        Rcmd.exe   Rgraphapp.dll  Rlapack.dll  Rzlib.dll
R.def          RSetReg.exe  Rdll.exp   Rgui.exe       Rscript.exe  open.exe

Jun 27, 2012 at 10:02 PM

HI,

 

 

I am facing the same problem that Jules is facing.

I did all the steps / solutions avaialbe. Adding to PATH and R_HOME, but nothign helps.

Can some one please help me to get it working?

Jun 27, 2012 at 10:31 PM
Edited Jun 28, 2012 at 4:57 PM

The solution depends on what version of R.NET you are using.

 If you are using the version 4.1 dated Feb 25, 2011 then you must use the SetDllDirectory call like in the example code on the home page.

However, if you are running on a Windows 64 bit machine the directory shown may not work.

By default, when you compile a .NET application, the target CPU = "Any CPU".

That means that on a 64-bit system the application will run as 64-bit code.

 REngine.SetDllDirectory(@"C:\Program Files\R\R-2.12.0\bin\i386"); points to the 32-bit version of the DLL.

You cannot mix 32-bit and 64-bit dll's and exe's in the same process.

So, 64-bit applications must use the R.DLL file located in the ...\R\R-2....\bin\x64 directory instead. 

If you want your application to run only 64 bit then change the target CPU to be "x64" and point to the x64 version of R.DLL.

If you want your application to run only 32 bit then change the target CPU to be "i386" and point to the i386 version of R.DLL.

 If you want the application to remain as "Any CPU" you must do something like the following code that I use.

 EDIT:  I do not know why my code example was not included. I am adding it now

Dim R_Home As String
Dim R_Version As String
Dim R_Path As String
Dim regkey As Microsoft.Win32.RegistryKey = Nothing

regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\R-core\R", False)

If regkey Is Nothing Then Throw New IO.FileNotFoundException("Unable to locate R installation in Registry")

Dim value As Object = regkey.GetValue("Current Version")
If value Is Nothing Then Throw New IO.FileNotFoundException("Unable to locate current R version in Registry")

R_Version = value.ToString()
Console.WriteLine("R Version: {0}", R_Version)

value = regkey.GetValue("InstallPath")
If value Is Nothing Then Throw New IO.FileNotFoundException("Unable to locate current R Install Path in Registry")

R_Home = value.ToString()
Console.WriteLine("R Home: {0}", R_Home)

'Get the the system CPU architecture and report it
Dim arch As String = My.Computer.Info.OSPlatform
Console.WriteLine("OS Architecture: {0}", arch)

Dim is64Bit As Boolean = System.Environment.Is64BitProcess
Console.WriteLine("Is process 64-bit?", is64Bit)

If is64Bit Then
                R_Path = String.Format("{0}/bin/x64", R_Home)
Else
                R_Path = String.Format("{0}/bin/i386", R_Home)
End If

Dim F As New IO.FileInfo(IO.Path.Combine(R_Path, "R.dll"))

If Not F.Exists() Then Throw New IO.FileNotFoundException(String.Format("Could not find R.DLL in folder:  {0}", R_Path))

RDotNet.REngine.SetDllDirectory(R_Path)

Using R As REngine = REngine.CreateInstance("RDotNet"), {"-q"})

... etc
   

The latest version, available as a source code download only, uses the PATH environment instead of the SetDllDirectory call.

 

Jun 29, 2012 at 12:26 AM

Thanks scubaskier fora quick and useful reply.
   But I am still stuck. The DLL exception is gone, but when I am executing the code, the CreateInstance statement crashes without any error.
   

    REngine.SetDllDirectory(@"C:\Program Files\R\R-2.15.1\bin\x64");
    RDotNet.Devices.ConsoleDevice console = new RDotNet.Devices.ConsoleDevice();
    REngine engine = RDotNet.REngine.CreateInstance("RDotNet", new [] {""},console);
   

   I tried out all the options of CreateInstance available, but all show same behaviour.
   
   Any pointers highly appreciated.
   
   Thanks,
   Sonali
   

Jun 29, 2012 at 8:49 PM

Hello karniksonali,

I do not know what the root cause could be. The best course, IMHO, is to download the source code, include it in your solution and debug. That usually sheds light on the problem.

The only other thought would be to try running the 32-bit version. I have not tried the 64-bit version myself.

 

PS. I am sorry my first reply attempt started a new thread for some reason.

Jan 6, 2013 at 1:08 AM
Edited Jan 6, 2013 at 1:11 AM

To eliminate the "DLL was not found" error, you need to install the 32-bit version of R, then set the path to point at the 32-bit version of the .dll:

var rBinPath = @"C:\Program Files (x86)\R\R-2.11.1\bin";

I know that v 2.11.1 of R definitely works - perhaps later versions do not??

I tested the code above on VS 2012, but it should work just as nicely on VS2010.

For the full solution, see:

http://stackoverflow.com/questions/5377070/c-sharp-r-interface