我需要将R连接到某个C#应用程序.我安装rscproxy_1.3并R_Scilab_DCOM3.0-1B5添加COM引用到STATCONNECTORCLNTLib,StatConnectorCommonLib和STATCONNECTORSRVLib,但我仍然不能得到它的工作.
当我运行以下测试程序时:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//COM references
using STATCONNECTORCLNTLib;
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;
namespace R_TESTING
{
class Program
{
static void Main(string[] args)
{
StatConnector sc1 = new STATCONNECTORSRVLib.StatConnectorClass();
sc1.Init("R");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
Unhandled Exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040013
at STATCONNECTORSRVLib.StatConnectorClass.Init(String bstrConnectorName)
Run Code Online (Sandbox Code Playgroud)
提前致谢.
更新: 好的,仍然没有运气.我会试着解释一下到目前为止我做了什么.
从rproject安装R-2.12.2-win.exe到
C:\Program Files\R\R-2.12.2下载rscproxy_1.3-1.zip并将其复制/粘贴到
C:\Program Files\R\R-2.12.2\library安装了R_Scilab_DCOM3.0-1B5.exe到
C:\Program Files (x86)\R\(D)COM Server
使用Scilab进行基本测试.我试图运行它,但我得到以下错误:
加载StatConnector服务器...完成初始化R ...函数调用失败代码:-2147221485文本:安装问题:无法加载连接器释放StatConnector服务器...完成
比我查看PATH /系统变量,发现没有路径/ R_HOME/R_USER信息.另外,我在注册表中找不到任何与R相关的内容.
我想我做的事情非常糟糕,所以我非常需要你们的帮助.
使用R.NET(我从NuGet安装了我的)和以下代码在一个新的C#控制台应用程序中(通过http://rdotnet.codeplex.com/进行了少量更改而复制).
它在指向32位版本的R v2.11.1时会起作用,但是当指向64位版本的R v2.11.1时它将不起作用(如下面的代码所示).
当我安装NuGet时,它会自动添加对当前项目的引用:RDotNet(RDotNet.dll)和RDotNet.NativeLIbrary(RDotNet.NativeLibrary.dll).在任何新项目中都需要这些引用.
在VS2012下工作(在VS2010下未经测试,但可能会有效).
在为"x32"和"所有CPU"编译时工作(在VS2012中的"Build..Configuration Manager"下).
// Call R from .NET. Advantage is that everything is in process.
// Tested on VS2012, will probably work on VS2010.
using System;
using System.IO;
using System.Linq;
using RDotNet;
class Program
{
static void Main(string[] args)
{
// Set the folder in which R.dll locates.
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = @"C:\Program Files (x86)\R\R-2.11.1\bin";
//var rBinPath = @"C:\Program Files\R\R-2.11.1-x64\bin"; // Doesn't work ("DLL was not found.")
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
using (REngine engine = REngine.CreateInstance("RDotNet"))
{
// Initializes settings.
engine.Initialize();
// .NET Framework array to R vector.
NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
engine.SetSymbol("group1", group1);
// Direct parsing from R script.
NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();
// Test difference of mean and get the P-value.
GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
double p = testResult["p.value"].AsNumeric().First();
Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
Console.WriteLine("P-value = {0:0.000}", p);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32946 次 |
| 最近记录: |