在.net项目中需要调用Matlab生成的DLL,但是在调用过程中报错,截图如下:
知道了我调用的DLL中有Matlab工具箱里面的函数,Matlab不允许某些工具箱中的工具被封装成DLL,于是就出现了上图所示的错误。
然后想用R语言实现。
2、下载RDotNet并编译
下载好后 打开 RDotNet.Tests解决方案,进行编译
3、测试
新建一个工程,引用如下图:
代码如下:
private static void Main(string[] args)
{string rHome = null;string rPath = null;if (args.Length > 0)
rPath= args[0];if (args.Length > 1)
rHome= args[1];
Console.WriteLine(RDotNet.NativeLibrary.NativeUtility.FindRPaths(ref rPath, refrHome));
rHome= null;
rPath= null;
REngine.SetEnvironmentVariables(rPath: rPath, rHome: rHome);
REngine e=REngine.GetInstance();//Console.WriteLine(RDotNet.NativeLibrary.NativeUtility.SetEnvironmentVariablesLog);//.NET Framework array to R vector.
NumericVector group1 = e.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99});
e.SetSymbol("group1", group1);//Direct parsing from R script.
NumericVector group2 = e.Evaluate("group2
GenericVector testResult = e.Evaluate("t.test(group1, group2)").AsList();double p = testResult["p.value"].AsNumeric().ToArray()[0];
Console.WriteLine("Group1: [{0}]", string.Join(",", group1));
Console.WriteLine("Group2: [{0}]", string.Join(",", group2));
Console.WriteLine("P-value = {0:0.000}", string.Join(",", p));
e.Dispose();
}
程序会根据R软件的注册表找到对应的dll从而实现调用R语言。
在调用R语言的时候,如果有的程序包没有引用的话需要在R程序菜单下的 “程序包” 来安装对应功能的程序包。
解决问题参考的博客:
原文:http://www.cnblogs.com/GIScore/p/5396647.html