Graviton
Graviton

Reputation: 123

Loading rdotnet in C# console application fails with an unknown error on macOS

I have a very basic C# console application on macOS with the code pasted below. When I try to run this code, the call to REngine.GetInstance() fails with the following exception:

Unhandled exception. System.ArgumentException: This 64-bit process failed to load the library libR.dylib. No further error message from the dynamic library loader
   at DynamicInterop.UnmanagedDll.ThrowFailedLibraryLoad(String dllFullName, String nativeError)
   at DynamicInterop.UnmanagedDll.ReportLoadLibError(String dllName, String nativeError)
   at DynamicInterop.UnmanagedDll..ctor(String dllName)
   at RDotNet.REngine..ctor(String id, String dll)
   at RDotNet.REngine.CreateInstance(String id, String dll)
   at RDotNet.REngine.GetInstance(String dll, Boolean initialize, StartupParameter parameter, ICharacterDevice device)
   at RTest.Program.Main(String[] args) in /Users/.../Projects/RTest/RTest/Program.cs:line 14

I have made sure that there are no multiple R installations on the system. Also, tried to execute the same code on the M1 machine as well as on Intel-based Mac.

Here is the code:

using System;
using RDotNet;

namespace RTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var e = REngine.GetInstance();
            e.Initialize();

        }
    }
}

I have also tried explicitly specifying R_HOME and R_PATH using REngine.SetEnvironmentVariables(rPath: "/usr/local/Cellar/r/4.1.2_1/lib/R/lib", rHome: "/usr/local/Cellar/r/4.1.2_1"); but to no avail.

Any help will be highly appreciated.

Thanks

Upvotes: 0

Views: 470

Answers (2)

Stephan Schlecht
Stephan Schlecht

Reputation: 27106

R can be installed from CRAN (The Comprehensive R Archive Network) for macOS from here https://cran.r-project.org/bin/macosx/ or with brew - there are also mixed installations possible.

Also I added the line

Console.WriteLine($"is running: {e.IsRunning}");

to have an output on the command line in success case to your C# test program.

To get the relevant R home directory, you might do the following:

  • enter R on command line
  • enter R.home(component = "home")

CRAN Installation

With a CRAN installation it would give you /Library/Frameworks/R.framework/Resources.

Check with ls /Library/Frameworks/R.framework/Resources/lib (note the appended path component lib) whether there is a libR.dylib inside.

Then enter on the command line:

export DYLD_LIBRARY_PATH=/Library/Frameworks/R.framework/Resources/lib

and try to start your .NET app on the command line again. It will presumably show now:

Fatal error: R home directory is not defined

But we know the home directory from our R command above. So next command is:

export R_HOME=/Library/Frameworks/R.framework/Resources

Now when you try to start the C# test program on the command line, the message is running: True should appear, which means that this time it was successful.

brew Installation

The R command R.home(component = "home") outputs on my machine with brew: /usr/local/Cellar/r/4.1.3/lib/R.

Interestingly on that machine I got initially a different error: DirectoryNotFoundException: Directory '/Library/Frameworks/R.framework/Resources'. So I tried by only using

export R_HOME=/usr/local/Cellar/r/4.1.3/lib/R

followed by calling the C# test program on the command line - and it worked.

Screenshot CRAN Installation

cran

Screenshot brew Installation

brew

One notable difference is that I had to explicitly set DYLD_LIBRARY_PATH in the CRAN installation, which was not necessary with the brew installation.

REngine.SetEnvironmentVariables did not work for me either. So I used the command line to set the environment variables.

But there it was then possible to run it successfully with both installation types, as you can see in the screenshots.

Upvotes: 0

Spanners
Spanners

Reputation: 438

Have you verified that you have two environment variables set. Either via terminal or via your IDE Environment variable setting mechanism?

From memory the two you need are:

export LD_LIBRARY_PATH=/Library/Frameworks/R.framework/Libraries/:$LD_LIBRARY_PATH

and

export PATH=/Library/Frameworks/R.framework/Libraries/:$PATH

Upvotes: 1

Related Questions