1. vs2015 nuget;
2. 搜索R.NET 把以下包下载到项目中:
<package id="DynamicInterop" version="0.7.4" targetFramework="net45" />
<package id="R.NET.Community" version="1.6.5" targetFramework="net45" />
<package id="R.NET.Community.FSharp" version="1.6.5" targetFramework="net45" />
3. 安装r语言包, 下载地址:
4. 新建控制台程序, 把测试代码加入, F5 正常运行:
using RDotNet;
using RDotNet.NativeLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
REngine.SetEnvironmentVariables();
// There are several options to initialize the engine, but by default the following suffice:
REngine engine = REngine.GetInstance();
// .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);
// you should always dispose of the REngine properly.
// After disposing of the engine, you cannot reinitialize nor reuse it
engine.Dispose();
}
}
}
控制台项目下载地址: http://download.csdn.net/detail/wdydxf/9906559