forked from Excel-DNA/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddIn.cs
More file actions
58 lines (51 loc) · 1.75 KB
/
Copy pathAddIn.cs
File metadata and controls
58 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
}
}
}