我正在尝试使用 R.Net 1.5.5 版(从 NuGet 加载)在 R 语言
中创建一个“Hello World”示例。不幸的是,我看到的所有在线样本都不起作用。
这就是我所做的:
- 已安装 Microsoft R Open 3.2.4, the enhanced R distribution
已安装 R Tools for Visual Studio (R 版本 3.2.4 (2016-03-16))
创建了一个 R 项目并测试了一个简单的脚本
- 创建了一个 MVC 应用程序并从 NuGet 引用了 R.Net 1.5.5 版
我的问题:
我看到的所有在线示例都必须使用早期版本,因为我无法为我的生活创建 REngine
的实例!事实上,我不断得到:
Dll was not found
...然而 C:\Program Files\Microsoft\MRO\R-3.2.4\bin\x64\r.dll
确实存在。
问:如何使用 R.Net version 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
}
最佳答案
我不想回答我自己的问题,但这是...
Microsoft R Open 3.2.4增强的 R 发行版安装 x64 文件。因此,在任何 CPU 下运行都会导致失败,因为它将选择 x86(默认情况下)。
在
项目属性 -> 构建:在“常规”部分
- 选择 x64 作为您的
平台目标
关于c# - 使用 R.Net 版本 1.5.5 创建 REngine 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140322/