我需要将R与一些C#应用程序进行接口。我安装了rscproxy_1.3
和R_Scilab_DCOM3.0-1B5
,添加了对STATCONNECTORCLNTLib
、StatConnectorCommonLib
和STATCONNECTORSRVLib
的COM引用,但仍然无法工作。
当我运行以下测试程序时:
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");
}
}
}
我明白这一例外:
Unhandled Exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040013
at STATCONNECTORSRVLib.StatConnectorClass.Init(String bstrConnectorName)
提前谢谢。
更新: Ok,仍然没有运气。我将尽力解释我迄今所做的事情。
从rproject安装到
C:\Program Files\R\R-2.12.2
的R-2.12.2-win.exe 下载rscproxy_1.3-1.zip并将其复制/粘贴到C:\Program Files\R\R-2.12.2\library
安装到C:\Program Files (x86)\R\(D)COM Server
的R_Scilab_DCOM3.0-1B5.exe
与Scilab一起来了一个基本的测试。我试图运行它,但是我得到了以下错误:
加载StatConnector服务器..。完成初始化R...Function调用失败代码:-2147221485文本:安装问题:无法加载连接器释放StatConnector Server...Done
而不是在PATH/System变量中找到path/R_HOME/R_USER信息。而且,我在注册表中找不到任何与R相关的东西。
我想我做错了什么,所以我非常需要你们的帮助。
发布于 2011-03-22 10:14:34
好吧,我终于解决了。问题是R (D)Com不适用于当前版本的R. I安装了2.11.1,而且它是开箱即用的。
非常感谢。
发布于 2011-03-22 09:31:45
你可以看看R.NET,作为另一种方法.
发布于 2013-01-05 22:40:37
使用R.NET (我从NuGet安装了我的),并在一个新的C#控制台应用程序中使用下面的代码(该应用程序是通过从http://rdotnet.codeplex.com/复制的)。
当指向Rv2.11.1的32位版本时,它将工作,但如果指向Rv2.11.1的64位版本(如下面的代码所述),则无法工作。
当我安装NuGet时,它会自动添加对当前项目的引用:RDotNet
(RDotNet.dll)和RDotNet.NativeLIbrary
(RDotNet.NativeLibrary.dll)。在任何新项目中都需要这些参考资料。
在VS2012下工作(在VS2010下未进行测试,但可能会工作)。
为"x32“和"All CPU”编译时工作(在VS2012中的"Build..Configuration管理器“下)。
// 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);
}
}
}
https://stackoverflow.com/questions/5377070
复制相似问题