请稍侯

C#调用R

28 June 2014

C#读作C Sharp,是微软公司在2000年6月发布的一种新的编程语言,是一种面向对象的,运行于.NET Framework之上的高级程序设计语言.C#可以很快捷的实现图形化界面,和R结合,能够很快生成一个应用工具.

本文介绍如何通过R.NET实现C#调用R.

R.NET是一个.NET框架包,该包可以实现.NET框架调用R.

首先,电脑在电脑中安装Visual StudioR.我的测试环境Visual Studio版本是Microsoft Visual Studio Ultimate 2013.R的版本基本信息如下,

sessionInfo()
## R version 3.1.0 (2014-04-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936 
## [2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936   
## [3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
## [4] LC_NUMERIC=C                                                   
## [5] LC_TIME=Chinese (Simplified)_People's Republic of China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] highlight_0.4.4 knitr_1.6      
## 
## loaded via a namespace (and not attached):
## [1] evaluate_0.5.5 formatR_0.10   stringr_0.6.2  tools_3.1.0

由于R.NET是发布在NuGet上的包,需要在Visual Studio上安装NuGet Package Manager.

打开Visual Studio,工具菜单栏下扩展和更新,选择联机下面的Visual Studio库,下载NuGet Package Manager for Visual Studio,安装并重启Visual Studio.

通过Visual Studio新建C#控制台应用程序类型解决方案Hello World.

右击解决方案资源管理器引用,选择管理NuGet程序包.

搜素联机R.NET,安装识别码为R.NET.CommunityR.NET.

安装成功后,引用会多出RDotNetRDotNet.NativeLibrary两项.

而且Visual Studio输出窗口会有相应输出.


------- 正在安装...R.NET.Community 1.5.15 -------
正在安装“R.NET.Community 1.5.15”。
已将文件“RDotNet.dll”添加到文件夹“R.NET.Community.1.5.15\lib\net40”。
已将文件“RDotNet.NativeLibrary.dll”添加到文件夹“R.NET.Community.1.5.15\lib\net40”。
已将文件“RDotNet.NativeLibrary.XML”添加到文件夹“R.NET.Community.1.5.15\lib\net40”。
已将文件“RDotNet.XML”添加到文件夹“R.NET.Community.1.5.15\lib\net40”。
已将文件“R.NET.Community.1.5.15.nupkg”添加到文件夹“R.NET.Community.1.5.15”。
已成功安装“R.NET.Community 1.5.15”。
正在将“R.NET.Community 1.5.15”添加到 Hello World。
用于将程序包“R.NET.Community 1.5.15”添加到目标为“net45”的项目“Hello World”,
>> 正在从“lib\net40” 添加 程序集引用
已将引用“RDotNet”添加到项目“Hello World”
已将引用“RDotNet.NativeLibrary”添加到项目“Hello World”
已将引用“System.Numerics”添加到项目“Hello World”
已添加文件“packages.config”。
已将文件“packages.config”添加到项目“Hello World”
已成功将“R.NET.Community 1.5.15”添加到 Hello World。
==============================

Program.cs中,输入如下代码.


using RDotNet;
using RDotNet.NativeLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
   class Program
   {
      static void Main(string[] args)
      {
         REngine.SetEnvironmentVariables(); // <-- May be omitted; next line would call it.
         REngine engine = REngine.GetInstance();
         // A somewhat contrived but customary Hello World:
         CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
         engine.SetSymbol("greetings", charVec);
         engine.Evaluate("str(greetings)"); // print out in the console
         string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
         Console.WriteLine("R answered: '{0}'", a[0]);
         Console.WriteLine("Press any key to exit the program");
         Console.ReadKey();
         engine.Dispose();
      }
   }
}

选择调试菜单栏下开始执行(不调试),输出如下.