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
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
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:R
on command lineR.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 componentlib
) whether there is alibR.dylib
inside.Then enter on the command line:
and try to start your .NET app on the command line again. It will presumably show now:
But we know the home directory from our
R
command above. So next command is: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
commandR.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 usingfollowed by calling the C# test program on the command line - and it worked.
Screenshot CRAN Installation
Screenshot brew Installation
One notable difference is that I had to explicitly set
DYLD_LIBRARY_PATH
in the CRAN installation, which was not necessary with thebrew
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.