我的代码
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();
}
}
}
}
获取以下错误
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
已添加
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
答案 0 :(得分:3)
使用R.Net Collaboration of .NET Framework和R统计计算我的 问题得到解决,以下是代码中的少数修改
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);
}
}
}
答案 1 :(得分:1)
只需使用Evaluate
代替EagerEvaluate
,因为它不适用于当前版本的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();
}
}
}
}