我正在尝试R Language
使用R.Net 1.5.5 版(从 NuGet 加载)创建一个“Hello World”示例。不幸的是,我见过的在线样本都没有工作。
这就是我所做的:
- 安装了 Microsoft R Open 3.2.4,增强的 R 发行版
为 Visual Studio安装R 工具(R 版本 3.2.4 (2016-03-16))
创建了一个 R 项目并测试了一个简单的脚本
- 创建了一个 MVC 应用程序并从 NuGet引用了 R.Net 1.5.5 版
我的问题:
我看到的所有在线示例都必须使用早期版本,因为我无法REngine
为我的生活创建一个实例!事实上,我不断得到:
找不到dll
...但C:\Program Files\Microsoft\MRO\R-3.2.4\bin\x64\r.dll
确实存在。
问:如何使用R.Net 1.5.5 版创建 REngine 实例?
我的代码看起来像:
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
}