如何让ExcelDNA与R.Net一起使用

时间:2015-03-26 13:36:46

标签: c# .net r excel-dna r.net

我是一名C#和R初学者试图运行示例http://mockquant.blogspot.com/2011/07/yet-another-way-to-use-r-in-excel-for.html

<DnaLibrary RuntimeVersion="v4.0" Name="My First XLL" Language="CS">
<ExternalLibrary Path="R.NET.dll" />
<Reference Name="R.NET" />
<![CDATA[using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ExcelDna.Integration;
    using RDotNet;

    namespace CSLib
    {
        public class CSLib
        {
            static REngine rengine = null;
            static CSLib()
            {
                // Set the folder in which R.dll locates.
                REngine.SetDllDirectory(@"C:\Program Files\R\R-2.13.0\bin\i386");
                rengine = REngine.CreateInstance("RDotNet", new[] { "-q" });
            }            
            [ExcelFunction(Description = "get random numbers obey to normal distribution")]
            public static double [] MyRnorm(int number)
            {
                return (rengine.EagerEvaluate("rnorm(" + number + ")").AsNumeric().ToArray<double>());
            }
        }
    }

我更新了SetDLLdirectory行中的链接,我尝试了32位和64位版本的R(我的cpu系统是win7 / 64位)

我尝试使用早期稳定版本的RDotNet,并使用Google搜索示例代码的更新,例如。这里:

https://groups.google.com/d/msg/exceldna/7_wr8pwuCZ0/GLKlVFjr6l8J

    <DnaLibrary RuntimeVersion="v4.0" Name="My First XLL" Language="CS">
<ExternalLibrary Path="RDotNet.dll" />
<ExternalLibrary Path="RDotNet.NativeLibrary.dll" />
<Reference Name="RDotNet" />
<Reference Name="RDotNet.NativeLibrary" />
<![CDATA[

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelDna.Integration;
using RDotNet;

namespace CSLib
{
    public class CSLib
    {
        static REngine rengine = null;
        static CSLib()
        {
            // Set the folder in which R.dll locates.
            var oldPath = System.Environment.GetEnvironmentVariable("PATH");
            var rPath = @"C:\Program Files\R\R-3.0.1\bin\x64";
    var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);

            System.Environment.SetEnvironmentVariable("PATH", newPath);
            rengine = REngine.CreateInstance("RDotNet");
        }            
        [ExcelFunction(Description = "get random numbers obey to normal distribution")]
        public static double [] MyRnorm(int number)
        {
            return (rengine.Evaluate("rnorm(" + number + ")").AsNumeric().ToArray<double>());
        }
    }
}

]]>
</DnaLibrary>

但我无法让它发挥作用......

在尝试了旧版本的r.net之后,我还尝试了使用旧代码的最新版本,然后我尝试将R.Net网站上的示例代码应用于上面的代码,假设r引擎的初始化现在使用了注册表中的路径:

<DnaLibrary RuntimeVersion="v4.0" Name="R.NET" Description="R.NETExcel" Language="CS">
<Reference Path="RDotNet.NativeLibrary.dll"  />
<Reference Path="RDotNet.dll"  />
<Reference Path="DynamicInterop.dll"  />

<![CDATA[
using System;
using System.IO;
using System.Linq;
using RDotNet;
using DynamicInterop;

namespace CSLib
{
    public class CSLib
    {




        public static double[] MyRnorm(int number)
        {
        REngine.SetEnvironmentVariables(); 
        REngine engine = REngine.GetInstance(); 
            engine.Initialize();

        return (engine.Evaluate("rnorm(" + number + ")").AsNumeric().ToArray<double>());

        engine.Dispose();   
    }

    }
}
]]>

</DnaLibrary>

这也没有结果。 Excel函数重新启动#num错误。

我确信当我注释掉试图连接到R的部分并粘贴其他一些简单的函数(如sum和两个值)时,ExcelDNA会起作用。

我认为我的问题可能与RdotNet的新发展有关,使得上面的示例代码过时(例如,它可能是初始化REngine实例的新方法)。我也想知道32位/ 64位冲突的可能性,这就是为什么我还试图让它在32位,win xp,dot.net 4.0上运行 - 没有结果。

那么将ExcelDNA连接到当前R.NET版本的正确方法是什么?

非常感谢您提前寻求帮助。

1 个答案:

答案 0 :(得分:3)

这些步骤对我来说很好:

  1. 确保已安装R.在我的Windows&#34;添加或删除程序&#34;列表我看到&#34; R代表Windows 3.02。

  2. 创建一个新的&#34;类库&#34; Visual Studio中的项目。

  3. 在NuGet包管理器控制台中,执行命令:

    PM> Install-Package Excel-DNA
    PM> Install-Package R.NET.Community
    
  4. 将以下代码添加到主.cs文件中:

    using System;
    using System.Linq;
    using ExcelDna.Integration;
    using ExcelDna.Logging;
    using RDotNet;
    
    namespace UsingRDotNet
    {
        public class AddIn : IExcelAddIn
        {
            public void AutoOpen()
            {
                MyFunctions.InitializeRDotNet();
            }
    
            public void AutoClose()
            {
            }
        }
    
        public static class MyFunctions
        {
            static REngine _engine;
            internal static void InitializeRDotNet()
            {
                try
                {
                    REngine.SetEnvironmentVariables();
                    _engine = REngine.GetInstance();
                    _engine.Initialize();
                }
                catch (Exception ex)
                {
                    LogDisplay.WriteLine("Error initializing RDotNet: " + ex.Message);
                }
            }
    
            public static double[] MyRnorm(int number)
            {
                return (_engine.Evaluate("rnorm(" + number + ")").AsNumeric().ToArray<double>());
            }
    
            public static object TestRDotNet()
            {
                // .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();
    
                return string.Format("Group1: [{0}], Group2: [{1}], P-value = {2:0.000}",  string.Join(", ", group1), string.Join(", ", group2), p);
            }
        }
    }
    
  5. F5在Excel中运行加载项。

  6. 输入公式= TestRDotNet()and = MyRNorm(5)`。数字显示在Excel中。

  7. 我已经添加了&#34; UsingRDotNet&#34;投射到Excel-DNA Samples on GitHub