Creating an instance of the REngine using R.Net ve

2020-07-11 07:08发布

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:

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
}

1条回答
手持菜刀,她持情操
2楼-- · 2020-07-11 07:36

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

查看更多
登录 后发表回答