3

I am getting an exception at REngine.SetEnvironmentVariables() in my below code, may I know how can I check the environment variables are set correctly.

static void Main(string[] args)
    {
        REngine.SetEnvironmentVariables(); // <-- May be omitted; the next line would call it.

        REngine engine = REngine.GetInstance();
        // A somewhat contrived but customary Hello World:
        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();
        engine.Dispose();
    }

Visual studio Exception

Mani
  • 105
  • 1
  • 1
  • 8
  • Just a tip, adding the text for the error "DirectoryNotFoundExecption" to this thread will help others find this post, whenever they run into the same issue. – SQLServerSteve Jan 12 '21 at 00:53

2 Answers2

4

Old thread but in case anyone else has this problem: REngine.SetEnvironmentVariables() needs two paramters (the second one being Rs "outer" home directory), so based on David's answer I used

REngine.SetEnvironmentVariables("C:\\Program Files\\R\\R-3.2.3\\bin\\i386","C:\\Program Files\\R\\R-3.2.3");

and it works. It seems that REngine.SetEnvironmentVariables tries to read this from the registry anyways - in my case I don't have admin rights on the computer and while R itself works fine these values were not written to the registry during installation.

dunkleosteus
  • 336
  • 1
  • 12
  • This solution worked for me, even though I received a different error, saying the Win32 registry was missing (even though I'm on a 64-bit machine & 64-bit R). – SQLServerSteve Jan 12 '21 at 00:53
1

I believe rdotnet needs to know the path to your R installation. For example, this works for me:

REngine.SetEnvironmentVariables("C:\\Program Files\\R\\R-3.2.3\\bin\\i386");
David Pedack
  • 482
  • 2
  • 10