4

My Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using RDotNet;
using RDotNet.Devices;
using RDotNet.Internals;
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", new[] { "-q" }))  // quiet mode
            {

                foreach (string path in engine.EagerEvaluate(".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();
            }
        }
    }
}

Gettting below error

Error 1 'RDotNet.REngine' does not contain a definition for 'SetDllDirectory' C:\Users\Shrinith_Sanil\Documents\Visual Studio 2013\Projects\Con_R\Con_R\Program.cs 161 21 Con_R

Error 2 'RDotNet.REngine' does not contain a definition for 'CreateInstance' C:\Users\Shrinith_Sanil\Documents\Visual Studio 2013\Projects\Con_R\Con_R\Program.cs 164 45 Con_R

Have added the

Error 3 'RDotNet.REngine' does not contain a definition for 'EagerEvaluate' and no extension method 'EagerEvaluate' accepting a first argument of type 'RDotNet.REngine' could be found (are you missing a using directive or an assembly reference?) C:\Users\Shrinith_Sanil\Documents\Visual Studio 2013\Projects\Con_R\Con_R\Program.cs 167 48 Con_R

Isabel Inc
  • 1,871
  • 2
  • 21
  • 28
Shrinith Sanil
  • 201
  • 2
  • 11

2 Answers2

3

By using R.Net Collaboration of .NET Framework with R statistical computing my issue got resolved and below are the few modification in code

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;
using System.IO;

namespace Con_R2
{
    class Program
    {

        static string  rPath = "";
        static void Main(string[] args)
        {

            SetupPath(); // current process, soon to be deprecated
            using (REngine engine = REngine.CreateInstance("RDotNet"))
            {
                engine.Initialize(); // required since v1.5

                REngine.SetDllDirectory(rPath);

                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('c:/Program Files/R/R-3.3.1/bin/load_forecast_grid.r')");
                engine.Evaluate("source('c:/Program Files/R/R-3.3.1/bin/testcmd.r')");
                Console.ReadLine();
                Console.ReadKey();
            }          
        }

        public static void SetupPath(string Rversion = "R-3.3.1")
        {
            var oldPath = System.Environment.GetEnvironmentVariable("PATH");
            rPath = System.Environment.Is64BitProcess ?
                                   string.Format(@"C:\Program Files\R\{0}\bin\x64", Rversion) :
                                   string.Format(@"C:\Program Files\R\{0}\bin\i386", Rversion);

            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);
        }


    }
}
Shrinith Sanil
  • 201
  • 2
  • 11
1

Just use the Evaluate instead of the EagerEvaluate as it's not available on the current version of the RDotNet.

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();
            }
        }
    }
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
  • Thanks for replying mok , it Evaluate thing worked , I am also getting the two more errors – Shrinith Sanil Jul 22 '16 at 12:49
  • Error 1 'RDotNet.REngine' does not contain a definition for 'SetDllDirectory' C:\Users\Shrinith_Sanil\Documents\Visual Studio 2013\Projects\Con_R\Con_R\Program.cs 163 21 Con_R Error 2 'RDotNet.REngine' does not contain a definition for 'CreateInstance' C:\Users\Shrinith_Sanil\Documents\Visual Studio 2013\Projects\Con_R\Con_R\Program.cs 166 45 Con_R – Shrinith Sanil Jul 22 '16 at 12:49
  • Have you added the current version of RDotNet using Nuget? – Mohsen Kamrani Jul 22 '16 at 12:51
  • Just make sure that you install the correct dll. http://stackoverflow.com/questions/30043036/f-r-provider-method-not-found-rdotnet-rengine-rdotnet-rengine-getinstance. – Mohsen Kamrani Jul 22 '16 at 13:14