• C# 调用R语言


    在.net项目中需要调用Matlab生成的DLL,但是在调用过程中报错,截图如下:

    在网上搜索一下资料,看到该博客:https://cn.mathworks.com/matlabcentral/newsreader/view_thread/282351

    知道了我调用的DLL中有Matlab工具箱里面的函数,Matlab不允许某些工具箱中的工具被封装成DLL,于是就出现了上图所示的错误。

    然后想用R语言实现。

    1、先下载R软件:http://mirrors.opencas.cn/cran/,选择base

    2、下载RDotNet并编译

       下载地址:http://rdotnet.codeplex.com/

       下载好后 打开 RDotNet.Tests解决方案,进行编译

       编译好的RDotner下载地址如下:http://pan.baidu.com/s/1c2NTnK8

    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, ref rHome));
                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 <- 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 = 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软件的路径可以将上面的代码修改如下:

     string rHome = @"E:R-3.2.4revised";
     string rPath = Path.Combine(rHome, @"bini386");
    REngine.SetEnvironmentVariables(rPath,rHome);

    在调用R语言的时候,如果有的程序包没有引用的话需要在R程序菜单下的 “程序包” 来安装对应功能的程序包。

    解决问题参考的博客:

    https://psychwire.wordpress.com/2011/06/19/making-guis-using-c-and-r-with-the-help-of-r-net/

    http://blog.csdn.net/guoer9973/article/details/45953471

    若程序运行失败,提示

    Fatal error: Unable to open the base package

     和 找不到 R.DLL错误,

    说明R软件设置了环境变量,程序会优先使用环境变量,如果R的环境变量和你使用的R软件路径不一致就会出现上面的错误,这时候需要修改或者删除R的环境变量,然后重启一下电脑。

    删除方法:

    打开注册表:在“运行”里面输入“regedit”

    进入下面路径,然后修改或删除R_HOMR值,重启一下电脑就可以了。

    HKEY_LOCAL_MACHINESYSTEMControlSet003ControlSession ManagerEnvironment

  • 相关阅读:
    shell 中的expect 用法
    Python下安装protobuf
    google protobuf 中的proto文件编写规则
    ubuntu 16.04 安装grpc
    python 常见的错误类型 和 继承关系
    倒排索引
    python 调用c函数
    python中的re模块,常用函数介绍
    leecode第二十二题(括号生成)
    leecode第十九题(删除链表的倒数第N个节点)
  • 原文地址:https://www.cnblogs.com/GIScore/p/5396647.html
Copyright © 2020-2023  润新知