Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an instance of the REngine using R.Net version 1.5.5

I am trying to create a "Hello World" example in R Language using R.Net version 1.5.5 (loaded from NuGet). Unfortunately, none of the online samples that I have seen work.

THIS IS WHAT I HAVE DONE:

  • Installed Microsoft R Open 3.2.4, the enhanced R distribution
  • Installed R Tools for Visual Studio (R version 3.2.4 (2016-03-16))

  • Created an R Project & tested a simple script

  • Created an MVC application & referenced R.Net version 1.5.5 from NuGet

MY PROBLEM:
All of the online examples I have seen must be using an earlier version because I cannot create an instance of the REngine for the LIFE of me! In fact, I keep getting:

Dll was not found

...yet C:\Program Files\Microsoft\MRO\R-3.2.4\bin\x64\r.dll does indeed exist.

Q: How do I create an instance of the REngine using R.Net version 1.5.5?

MY CODE LOOKS LIKE:

class Program
{
    #region <Methods>

    static void Main(string[] args)
    {
        SetupPath(); // current process, soon to be deprecated

        using (REngine engine = REngine.CreateInstance("RDotNet"))
        {
            engine.Initialize(); // required since v1.5
            CharacterVector charVec = engine.CreateCharacterVector(new[] {"Hello, R world!, .NET speaking" });

            engine.SetSymbol("greetings", charVec);
            engine.Evaluate("str(greetings)"); // print out in the console

            string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();

            Console.WriteLine("R answered: '{0}'", a[0]);

        }

        Console.WriteLine("Press any key to exit the program");
        Console.ReadKey();
    }

    public static void SetupPath()
    {
        var oldPath = System.Environment.GetEnvironmentVariable("PATH");
        var rPath = @"C:\Program Files\Microsoft\MRO\R-3.2.4\bin\x64";

        if (!Directory.Exists(rPath))
            throw new DirectoryNotFoundException(string.Format(" R.dll not found in : {0}", rPath));

        var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);

        System.Environment.SetEnvironmentVariable("PATH", newPath);
    }

    #endregion
}
like image 486
Prisoner ZERO Avatar asked May 10 '16 13:05

Prisoner ZERO


1 Answers

I hate to answer my own question, but here it is...

The Microsoft R Open 3.2.4 enhanced R distribution installs x64 files. As such, running under ANY CPU will cause a failure because it will choose x86 (by default).

Under
Project Properties -> Build: in the "General" section

  • Choose x64 as your Platform Target

enter image description here

like image 102
Prisoner ZERO Avatar answered Oct 20 '22 15:10

Prisoner ZERO