C#没有使用RDotNet连接到R. [英] C# not connecting to R using RDotNet

查看:1215
本文介绍了C#没有使用RDotNet连接到R.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 RDotNet 将C#连接到R。



以下代码是希望R计算两个数字的和和C#以获得结果,并在命令窗口中显示它。

 使用System; 
使用RDotNet;

命名空间rcon
{
类程序
{
static void Main(string [] args)
{
string dllPath = @C:\Program Files\R\R-3.1.0\bin\i386;

REngine.SetDllDirectory(dllPath);
REngine.CreateInstance(RDotNet);

// REngine engine = REngine.GetInstanceFromID(RDotNet);

使用(REngine engine = REngine.GetInstanceFromID(RDotNet))
{
var x = engine.Evaluate(x< - 1 + 2);
Console.WriteLine(x);
}
}
}
}

当我尝试发送命令到R,并得到回报在我的calue我有一个错误:


InvalidOperationException未处理 p>

由于对象的当前状态,操作无效。


如果我探索对象引擎,我看到 IsRunning = false



这可能是问题吗?我如何解决这个问题,以便能够与R接口?

解决方案

看起来您已经过时了版本的R.NET。



R.NET项目文档


R.NET 1.5.10和后续版本包括重大更改
显着减轻常常由用户处理的两个绊脚石:
到R共享库的路径,并防止多个引擎
初始化。


您可以使用Visual Studio中的NuGet管理器更新您的R.NET。



这里是来自同一个documentatin页面的代码示例 - 请注意 REngine 现在显着简化了(现在Rengine看着由R安装程序设置的注册表设置):

  REngine.SetEnvironmentVariables ); // < - 可以省略;下一行会调用它。 
REngine engine = REngine.GetInstance();
//有点麻烦,但习惯的Hello World:
CharacterVector charVec = engine.CreateCharacterVector(new [] {Hello,R world !, .NET speaking});
engine.SetSymbol(greetings,charVec);
engine.Evaluate(str(greetings)); //在控制台中打印
string [] a = engine.Evaluate('Hi there .NET,from the R engine')AsCharacter()。ToArray();
Console.WriteLine(R answered:'{0}',a [0]);
Console.WriteLine(按任意键退出程序);
Console.ReadKey();
engine.Dispose();


I am trying to interface C# to R using RDotNet.

The following code is wants R to calculate the sum of two numbers and C# to get the result back and display it in the command window.

using System;
using RDotNet;

namespace rcon
{
class Program
{
    static void Main(string[] args)
    {
        string dllPath = @"C:\Program Files\R\R-3.1.0\bin\i386";

        REngine.SetDllDirectory(dllPath);
        REngine.CreateInstance("RDotNet");

        //REngine engine = REngine.GetInstanceFromID("RDotNet");

        using (REngine engine = REngine.GetInstanceFromID("RDotNet"))
        {
            var x = engine.Evaluate("x <- 1 + 2");
            Console.WriteLine(x);
        }
    }
}
}

but when I try to send the command to R and get back the calue in x I got an error:

"InvalidOperationException was unhandled"

"Operation is not valid due to the current state of the object."

If I explore the object "engine" I see that IsRunning=false.

Can this be the problem? And how can I fix this in order to be able to interface to R?

解决方案

It looks like you have outdated version of R.NET.

From R.NET project documentation

R.NET 1.5.10 and subsequent versions include significant changes notably to alleviate two stumbling blocks often dealt with by users: paths to the R shared library, and preventing multiple engine initializations.

You can update your R.NET using NuGet manager from Visual Studio. See the same documentation page for detals.

Here is code sample from the same documentatin page - note that initialization of REngine is significantly simpler now (as now Rengine looks at the Registry settings set up by the R installer):

REngine.SetEnvironmentVariables(); // <-- May be omitted; the next line would call it.
REngine engine = REngine.GetInstance();
// A somewhat contrived but customary Hello World:
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();
engine.Dispose();

这篇关于C#没有使用RDotNet连接到R.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆