0

I am new c# developper. I need to run my R scripts within c# program. I find documentation here http://quantlabs.net/academy/very-nice-my-c-program-calls-r-code-through-the-r-net-package-dancing-in-the-streets/ and I tried to adapt it to my project. It usually showing this error :

REngine' does not contain a definition for 'SetDllDirectory'

REngine' does not contain a definition for 'CreateInstance'

I search here , I find suggestions here REngine' does not contain a definition for 'SetDllDirectory', 'RDotNet and I change it but no way.For your information I am working with the latest version of visual studio(2017) and I download the latest R.net package(1.7.0). Please, I need advice and help from whom were working in isssues that include interaction between R and C#.In fact, all my r scripts are just ready to run them in C# program.

My code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using RDotNet;
using Microsoft.Win32;

namespace Con_R
{
    class Program
    {
        static void Main(string[] args)
        {
            string rhome = System.Environment.GetEnvironmentVariable("R_HOME");
            if (string.IsNullOrEmpty(rhome))
                rhome = @"C:\Program Files\R\R-3.3.1";

            System.Environment.SetEnvironmentVariable("R_HOME", rhome);
            System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("PATH") + ";" + rhome + @"binx64");



            // Set the folder in which R.dll locates.
            //REngine.SetDllDirectory(@"C:Program FilesRR-2.12.0bini386″);
            REngine.SetDllDirectory(@"C:\Program Files\R\R-3.3.1\bin\x64");

            // REngine e = REngine.CreateInstance("test", new[] { "" });
            using (REngine engine = REngine.CreateInstance("RDotNet",  "-q" ))  // quiet mode
            {


                foreach (string path in engine.Evaluate(".libPaths()").AsCharacter())
                {
                    Console.WriteLine(path);
                }
                engine.Evaluate(".libPaths(C:\\Program Files\\R\\R-3.3.1\\library)");
                engine.Evaluate("source(D:\\R\\Script\\load_forecast_grid.r)");

                Console.ReadLine();
            }
        }
    }
}
Community
  • 1
  • 1
User2018
  • 61
  • 10
  • Looking at the [Source Code](https://github.com/jmp75/rdotnet/blob/master/R.NET/REngine.cs) for `REngine`, it looks like the `CreateInstance` method is `private` so you cannot call it directly. It looks like you should call `GetInstance` instead? Also, there is no definition for `SetDllDirectory` in the source code, but it seems you can specify the dll in the call to `GetInstance`. – Chris Dunaway Jan 04 '18 at 16:29
  • @ChrisDunaway,thanks for your answer ! It works with getInstance as you say! – User2018 Jan 08 '18 at 08:50

0 Answers0