VS、C#配置R語言開發環境

R語言學習筆記(一)——在Vs、C#中配置R語言開發環境。算法

最近在學習小衆的R語言,因此將遇到的問題記錄下來供你們參考,不足之處歡迎你們交流指正。ide

至於R語言的介紹就很少說了,它集成了複雜的數學算法,將之封裝成簡單函數,開發者能夠直接調用,使用得當絕對是一把利器。函數

配置前準備:學習

1.R語言安裝包,由於是開源的因此你們能夠直接去官網下載。https://cran.r-project.org/src/base/R-3/測試

官網最新版是3.6.1,我這是使用的是3.4.1。ui

安裝包P地址lua

連接:https://pan.baidu.com/s/16Z4gD9uhIdDoqttkrJ7KBw
提取碼:xbj2 spa

 

注意:R語言的安裝包版本要與下面的引用類庫版本兼容,否則就會出現.net

engine = REngine.GetInstance(); engine = null的狀況。3d

我這裏使用的3.4.1與類庫版本親測兼容,你們嫌麻煩能夠直接使用個人。可是版本最高支持.netFrameWork 4.5。

2.R環境的引用類庫。

直接網盤奉獻:

連接:https://pan.baidu.com/s/1wYSLbXDs3CD6hFp5tcPKHg
提取碼:w2lo

正式開始:
一.打開下載好的安裝包,注意:要用管理員權限打開。

而後一步一步下一步。

 

 

 

下一步安裝便可。

二.打開VS,我這裏是2012。

1.新建控制檯

2.添加引用

 

3.能夠將下面這段代碼拷走測試

先設置R語言路徑、環境,後進行函數調用。

這裏給了兩種測試代碼。

 1         static void Main(string[] args)
 2         {
 3            Program mypro = new Program();
 4            mypro.ExcuteCode();
 5         }
 6         private REngine engine;
 7 
 8         #region 測試代碼
 9         public void ExcuteCode()
10         {
11             InitREngine();
12             #region Test1
13 
14             using (engine = REngine.GetInstance(null, true, null, null))
15             {
16                 engine.Initialize(); // required since v1.5
17                 CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
18                 engine.SetSymbol("greetings", charVec);
19                 engine.Evaluate("str(greetings)"); // print out in the console
20                 string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
21             }
22 
23             Console.ReadKey();
24 
25             #endregion
26 
27             #region Test2
28 
29             // 初始化R環境
30             //engine = REngine.GetInstance(null, true, null, null);
31 
32             //// .NET Framework array to R vector.
33             //NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
34             //engine.SetSymbol("group1", group1);
35             //// Direct parsing from R script.
36             //NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();
37 
38             //// Test difference of mean and get the P-value.
39             //GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
40             //double p = testResult["p.value"].AsNumeric().First();
41 
42             //Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
43             //Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
44             //Console.WriteLine("P-value = {0:0.000}", p);
45 
46             //// you should always dispose of the REngine properly.
47             //// After disposing of the engine, you cannot reinitialize nor reuse it
48             //engine.Dispose();
49             //Console.ReadKey();
50 
51             #endregion
52 
53         } 
54         #endregion
55 
56         #region 配置R環境
57         public void InitREngine()
58         {
59             var oldPath = System.Environment.GetEnvironmentVariable("PATH"); /////C:\Program Files\R\R-3.5.1\bin
60             var rPath = System.Environment.Is64BitProcess
61                 ? @"C:\Program Files\R\R-3.4.1\bin\x64"
62                 : @"C:\Program Files\R\R-3.4.1\bin\i386";
63             if (Directory.Exists(rPath) == false)
64             {
65                 throw new DirectoryNotFoundException(
66                     string.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath));
67             }
68 
69             var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);
70             System.Environment.SetEnvironmentVariable("PATH", newPath);
71             // NOTE: you may need to set up R_HOME manually also on some machines
72             string rHome = "";
73             var platform = Environment.OSVersion.Platform;
74             switch (platform)
75             {
76                 case PlatformID.Win32NT:
77                     break; // R on Windows seems to have a way to deduce its R_HOME if its R.dll is in the PATH
78                 case PlatformID.MacOSX:
79                     rHome = "/Library/Frameworks/R.framework/Resources";
80                     break;
81                 case PlatformID.Unix:
82                     rHome = "/usr/lib/R";
83                     break;
84                 default:
85                     throw new NotSupportedException(platform.ToString());
86             }
87 
88             if (!string.IsNullOrEmpty(rHome))
89             {
90                 Environment.SetEnvironmentVariable("R_HOME", rHome);
91             }
92         } 
93         #endregion
View Code

 

持續更博中...

相關文章
相關標籤/搜索