c# - 使用 R.Net 版本 1.5.5 创建 REngine 实例

标签 c# r asp.net-mvc r.net

我正在尝试使用 R.Net 1.5.5 版(从 NuGet 加载)在 R 语言 中创建一个“Hello World”示例。不幸的是,我看到的所有在线样本都不起作用。

这就是我所做的:

我的问题:
我看到的所有在线示例都必须使用早期版本,因为我无法为我的生活创建 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 作为您的平台目标

enter image description here

关于c# - 使用 R.Net 版本 1.5.5 创建 REngine 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140322/

相关文章:

sql-server - 如何在 SQL Server 列中存储超过 varchar(max) 的非常大的数据?

asp.net-mvc - ASP 包中的绝对 URL

c# - 基本表单控件在子表单中不可见

c# - 如何检查 Word 段落是否是目录的一部分?

asp.net-mvc - 部分 View 中的 MVC 操作链接

r - 如何将变量传递给spark_apply()中调用的函数?

r - 为什么汇总和汇总给出的答案顺序不同?

c# - 在 List<Product> 中搜索产品名称

c# - 将数据 URI 设置为 asp :Image in code behind using ASP.Net 和 C#

r - 使用逻辑向量从字符向量中排除 FALSE 元素